Upgrade to Babel 7

Also, update to webpack 4 and add support for dynamic import() and
import.meta in ?module mode.

Fixes #108
Fixes #151
This commit is contained in:
Michael Jackson
2018-12-05 22:54:42 -05:00
parent fde375782c
commit 4fd697aec1
8 changed files with 4545 additions and 1454 deletions

View File

@ -5,40 +5,85 @@ const origin = require("../serverConfig").origin;
const bareIdentifierFormat = /^((?:@[^/]+\/)?[^/]+)(\/.*)?$/;
function isValidURL(value) {
return URL.parseURL(value) != null;
}
function isProbablyURLWithoutProtocol(value) {
return value.substr(0, 2) === "//";
}
function isAbsoluteURL(value) {
return isValidURL(value) || isProbablyURLWithoutProtocol(value);
}
function isBareIdentifier(value) {
return value.charAt(0) !== "." && value.charAt(0) !== "/";
}
function rewriteValue(/* StringLiteral */ node, dependencies) {
if (isAbsoluteURL(node.value)) {
return;
}
if (isBareIdentifier(node.value)) {
// "bare" identifier
const match = bareIdentifierFormat.exec(node.value);
const packageName = match[1];
const file = match[2] || "";
warning(
dependencies[packageName],
'Missing version info for package "%s" in dependencies; falling back to "latest"',
packageName
);
const version = dependencies[packageName] || "latest";
node.value = `${origin}/${packageName}@${version}${file}?module`;
} else {
// local path
node.value = `${node.value}?module`;
}
}
function unpkgRewrite(dependencies = {}) {
return {
inherits: require("babel-plugin-syntax-export-extensions"),
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push(
"dynamicImport",
"exportDefaultFrom",
"exportNamespaceFrom",
"importMeta"
);
},
visitor: {
"ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration"(path) {
if (!path.node.source) return; // probably a variable declaration
if (
URL.parseURL(path.node.source.value) != null ||
path.node.source.value.substr(0, 2) === "//"
)
return; // valid URL or URL w/o protocol, leave it alone
if ([".", "/"].indexOf(path.node.source.value.charAt(0)) >= 0) {
// local path
path.node.source.value = `${path.node.source.value}?module`;
} else {
// "bare" identifier
const match = bareIdentifierFormat.exec(path.node.source.value);
const packageName = match[1];
const file = match[2] || "";
warning(
dependencies[packageName],
'Missing version info for package "%s" in dependencies; falling back to "latest"',
packageName
);
const version = dependencies[packageName] || "latest";
const url = `${origin}/${packageName}@${version}${file}?module`;
path.node.source.value = url;
CallExpression(path) {
if (path.node.callee.type !== "Import") {
// Some other function call, not import();
return;
}
rewriteValue(path.node.arguments[0], dependencies);
},
ExportAllDeclaration(path) {
return rewriteValue(path.node.source, dependencies);
},
ExportNamedDeclaration(path) {
if (!path.node.source) {
// This export has no "source", so it's probably
// a local variable or function, e.g.
// export { varName }
// export const constName = ...
// export function funcName() {}
return;
}
rewriteValue(path.node.source, dependencies);
},
ImportDeclaration(path) {
return rewriteValue(path.node.source, dependencies);
}
}
};