Specify runtime exports (#10853)
This commit is contained in:
@@ -90,24 +90,56 @@ function writeCoreJS({
|
||||
`module.exports = require("${corejsRoot}/${corejsPath}");`
|
||||
);
|
||||
});
|
||||
|
||||
writeCorejsExports(pkgDirname, runtimeRoot, paths);
|
||||
}
|
||||
|
||||
function writeCorejsExports(pkgDirname, runtimeRoot, paths) {
|
||||
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
|
||||
const pkgJson = require(pkgJsonPath);
|
||||
const exports = pkgJson.exports;
|
||||
// Export `./core-js/` so `import "@babel/runtime-corejs3/core-js/some-feature.js"` works
|
||||
exports[`./${runtimeRoot}/`] = `./${runtimeRoot}/`;
|
||||
for (const corejsPath of paths) {
|
||||
// Export `./core-js/some-feature` so `import "@babel/runtime-corejs3/core-js/some-feature"` also works
|
||||
const corejsExportPath = `./${runtimeRoot}/${corejsPath}`;
|
||||
exports[corejsExportPath] = corejsExportPath + ".js";
|
||||
}
|
||||
pkgJson.exports = exports;
|
||||
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
|
||||
}
|
||||
|
||||
function writeHelpers(runtimeName, { corejs } = {}) {
|
||||
writeHelperFiles(runtimeName, { corejs, esm: false });
|
||||
writeHelperFiles(runtimeName, { corejs, esm: true });
|
||||
const helperPaths = writeHelperFiles(runtimeName, { corejs, esm: false });
|
||||
const helperESMPaths = writeHelperFiles(runtimeName, { corejs, esm: true });
|
||||
writeHelperExports(runtimeName, helperPaths.concat(helperESMPaths));
|
||||
}
|
||||
|
||||
function writeHelperExports(runtimeName, helperPaths) {
|
||||
const helperSubExports = {};
|
||||
for (const helperPath of helperPaths) {
|
||||
helperSubExports[helperPath.replace(".js", "")] = helperPath;
|
||||
}
|
||||
const exports = {
|
||||
"./helpers/": "./helpers/",
|
||||
...helperSubExports,
|
||||
"./package.json": "./package.json",
|
||||
"./regenerator": "./regenerator/index.js",
|
||||
"./regenerator/": "./regenerator/",
|
||||
};
|
||||
const pkgDirname = getRuntimeRoot(runtimeName);
|
||||
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
|
||||
const pkgJson = require(pkgJsonPath);
|
||||
pkgJson.exports = exports;
|
||||
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
|
||||
}
|
||||
function writeHelperFiles(runtimeName, { esm, corejs }) {
|
||||
const pkgDirname = getRuntimeRoot(runtimeName);
|
||||
|
||||
const helperPaths = [];
|
||||
for (const helperName of helpers.list) {
|
||||
const helperFilename = path.join(
|
||||
pkgDirname,
|
||||
"helpers",
|
||||
esm ? "esm" : "",
|
||||
`${helperName}.js`
|
||||
);
|
||||
|
||||
const helperPath =
|
||||
"./" + path.join("helpers", esm ? "esm" : "", `${helperName}.js`);
|
||||
const helperFilename = path.join(pkgDirname, helperPath);
|
||||
outputFile(
|
||||
helperFilename,
|
||||
buildHelper(runtimeName, pkgDirname, helperFilename, helperName, {
|
||||
@@ -115,7 +147,11 @@ function writeHelperFiles(runtimeName, { esm, corejs }) {
|
||||
corejs,
|
||||
})
|
||||
);
|
||||
|
||||
helperPaths.push(helperPath);
|
||||
}
|
||||
|
||||
return helperPaths;
|
||||
}
|
||||
|
||||
function getRuntimeRoot(runtimeName) {
|
||||
@@ -171,11 +207,7 @@ function buildHelper(
|
||||
transformRuntime,
|
||||
{ corejs, useESModules: esm, version: runtimeVersion },
|
||||
],
|
||||
buildRuntimeRewritePlugin(
|
||||
runtimeName,
|
||||
path.relative(path.dirname(helperFilename), pkgDirname),
|
||||
helperName
|
||||
),
|
||||
buildRuntimeRewritePlugin(runtimeName, helperName, esm),
|
||||
],
|
||||
overrides: [
|
||||
{
|
||||
@@ -186,12 +218,19 @@ function buildHelper(
|
||||
}).code;
|
||||
}
|
||||
|
||||
function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
|
||||
function adjustImportPath(node, relativePath) {
|
||||
node.value =
|
||||
helpers.list.indexOf(node.value) !== -1
|
||||
? `./${node.value}`
|
||||
: node.value.replace(runtimeName + "/", relativePath + "/");
|
||||
function buildRuntimeRewritePlugin(runtimeName, helperName, esm) {
|
||||
const helperPath = esm ? "helpers/esm" : "helpers";
|
||||
/**
|
||||
* rewrite helpers imports to runtime imports
|
||||
* @example
|
||||
* adjustImportPath(ast`"setPrototypeOf"`)
|
||||
* // returns ast`"@babel/runtime/helpers/esm/setPrototypeOf"`
|
||||
* @param {*} node The string literal contains import path
|
||||
*/
|
||||
function adjustImportPath(node) {
|
||||
if (helpers.list.includes(node.value)) {
|
||||
node.value = `${runtimeName}/${helperPath}/${node.value}`;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -206,7 +245,7 @@ function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
|
||||
},
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
adjustImportPath(path.get("source").node, relativePath);
|
||||
adjustImportPath(path.get("source").node);
|
||||
},
|
||||
CallExpression(path) {
|
||||
if (
|
||||
@@ -217,9 +256,8 @@ function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
|
||||
return;
|
||||
}
|
||||
|
||||
// replace any reference to @babel/runtime and other helpers
|
||||
// with a relative path
|
||||
adjustImportPath(path.get("arguments")[0].node, relativePath);
|
||||
// replace reference to internal helpers with @babel/runtime import path
|
||||
adjustImportPath(path.get("arguments")[0].node);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user