Merge pull request #3118 from benjamn/import-hoisting
Add test of import hoisting that fails when the runtime transform is used.
This commit is contained in:
commit
f1ef0ff5e9
@ -157,15 +157,23 @@ export default function () {
|
|||||||
|
|
||||||
let requires = Object.create(null);
|
let requires = Object.create(null);
|
||||||
|
|
||||||
function addRequire(source) {
|
function addRequire(source, blockHoist) {
|
||||||
let cached = requires[source];
|
let cached = requires[source];
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
let ref = path.scope.generateUidIdentifier(basename(source, extname(source)));
|
let ref = path.scope.generateUidIdentifier(basename(source, extname(source)));
|
||||||
|
|
||||||
topNodes.push(t.variableDeclaration("var", [
|
let varDecl = t.variableDeclaration("var", [
|
||||||
t.variableDeclarator(ref, buildRequire(t.stringLiteral(source)).expression)
|
t.variableDeclarator(ref, buildRequire(
|
||||||
]));
|
t.stringLiteral(source)
|
||||||
|
).expression)
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (typeof blockHoist === "number" && blockHoist > 0) {
|
||||||
|
varDecl._blockHoist = blockHoist;
|
||||||
|
}
|
||||||
|
|
||||||
|
topNodes.push(varDecl);
|
||||||
|
|
||||||
return requires[source] = ref;
|
return requires[source] = ref;
|
||||||
}
|
}
|
||||||
@ -190,7 +198,24 @@ export default function () {
|
|||||||
|
|
||||||
if (path.isImportDeclaration()) {
|
if (path.isImportDeclaration()) {
|
||||||
hasImports = true;
|
hasImports = true;
|
||||||
addTo(imports, path.node.source.value, path.node.specifiers);
|
|
||||||
|
let key = path.node.source.value;
|
||||||
|
let importsEntry = imports[key] || {
|
||||||
|
specifiers: [],
|
||||||
|
maxBlockHoist: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
importsEntry.specifiers.push(...path.node.specifiers);
|
||||||
|
|
||||||
|
if (typeof path.node._blockHoist === "number") {
|
||||||
|
importsEntry.maxBlockHoist = Math.max(
|
||||||
|
path.node._blockHoist,
|
||||||
|
importsEntry.maxBlockHoist
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
imports[key] = importsEntry;
|
||||||
|
|
||||||
path.remove();
|
path.remove();
|
||||||
} else if (path.isExportDefaultDeclaration()) {
|
} else if (path.isExportDefaultDeclaration()) {
|
||||||
let declaration = path.get("declaration");
|
let declaration = path.get("declaration");
|
||||||
@ -262,7 +287,7 @@ export default function () {
|
|||||||
let nodes = [];
|
let nodes = [];
|
||||||
let source = path.node.source
|
let source = path.node.source
|
||||||
if (source) {
|
if (source) {
|
||||||
let ref = addRequire(source.value);
|
let ref = addRequire(source.value, path.node._blockHoist);
|
||||||
|
|
||||||
for (let specifier of specifiers) {
|
for (let specifier of specifiers) {
|
||||||
if (specifier.isExportNamespaceSpecifier()) {
|
if (specifier.isExportNamespaceSpecifier()) {
|
||||||
@ -288,16 +313,16 @@ export default function () {
|
|||||||
} else if (path.isExportAllDeclaration()) {
|
} else if (path.isExportAllDeclaration()) {
|
||||||
topNodes.push(buildExportAll({
|
topNodes.push(buildExportAll({
|
||||||
KEY: path.scope.generateUidIdentifier("key"),
|
KEY: path.scope.generateUidIdentifier("key"),
|
||||||
OBJECT: addRequire(path.node.source.value)
|
OBJECT: addRequire(path.node.source.value, path.node._blockHoist)
|
||||||
}));
|
}));
|
||||||
path.remove();
|
path.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let source in imports) {
|
for (let source in imports) {
|
||||||
let specifiers = imports[source];
|
let {specifiers, maxBlockHoist} = imports[source];
|
||||||
if (specifiers.length) {
|
if (specifiers.length) {
|
||||||
let uid = addRequire(source);
|
let uid = addRequire(source, maxBlockHoist);
|
||||||
|
|
||||||
let wildcard;
|
let wildcard;
|
||||||
|
|
||||||
@ -307,9 +332,21 @@ export default function () {
|
|||||||
if (strict) {
|
if (strict) {
|
||||||
remaps[specifier.local.name] = uid;
|
remaps[specifier.local.name] = uid;
|
||||||
} else {
|
} else {
|
||||||
topNodes.push(t.variableDeclaration("var", [
|
const varDecl = t.variableDeclaration("var", [
|
||||||
t.variableDeclarator(specifier.local, t.callExpression(this.addHelper("interopRequireWildcard"), [uid]))
|
t.variableDeclarator(
|
||||||
]));
|
specifier.local,
|
||||||
|
t.callExpression(
|
||||||
|
this.addHelper("interopRequireWildcard"),
|
||||||
|
[uid]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (maxBlockHoist > 0) {
|
||||||
|
varDecl._blockHoist = maxBlockHoist;
|
||||||
|
}
|
||||||
|
|
||||||
|
topNodes.push(varDecl);
|
||||||
}
|
}
|
||||||
wildcard = specifier.local;
|
wildcard = specifier.local;
|
||||||
} else if (t.isImportDefaultSpecifier(specifier)) {
|
} else if (t.isImportDefaultSpecifier(specifier)) {
|
||||||
@ -325,9 +362,21 @@ export default function () {
|
|||||||
target = wildcard;
|
target = wildcard;
|
||||||
} else {
|
} else {
|
||||||
target = wildcard = path.scope.generateUidIdentifier(uid.name);
|
target = wildcard = path.scope.generateUidIdentifier(uid.name);
|
||||||
topNodes.push(t.variableDeclaration("var", [
|
const varDecl = t.variableDeclaration("var", [
|
||||||
t.variableDeclarator(target, t.callExpression(this.addHelper("interopRequireDefault"), [uid]))
|
t.variableDeclarator(
|
||||||
]));
|
target,
|
||||||
|
t.callExpression(
|
||||||
|
this.addHelper("interopRequireDefault"),
|
||||||
|
[uid]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (maxBlockHoist > 0) {
|
||||||
|
varDecl._blockHoist = maxBlockHoist;
|
||||||
|
}
|
||||||
|
|
||||||
|
topNodes.push(varDecl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
remaps[specifier.local.name] = t.memberExpression(target, t.cloneWithoutLoc(specifier.imported));
|
remaps[specifier.local.name] = t.memberExpression(target, t.cloneWithoutLoc(specifier.imported));
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
tag`foo`;
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var _taggedTemplateLiteral2 = require("babel-runtime/helpers/taggedTemplateLiteral");
|
||||||
|
|
||||||
|
var _taggedTemplateLiteral3 = _interopRequireDefault(_taggedTemplateLiteral2);
|
||||||
|
|
||||||
|
var _templateObject = (0, _taggedTemplateLiteral3.default)(["foo"], ["foo"]);
|
||||||
|
|
||||||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||||
|
|
||||||
|
tag(_templateObject);
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"transform-runtime",
|
||||||
|
"transform-es2015-template-literals",
|
||||||
|
"transform-es2015-modules-commonjs"
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user