feat: add noIncompleteNsImportDetection assumption to plugin-transform-modules-commonjs (#13290)

This commit is contained in:
Federico Ciardi 2021-08-03 23:55:09 +02:00 committed by GitHub
parent c35637e247
commit 7e50ee2d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 64 additions and 11 deletions

View File

@ -265,6 +265,7 @@ export const assumptionsNames = new Set<string>([
"mutableTemplateObject",
"noClassCalls",
"noDocumentAll",
"noIncompleteNsImportDetection",
"noNewArrows",
"objectRestNoSymbols",
"privateFieldsAsProperties",

View File

@ -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");

View File

@ -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;
}

View File

@ -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,
},
);

View File

@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _default = foo;
exports.default = _default;

View File

@ -0,0 +1,7 @@
{
"assumptions": {
"noIncompleteNsImportDetection": true,
"constantReexports": true
},
"plugins": ["transform-modules-commonjs"]
}

View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _foo = require("foo");
exports.foo = _foo.foo;

View File

@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = 2;
exports.foo = foo;

View File

@ -0,0 +1,6 @@
{
"assumptions": {
"noIncompleteNsImportDetection": true
},
"plugins": ["transform-modules-commonjs"]
}