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 { codeFrameColumns } from "@babel/code-frame";
|
||||||
import defaults from "lodash/defaults";
|
import defaults from "lodash/defaults";
|
||||||
import includes from "lodash/includes";
|
import includes from "lodash/includes";
|
||||||
|
import escapeRegExp from "lodash/escapeRegExp";
|
||||||
import * as helpers from "./helpers";
|
import * as helpers from "./helpers";
|
||||||
import extend from "lodash/extend";
|
import extend from "lodash/extend";
|
||||||
import merge from "lodash/merge";
|
import merge from "lodash/merge";
|
||||||
@ -180,7 +181,7 @@ function run(task) {
|
|||||||
function getOpts(self) {
|
function getOpts(self) {
|
||||||
const newOpts = merge(
|
const newOpts = merge(
|
||||||
{
|
{
|
||||||
cwd: path.dirname(self.filename),
|
cwd: path.dirname(self.loc),
|
||||||
filename: self.loc,
|
filename: self.loc,
|
||||||
filenameRelative: self.filename,
|
filenameRelative: self.filename,
|
||||||
sourceFileName: self.filename,
|
sourceFileName: self.filename,
|
||||||
@ -235,10 +236,15 @@ function run(task) {
|
|||||||
const expectCode = expected.code;
|
const expectCode = expected.code;
|
||||||
if (!execCode || actualCode) {
|
if (!execCode || actualCode) {
|
||||||
result = babel.transform(actualCode, getOpts(actual));
|
result = babel.transform(actualCode, getOpts(actual));
|
||||||
|
const expectedCode = result.code.replace(
|
||||||
|
escapeRegExp(path.resolve(__dirname, "../../../")),
|
||||||
|
"<CWD>",
|
||||||
|
);
|
||||||
|
|
||||||
checkDuplicatedNodes(result.ast);
|
checkDuplicatedNodes(result.ast);
|
||||||
if (
|
if (
|
||||||
!expected.code &&
|
!expected.code &&
|
||||||
result.code &&
|
expectedCode &&
|
||||||
!opts.throws &&
|
!opts.throws &&
|
||||||
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
|
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
|
||||||
!process.env.CI
|
!process.env.CI
|
||||||
@ -249,7 +255,7 @@ function run(task) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
console.log(`New test file created: ${expectedFile}`);
|
console.log(`New test file created: ${expectedFile}`);
|
||||||
fs.writeFileSync(expectedFile, `${result.code}\n`);
|
fs.writeFileSync(expectedFile, `${expectedCode}\n`);
|
||||||
|
|
||||||
if (expected.loc !== expectedFile) {
|
if (expected.loc !== expectedFile) {
|
||||||
try {
|
try {
|
||||||
@ -257,7 +263,7 @@ function run(task) {
|
|||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
actualCode = result.code.trim();
|
actualCode = expectedCode.trim();
|
||||||
try {
|
try {
|
||||||
expect(actualCode).toEqualFile({
|
expect(actualCode).toEqualFile({
|
||||||
filename: expected.loc,
|
filename: expected.loc,
|
||||||
@ -267,7 +273,7 @@ function run(task) {
|
|||||||
if (!process.env.OVERWRITE) throw e;
|
if (!process.env.OVERWRITE) throw e;
|
||||||
|
|
||||||
console.log(`Updated test file: ${expected.loc}`);
|
console.log(`Updated test file: ${expected.loc}`);
|
||||||
fs.writeFileSync(expected.loc, `${result.code}\n`);
|
fs.writeFileSync(expected.loc, `${expectedCode}\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actualCode) {
|
if (actualCode) {
|
||||||
|
|||||||
@ -10,7 +10,8 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/helper-module-imports": "7.0.0-rc.1",
|
"@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": {
|
"peerDependencies": {
|
||||||
"@babel/core": "^7.0.0-0"
|
"@babel/core": "^7.0.0-0"
|
||||||
@ -21,6 +22,7 @@
|
|||||||
"@babel/helpers": "7.0.0-rc.1",
|
"@babel/helpers": "7.0.0-rc.1",
|
||||||
"@babel/plugin-transform-runtime": "7.0.0-rc.1",
|
"@babel/plugin-transform-runtime": "7.0.0-rc.1",
|
||||||
"@babel/preset-env": "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/template": "7.0.0-rc.1",
|
||||||
"@babel/types": "7.0.0-beta.53"
|
"@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 { declare } from "@babel/helper-plugin-utils";
|
||||||
import { addDefault, isModule } from "@babel/helper-module-imports";
|
import { addDefault, isModule } from "@babel/helper-module-imports";
|
||||||
import { types as t } from "@babel/core";
|
import { types as t } from "@babel/core";
|
||||||
|
|
||||||
import definitions from "./definitions";
|
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);
|
api.assertVersion(7);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -13,6 +34,7 @@ export default declare((api, options) => {
|
|||||||
regenerator: useRuntimeRegenerator = true,
|
regenerator: useRuntimeRegenerator = true,
|
||||||
useESModules = false,
|
useESModules = false,
|
||||||
version: runtimeVersion = "7.0.0-beta.0",
|
version: runtimeVersion = "7.0.0-beta.0",
|
||||||
|
absoluteRuntime = false,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
if (typeof useRuntimeRegenerator !== "boolean") {
|
if (typeof useRuntimeRegenerator !== "boolean") {
|
||||||
@ -28,6 +50,14 @@ export default declare((api, options) => {
|
|||||||
"The 'useESModules' option must be undefined, or a boolean.",
|
"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 (
|
if (
|
||||||
corejsVersion !== false &&
|
corejsVersion !== false &&
|
||||||
(typeof corejsVersion !== "number" || corejsVersion !== 2) &&
|
(typeof corejsVersion !== "number" || corejsVersion !== 2) &&
|
||||||
@ -71,7 +101,9 @@ export default declare((api, options) => {
|
|||||||
if (has(options, "moduleName")) {
|
if (has(options, "moduleName")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"The 'moduleName' option has been removed. @babel/transform-runtime " +
|
"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"];
|
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
|
||||||
|
|
||||||
|
let modulePath = moduleName;
|
||||||
|
if (absoluteRuntime !== false) {
|
||||||
|
modulePath = resolveAbsoluteRuntime(
|
||||||
|
moduleName,
|
||||||
|
path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pre(file) {
|
pre(file) {
|
||||||
if (useRuntimeHelpers) {
|
if (useRuntimeHelpers) {
|
||||||
@ -106,7 +146,7 @@ export default declare((api, options) => {
|
|||||||
isInteropHelper && !isModule(file.path) ? 4 : undefined;
|
isInteropHelper && !isModule(file.path) ? 4 : undefined;
|
||||||
|
|
||||||
return this.addDefaultImport(
|
return this.addDefaultImport(
|
||||||
`${moduleName}/${helpersDir}/${name}`,
|
`${modulePath}/${helpersDir}/${name}`,
|
||||||
name,
|
name,
|
||||||
blockHoist,
|
blockHoist,
|
||||||
);
|
);
|
||||||
@ -144,7 +184,7 @@ export default declare((api, options) => {
|
|||||||
if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/regenerator`,
|
`${modulePath}/regenerator`,
|
||||||
"regeneratorRuntime",
|
"regeneratorRuntime",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -160,7 +200,7 @@ export default declare((api, options) => {
|
|||||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
|
`${modulePath}/core-js/${definitions.builtins[node.name]}`,
|
||||||
node.name,
|
node.name,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -183,7 +223,7 @@ export default declare((api, options) => {
|
|||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
t.callExpression(
|
t.callExpression(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/core-js/get-iterator`,
|
`${modulePath}/core-js/get-iterator`,
|
||||||
"getIterator",
|
"getIterator",
|
||||||
),
|
),
|
||||||
[callee.object],
|
[callee.object],
|
||||||
@ -201,7 +241,7 @@ export default declare((api, options) => {
|
|||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
t.callExpression(
|
t.callExpression(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/core-js/is-iterable`,
|
`${modulePath}/core-js/is-iterable`,
|
||||||
"isIterable",
|
"isIterable",
|
||||||
),
|
),
|
||||||
[path.node.right],
|
[path.node.right],
|
||||||
@ -243,7 +283,7 @@ export default declare((api, options) => {
|
|||||||
|
|
||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/core-js/${methods[prop.name]}`,
|
`${modulePath}/core-js/${methods[prop.name]}`,
|
||||||
`${obj.name}$${prop.name}`,
|
`${obj.name}$${prop.name}`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -262,7 +302,7 @@ export default declare((api, options) => {
|
|||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
t.memberExpression(
|
t.memberExpression(
|
||||||
this.addDefaultImport(
|
this.addDefaultImport(
|
||||||
`${moduleName}/core-js/${definitions.builtins[obj.name]}`,
|
`${modulePath}/core-js/${definitions.builtins[obj.name]}`,
|
||||||
obj.name,
|
obj.name,
|
||||||
),
|
),
|
||||||
node.property,
|
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