diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 3f7c74b9ac0d0a..3bc4d4ba26fb9d 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -162,12 +162,10 @@ This option is an alias of `build.rolldownOptions` option. Use `build.rolldownOp ## build.dynamicImportVarsOptions -- **Type:** [`RollupDynamicImportVarsOptions`](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#options) +- **Type:** `{ include?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[] }` - **Related:** [Dynamic Import](/guide/features#dynamic-import) -Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars). - - +Whether to transform dynamic imports with variables. ## build.lib diff --git a/docs/guide/features.md b/docs/guide/features.md index 1c8d268007b334..1e48b12f48bf21 100644 --- a/docs/guide/features.md +++ b/docs/guide/features.md @@ -642,6 +642,14 @@ const module = await import(`./dir/${file}.js`) Note that variables only represent file names one level deep. If `file` is `'foo/bar'`, the import would fail. For more advanced usage, you can use the [glob import](#glob-import) feature. +Also note that the dynamic import must match the following rules to be bundled: + +- Imports must start with `./` or `../`: ``import(`./dir/${foo}.js`)`` is valid, but ``import(`${foo}.js`)`` is not. +- Imports must end with a file extension: ``import(`./dir/${foo}.js`)`` is valid, but ``import(`./dir/${foo}`)`` is not. +- Imports to the own directory must specify a file name pattern: ``import(`./prefix-${foo}.js`)`` is valid, but ``import(`./${foo}.js`)`` is not. + +These rules are enforced to prevent accidentally importing files that are not intended to be bundled. For example, without these rules, `import(foo)` would bundle everything in the file system. + ## WebAssembly Pre-compiled `.wasm` files can be imported with `?init`. diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 89548650d4c0ba..6afb125fa594b3 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -335,6 +335,7 @@ The following options are deprecated and will be removed in the future: - `build.rollupOptions`: renamed to `build.rolldownOptions` - `worker.rollupOptions`: renamed to `worker.rolldownOptions` - `build.commonjsOptions`: it is now no-op +- `build.dynamicImportVarsOptions.warnOnError`: it is now no-op ## General Changes [](#migration-from-v7) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 149caf2acc05b8..cf172f8370a1cc 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -394,7 +394,6 @@ const _buildEnvironmentOptionsDefaults = Object.freeze({ extensions: ['.js', '.cjs'], }, dynamicImportVarsOptions: { - warnOnError: true, exclude: [/node_modules/], }, write: true, diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 85c807d059891d..b3f04420d117ea 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -272,11 +272,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { config.root, ) } catch (error) { - if (environment.config.build.dynamicImportVarsOptions.warnOnError) { - this.warn(error) - } else { - this.error(error) - } + this.warn(error) } if (!result) { diff --git a/packages/vite/src/types/dynamicImportVars.d.ts b/packages/vite/src/types/dynamicImportVars.d.ts index 99f1b5c453ba97..761fbe5f0ed552 100644 --- a/packages/vite/src/types/dynamicImportVars.d.ts +++ b/packages/vite/src/types/dynamicImportVars.d.ts @@ -10,8 +10,7 @@ export interface RollupDynamicImportVarsOptions { */ exclude?: string | RegExp | (string | RegExp)[] /** - * By default, the plugin quits the build process when it encounters an error. If you set this option to true, it will throw a warning instead and leave the code untouched. - * @default false + * @deprecated This option is no-op and will be removed in future versions. */ warnOnError?: boolean }