Implement a new utility module for injecting module imports.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-module-transforms",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"babel-helper-module-imports": "7.0.0-beta.2",
|
||||
"babel-template": "7.0.0-beta.2",
|
||||
"babel-types": "7.0.0-beta.2",
|
||||
"lodash": "^4.2.0"
|
||||
|
||||
@@ -3,6 +3,8 @@ import * as t from "babel-types";
|
||||
import template from "babel-template";
|
||||
import chunk from "lodash/chunk";
|
||||
|
||||
import { isModule } from "babel-helper-module-imports";
|
||||
|
||||
import rewriteThis from "./rewrite-this";
|
||||
import rewriteLiveReferences from "./rewrite-live-references";
|
||||
import normalizeAndLoadModuleMetadata, {
|
||||
@@ -10,32 +12,7 @@ import normalizeAndLoadModuleMetadata, {
|
||||
isSideEffectImport,
|
||||
} from "./normalize-and-load-metadata";
|
||||
|
||||
export { hasExports, isSideEffectImport };
|
||||
|
||||
export function isModule(path: NodePath, requireUnambiguous: boolean = false) {
|
||||
const { sourceType } = path.node;
|
||||
if (sourceType !== "module" && sourceType !== "script") {
|
||||
throw path.buildCodeFrameError(
|
||||
`Unknown sourceType "${sourceType}", cannot transform.`,
|
||||
);
|
||||
}
|
||||
|
||||
const filename = path.hub.file.opts.filename;
|
||||
if (/\.mjs$/.test(filename)) {
|
||||
requireUnambiguous = false;
|
||||
}
|
||||
|
||||
return (
|
||||
path.node.sourceType === "module" &&
|
||||
(!requireUnambiguous || isUnambiguousModule(path))
|
||||
);
|
||||
}
|
||||
|
||||
// This approach is not ideal. It is here to preserve compatibility for now,
|
||||
// but really this should just return true or be deleted.
|
||||
function isUnambiguousModule(path) {
|
||||
return path.get("body").some(p => p.isModuleDeclaration());
|
||||
}
|
||||
export { hasExports, isSideEffectImport, isModule };
|
||||
|
||||
/**
|
||||
* Perform all of the generic ES6 module rewriting needed to handle initial
|
||||
|
||||
Reference in New Issue
Block a user