From 04f744891cfe00f9e6b3d4db6715d09823478313 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Tue, 29 Jun 2021 16:04:57 +0800 Subject: [PATCH] fix(commonjs): `ignoreDynamicRequires` should default to `false` See https://github.com/vitejs/vite/pull/3353#issuecomment-851520683 Fixes #3426 Fixes #3997 --- packages/vite/src/node/build.ts | 5 +---- packages/vite/types/commonjs.d.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index a6a3d707076aef..c39ef603fa861e 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -272,10 +272,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): { return { pre: [ buildHtmlPlugin(config), - commonjsPlugin({ - ignoreDynamicRequires: true, - ...options.commonjsOptions - }), + commonjsPlugin(options.commonjsOptions), dataURIPlugin(), dynamicImportVars(options.dynamicImportVarsOptions), assetImportMetaUrlPlugin(config), diff --git a/packages/vite/types/commonjs.d.ts b/packages/vite/types/commonjs.d.ts index 3ae7db8236bd17..fd9ab8aa191001 100644 --- a/packages/vite/types/commonjs.d.ts +++ b/packages/vite/types/commonjs.d.ts @@ -41,6 +41,18 @@ export interface RollupCommonJSOptions { * @default true */ sourceMap?: boolean + /** + * Some `require` calls cannot be resolved statically to be translated to + * imports. + * When this option is set to `false`, the generated code will either + * directly throw an error when such a call is encountered or, when + * `dynamicRequireTargets` is used, when such a call cannot be resolved with a + * configured dynamic require target. + * Setting this option to `true` will instead leave the `require` call in the + * code or use it as a fallback for `dynamicRequireTargets`. + * @default false + */ + ignoreDynamicRequires?: boolean /** * Instructs the plugin whether to enable mixed module transformations. This * is useful in scenarios with modules that contain a mix of ES `import`