diff --git a/packages/babel-core/src/config/validation/options.ts b/packages/babel-core/src/config/validation/options.ts index 161462ef4f..7b9361bcd1 100644 --- a/packages/babel-core/src/config/validation/options.ts +++ b/packages/babel-core/src/config/validation/options.ts @@ -265,6 +265,7 @@ export const assumptionsNames = new Set([ "mutableTemplateObject", "noClassCalls", "noDocumentAll", + "noIncompleteNsImportDetection", "noNewArrows", "objectRestNoSymbols", "privateFieldsAsProperties", diff --git a/packages/babel-helper-create-class-features-plugin/src/index.ts b/packages/babel-helper-create-class-features-plugin/src/index.ts index d0f30d693c..24546834a1 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -46,7 +46,7 @@ export function createClassFeaturePlugin({ feature, loose, manipulateOptions, - // TODO(Babel 8): Remove the default falue + // TODO(Babel 8): Remove the default value api = { assumption: () => void 0 }, }: Options) { const setPublicClassFields = api.assumption("setPublicClassFields"); diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 826ff2f6d7..8b8b5364d8 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -45,6 +45,7 @@ export function rewriteModuleStatementsAndPrepareHeader( constantReexports = loose, enumerableModuleMeta = loose, + noIncompleteNsImportDetection, }: { exportName?; strict; @@ -57,6 +58,7 @@ export function rewriteModuleStatementsAndPrepareHeader( esNamespaceOnly?; constantReexports?; enumerableModuleMeta?; + noIncompleteNsImportDetection?: boolean; }, ) { validateImportInteropOption(importInterop); @@ -102,7 +104,12 @@ export function rewriteModuleStatementsAndPrepareHeader( // Create all of the statically known named exports. headers.push( - ...buildExportInitializationStatements(path, meta, constantReexports), + ...buildExportInitializationStatements( + path, + meta, + constantReexports, + noIncompleteNsImportDetection, + ), ); return { meta, headers }; @@ -388,6 +395,7 @@ function buildExportInitializationStatements( programPath: NodePath, metadata: ModuleMetadata, constantReexports: boolean = false, + noIncompleteNsImportDetection = false, ) { const initStatements = []; @@ -413,15 +421,17 @@ function buildExportInitializationStatements( } } - initStatements.push( - ...chunk(exportNames, 100).map(members => { - return buildInitStatement( - metadata, - members, - programPath.scope.buildUndefinedNode(), - ); - }), - ); + if (!noIncompleteNsImportDetection) { + initStatements.push( + ...chunk(exportNames, 100).map(members => { + return buildInitStatement( + metadata, + members, + programPath.scope.buildUndefinedNode(), + ); + }), + ); + } return initStatements; } diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.js b/packages/babel-plugin-transform-modules-commonjs/src/index.js index d63c9b60f6..e33768c1cc 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.js @@ -40,6 +40,8 @@ export default declare((api, options) => { api.assumption("constantReexports") ?? options.loose; const enumerableModuleMeta = api.assumption("enumerableModuleMeta") ?? options.loose; + const noIncompleteNsImportDetection = + api.assumption("noIncompleteNsImportDetection") ?? false; if ( typeof lazy !== "boolean" && @@ -186,6 +188,7 @@ export default declare((api, options) => { /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace, + noIncompleteNsImportDetection, }, ); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/input.mjs new file mode 100644 index 0000000000..f7b318b3f6 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/input.mjs @@ -0,0 +1 @@ +export default foo; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/output.js new file mode 100644 index 0000000000..bd88735863 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-default/output.js @@ -0,0 +1,7 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +var _default = foo; +exports.default = _default; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/input.mjs new file mode 100644 index 0000000000..d5296d83cd --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/input.mjs @@ -0,0 +1 @@ +export { foo } from "foo"; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/options.json new file mode 100644 index 0000000000..8c1658c6ca --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/options.json @@ -0,0 +1,7 @@ +{ + "assumptions": { + "noIncompleteNsImportDetection": true, + "constantReexports": true + }, + "plugins": ["transform-modules-commonjs"] +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/output.js new file mode 100644 index 0000000000..4917249d66 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export-from/output.js @@ -0,0 +1,9 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _foo = require("foo"); + +exports.foo = _foo.foo; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/input.mjs new file mode 100644 index 0000000000..2263b5a27c --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/input.mjs @@ -0,0 +1 @@ +export var foo = 2; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/output.js new file mode 100644 index 0000000000..d26a674b92 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/export/output.js @@ -0,0 +1,7 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +var foo = 2; +exports.foo = foo; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/options.json new file mode 100644 index 0000000000..9aaab0bfc4 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/assumptions-noIncompleteNsImportDetection/options.json @@ -0,0 +1,6 @@ +{ + "assumptions": { + "noIncompleteNsImportDetection": true + }, + "plugins": ["transform-modules-commonjs"] +}