Use strict namespace behavior for mjs files. (#7545)

This commit is contained in:
Logan Smyth
2018-03-15 13:27:01 -07:00
committed by GitHub
parent b6e54800b4
commit c662c2ada2
27 changed files with 83 additions and 25 deletions

View File

@@ -22,7 +22,16 @@ export { hasExports, isSideEffectImport, isModule };
*/
export function rewriteModuleStatementsAndPrepareHeader(
path: NodePath,
{ exportName, strict, allowTopLevelThis, strictMode, loose, noInterop, lazy },
{
exportName,
strict,
allowTopLevelThis,
strictMode,
loose,
noInterop,
lazy,
esNamespaceOnly,
},
) {
assert(isModule(path), "Cannot process module statements in a script");
path.node.sourceType = "script";
@@ -31,6 +40,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
noInterop,
loose,
lazy,
esNamespaceOnly,
});
if (!allowTopLevelThis) {

View File

@@ -86,7 +86,12 @@ export function isSideEffectImport(source: SourceModuleMetadata) {
export default function normalizeModuleAndLoadMetadata(
programPath: NodePath,
exportName?: string,
{ noInterop = false, loose = false, lazy = false } = {},
{
noInterop = false,
loose = false,
lazy = false,
esNamespaceOnly = false,
} = {},
): ModuleMetadata {
if (!exportName) {
exportName = programPath.scope.generateUidIdentifier("exports").name;
@@ -107,6 +112,16 @@ export default function normalizeModuleAndLoadMetadata(
}
if (noInterop) metadata.interop = "none";
else if (esNamespaceOnly) {
// Both the default and namespace interops pass through __esModule
// objects, but the namespace interop is used to enable Babel's
// destructuring-like interop behavior for normal CommonJS.
// Since some tooling has started to remove that behavior, we expose
// it as the `esNamespace` option.
if (metadata.interop === "namespace") {
metadata.interop = "default";
}
}
}
return {