Add a transformer to import the runtime from a file
The `externalRuntime` optional transformer can be used in conjunction with the `runtime` option to import and use the runtime from a module instead of polluting the global environment.
This commit is contained in:
parent
1c6cb7ce40
commit
e8237910e8
@ -87,7 +87,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
|||||||
var key = t.getSpecifierName(specifier);
|
var key = t.getSpecifierName(specifier);
|
||||||
var ref = this._push(node);
|
var ref = this._push(node);
|
||||||
|
|
||||||
if (t.isImportBatchSpecifier(specifier)) {
|
if (t.isImportBatchSpecifier(specifier) || node._noInteropRequire) {
|
||||||
// import * as bar from "foo";
|
// import * as bar from "foo";
|
||||||
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||||
// import foo from "foo";
|
// import foo from "foo";
|
||||||
|
|||||||
@ -26,13 +26,11 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
|
|||||||
|
|
||||||
// import foo from "foo";
|
// import foo from "foo";
|
||||||
if (t.isSpecifierDefault(specifier)) {
|
if (t.isSpecifierDefault(specifier)) {
|
||||||
nodes.push(t.variableDeclaration("var", [
|
var ref = util.template("require", {MODULE_NAME: node.source});
|
||||||
t.variableDeclarator(variableName,
|
if (!node._noInteropRequire) {
|
||||||
t.callExpression(this.file.addHelper("interop-require"), [util.template("require", {
|
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||||
MODULE_NAME: node.source
|
}
|
||||||
})])
|
nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)]));
|
||||||
)
|
|
||||||
]));
|
|
||||||
} else {
|
} else {
|
||||||
if (specifier.type === "ImportBatchSpecifier") {
|
if (specifier.type === "ImportBatchSpecifier") {
|
||||||
// import * as bar from "foo";
|
// import * as bar from "foo";
|
||||||
|
|||||||
@ -95,6 +95,7 @@ _.each({
|
|||||||
_moduleFormatter: require("./transformers/_module-formatter"),
|
_moduleFormatter: require("./transformers/_module-formatter"),
|
||||||
|
|
||||||
typeofSymbol: require("./transformers/optional-typeof-symbol"),
|
typeofSymbol: require("./transformers/optional-typeof-symbol"),
|
||||||
|
externalRuntime: require("./transformers/optional-external-runtime"),
|
||||||
coreAliasing: require("./transformers/optional-core-aliasing"),
|
coreAliasing: require("./transformers/optional-core-aliasing"),
|
||||||
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
|
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
exports.optional = true;
|
||||||
|
|
||||||
|
// In theory, it would be more appropriate to do this in `manipulateOptions`,
|
||||||
|
// but we need an identifier for the import and we can't get that before the
|
||||||
|
// AST is built.
|
||||||
|
exports.ast = {
|
||||||
|
enter: function (ast, file) {
|
||||||
|
file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name;
|
||||||
|
file.dynamicImports[file.dynamicImports.length - 1]._noInteropRequire = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user