Allow transform-runtime to insert runtime references with absolute paths. (#8435)
This commit is contained in:
parent
8c65230258
commit
1e0b649485
@ -6,6 +6,7 @@ import sourceMap from "source-map";
|
||||
import { codeFrameColumns } from "@babel/code-frame";
|
||||
import defaults from "lodash/defaults";
|
||||
import includes from "lodash/includes";
|
||||
import escapeRegExp from "lodash/escapeRegExp";
|
||||
import * as helpers from "./helpers";
|
||||
import extend from "lodash/extend";
|
||||
import merge from "lodash/merge";
|
||||
@ -180,7 +181,7 @@ function run(task) {
|
||||
function getOpts(self) {
|
||||
const newOpts = merge(
|
||||
{
|
||||
cwd: path.dirname(self.filename),
|
||||
cwd: path.dirname(self.loc),
|
||||
filename: self.loc,
|
||||
filenameRelative: self.filename,
|
||||
sourceFileName: self.filename,
|
||||
@ -235,10 +236,15 @@ function run(task) {
|
||||
const expectCode = expected.code;
|
||||
if (!execCode || actualCode) {
|
||||
result = babel.transform(actualCode, getOpts(actual));
|
||||
const expectedCode = result.code.replace(
|
||||
escapeRegExp(path.resolve(__dirname, "../../../")),
|
||||
"<CWD>",
|
||||
);
|
||||
|
||||
checkDuplicatedNodes(result.ast);
|
||||
if (
|
||||
!expected.code &&
|
||||
result.code &&
|
||||
expectedCode &&
|
||||
!opts.throws &&
|
||||
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
|
||||
!process.env.CI
|
||||
@ -249,7 +255,7 @@ function run(task) {
|
||||
);
|
||||
|
||||
console.log(`New test file created: ${expectedFile}`);
|
||||
fs.writeFileSync(expectedFile, `${result.code}\n`);
|
||||
fs.writeFileSync(expectedFile, `${expectedCode}\n`);
|
||||
|
||||
if (expected.loc !== expectedFile) {
|
||||
try {
|
||||
@ -257,7 +263,7 @@ function run(task) {
|
||||
} catch (e) {}
|
||||
}
|
||||
} else {
|
||||
actualCode = result.code.trim();
|
||||
actualCode = expectedCode.trim();
|
||||
try {
|
||||
expect(actualCode).toEqualFile({
|
||||
filename: expected.loc,
|
||||
@ -267,7 +273,7 @@ function run(task) {
|
||||
if (!process.env.OVERWRITE) throw e;
|
||||
|
||||
console.log(`Updated test file: ${expected.loc}`);
|
||||
fs.writeFileSync(expected.loc, `${result.code}\n`);
|
||||
fs.writeFileSync(expected.loc, `${expectedCode}\n`);
|
||||
}
|
||||
|
||||
if (actualCode) {
|
||||
|
||||
@ -10,7 +10,8 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/helper-module-imports": "7.0.0-rc.1",
|
||||
"@babel/helper-plugin-utils": "7.0.0-rc.1"
|
||||
"@babel/helper-plugin-utils": "7.0.0-rc.1",
|
||||
"resolve": "^1.8.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@babel/core": "^7.0.0-0"
|
||||
@ -21,6 +22,7 @@
|
||||
"@babel/helpers": "7.0.0-rc.1",
|
||||
"@babel/plugin-transform-runtime": "7.0.0-rc.1",
|
||||
"@babel/preset-env": "7.0.0-rc.1",
|
||||
"@babel/runtime": "7.0.0-rc.1",
|
||||
"@babel/template": "7.0.0-rc.1",
|
||||
"@babel/types": "7.0.0-beta.53"
|
||||
}
|
||||
|
||||
@ -1,10 +1,31 @@
|
||||
import path from "path";
|
||||
import resolve from "resolve";
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import { addDefault, isModule } from "@babel/helper-module-imports";
|
||||
import { types as t } from "@babel/core";
|
||||
|
||||
import definitions from "./definitions";
|
||||
|
||||
export default declare((api, options) => {
|
||||
function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
|
||||
try {
|
||||
return path.dirname(
|
||||
resolve.sync(`${moduleName}/package.json`, { basedir: dirname }),
|
||||
);
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
|
||||
throw Object.assign(
|
||||
new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
|
||||
{
|
||||
code: "BABEL_RUNTIME_NOT_FOUND",
|
||||
runtime: moduleName,
|
||||
dirname,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default declare((api, options, dirname) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const {
|
||||
@ -13,6 +34,7 @@ export default declare((api, options) => {
|
||||
regenerator: useRuntimeRegenerator = true,
|
||||
useESModules = false,
|
||||
version: runtimeVersion = "7.0.0-beta.0",
|
||||
absoluteRuntime = false,
|
||||
} = options;
|
||||
|
||||
if (typeof useRuntimeRegenerator !== "boolean") {
|
||||
@ -28,6 +50,14 @@ export default declare((api, options) => {
|
||||
"The 'useESModules' option must be undefined, or a boolean.",
|
||||
);
|
||||
}
|
||||
if (
|
||||
typeof absoluteRuntime !== "boolean" &&
|
||||
typeof absoluteRuntime !== "string"
|
||||
) {
|
||||
throw new Error(
|
||||
"The 'absoluteRuntime' option must be undefined, a boolean, or a string.",
|
||||
);
|
||||
}
|
||||
if (
|
||||
corejsVersion !== false &&
|
||||
(typeof corejsVersion !== "number" || corejsVersion !== 2) &&
|
||||
@ -71,7 +101,9 @@ export default declare((api, options) => {
|
||||
if (has(options, "moduleName")) {
|
||||
throw new Error(
|
||||
"The 'moduleName' option has been removed. @babel/transform-runtime " +
|
||||
"no longer supports arbitrary runtimes.",
|
||||
"no longer supports arbitrary runtimes. If you were using this to " +
|
||||
"set an absolute path for Babel's standard runtimes, please use the " +
|
||||
"'absoluteRuntime' option.",
|
||||
);
|
||||
}
|
||||
|
||||
@ -83,6 +115,14 @@ export default declare((api, options) => {
|
||||
|
||||
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
|
||||
|
||||
let modulePath = moduleName;
|
||||
if (absoluteRuntime !== false) {
|
||||
modulePath = resolveAbsoluteRuntime(
|
||||
moduleName,
|
||||
path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
pre(file) {
|
||||
if (useRuntimeHelpers) {
|
||||
@ -106,7 +146,7 @@ export default declare((api, options) => {
|
||||
isInteropHelper && !isModule(file.path) ? 4 : undefined;
|
||||
|
||||
return this.addDefaultImport(
|
||||
`${moduleName}/${helpersDir}/${name}`,
|
||||
`${modulePath}/${helpersDir}/${name}`,
|
||||
name,
|
||||
blockHoist,
|
||||
);
|
||||
@ -144,7 +184,7 @@ export default declare((api, options) => {
|
||||
if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/regenerator`,
|
||||
`${modulePath}/regenerator`,
|
||||
"regeneratorRuntime",
|
||||
),
|
||||
);
|
||||
@ -160,7 +200,7 @@ export default declare((api, options) => {
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
|
||||
`${modulePath}/core-js/${definitions.builtins[node.name]}`,
|
||||
node.name,
|
||||
),
|
||||
);
|
||||
@ -183,7 +223,7 @@ export default declare((api, options) => {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/get-iterator`,
|
||||
`${modulePath}/core-js/get-iterator`,
|
||||
"getIterator",
|
||||
),
|
||||
[callee.object],
|
||||
@ -201,7 +241,7 @@ export default declare((api, options) => {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/is-iterable`,
|
||||
`${modulePath}/core-js/is-iterable`,
|
||||
"isIterable",
|
||||
),
|
||||
[path.node.right],
|
||||
@ -243,7 +283,7 @@ export default declare((api, options) => {
|
||||
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/${methods[prop.name]}`,
|
||||
`${modulePath}/core-js/${methods[prop.name]}`,
|
||||
`${obj.name}$${prop.name}`,
|
||||
),
|
||||
);
|
||||
@ -262,7 +302,7 @@ export default declare((api, options) => {
|
||||
path.replaceWith(
|
||||
t.memberExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[obj.name]}`,
|
||||
`${modulePath}/core-js/${definitions.builtins[obj.name]}`,
|
||||
obj.name,
|
||||
),
|
||||
node.property,
|
||||
|
||||
1
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/input.js
vendored
Normal file
1
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
class Foo {}
|
||||
6
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/options.json
vendored
Normal file
6
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-classes",
|
||||
["transform-runtime", { "absoluteRuntime": "./subfolder" }]
|
||||
]
|
||||
}
|
||||
7
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js
vendored
Normal file
7
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/node_modules/@babel/runtime/helpers/classCallCheck");
|
||||
|
||||
let Foo = function Foo() {
|
||||
"use strict";
|
||||
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
1
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/input.js
vendored
Normal file
1
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
class Foo {}
|
||||
6
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/options.json
vendored
Normal file
6
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-classes",
|
||||
["transform-runtime", { "absoluteRuntime": true }]
|
||||
]
|
||||
}
|
||||
7
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js
vendored
Normal file
7
packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/node_modules/@babel/runtime/helpers/classCallCheck");
|
||||
|
||||
let Foo = function Foo() {
|
||||
"use strict";
|
||||
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user