Restrict Babel's plugins/presets to a single target. (#5547)
This commit is contained in:
parent
c59e9f5f0e
commit
2b86d353d6
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
node_modules
|
/node_modules
|
||||||
|
/packages/*/node_modules
|
||||||
*.log
|
*.log
|
||||||
*.cache
|
*.cache
|
||||||
/.eslintcache
|
/.eslintcache
|
||||||
|
|||||||
@ -5,25 +5,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import resolve from "resolve";
|
import resolve from "resolve";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export function resolvePlugin(pluginName: string, dirname: string): string|null {
|
const EXACT_RE = /^module:/;
|
||||||
const possibleNames = [`babel-plugin-${pluginName}`, pluginName];
|
const BABEL_PLUGIN_PREFIX_RE = /^(?!@|module:|[^/\/]+[/\/]|babel-plugin-)/;
|
||||||
|
const BABEL_PRESET_PREFIX_RE = /^(?!@|module:|[^/\/]+[/\/]|babel-preset-)/;
|
||||||
|
const BABEL_PLUGIN_ORG_RE = /^(@babel[/\/])(?!plugin-|[^/\/]+[/\/])/;
|
||||||
|
const BABEL_PRESET_ORG_RE = /^(@babel[/\/])(?!preset-|[^/\/]+[/\/])/;
|
||||||
|
const OTHER_PLUGIN_ORG_RE = /^(@(?!babel[/\/])[^/\/]+[/\/])(?!babel-plugin-|[^/\/]+[/\/])/;
|
||||||
|
const OTHER_PRESET_ORG_RE = /^(@(?!babel[/\/])[^/\/]+[/\/])(?!babel-preset-|[^/\/]+[/\/])/;
|
||||||
|
|
||||||
return resolveFromPossibleNames(possibleNames, dirname);
|
export function resolvePlugin(name: string, dirname: string): string|null {
|
||||||
|
return resolveStandardizedName("plugin", name, dirname);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolvePreset(presetName: string, dirname: string): string|null {
|
export function resolvePreset(name: string, dirname: string): string|null {
|
||||||
const possibleNames = [`babel-preset-${presetName}`, presetName];
|
return resolveStandardizedName("preset", name, dirname);
|
||||||
|
|
||||||
// trying to resolve @organization shortcat
|
|
||||||
// @foo/es2015 -> @foo/babel-preset-es2015
|
|
||||||
const matches = presetName.match(/^(@[^/]+)\/(.+)$/);
|
|
||||||
if (matches) {
|
|
||||||
const [, orgName, presetPath] = matches;
|
|
||||||
possibleNames.push(`${orgName}/babel-preset-${presetPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolveFromPossibleNames(possibleNames, dirname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadPlugin(name: string, dirname: string): { filepath: string, value: mixed } {
|
export function loadPlugin(name: string, dirname: string): { filepath: string, value: mixed } {
|
||||||
@ -47,8 +44,7 @@ export function loadPreset(name: string, dirname: string): { filepath: string, v
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function loadParser(name: string, dirname: string): { filepath: string, value: Function } {
|
export function loadParser(name: string, dirname: string): { filepath: string, value: Function } {
|
||||||
const filepath = resolveQuiet(name, dirname);
|
const filepath = resolve.sync(name, { basedir: dirname });
|
||||||
if (!filepath) throw new Error(`Parser ${name} not found relative to ${dirname}`);
|
|
||||||
|
|
||||||
const mod = requireModule(filepath);
|
const mod = requireModule(filepath);
|
||||||
|
|
||||||
@ -66,8 +62,7 @@ export function loadParser(name: string, dirname: string): { filepath: string, v
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function loadGenerator(name: string, dirname: string): { filepath: string, value: Function } {
|
export function loadGenerator(name: string, dirname: string): { filepath: string, value: Function } {
|
||||||
const filepath = resolveQuiet(name, dirname);
|
const filepath = resolve.sync(name, { basedir: dirname });
|
||||||
if (!filepath) throw new Error(`Generator ${name} not found relative to ${dirname}`);
|
|
||||||
|
|
||||||
const mod = requireModule(filepath);
|
const mod = requireModule(filepath);
|
||||||
|
|
||||||
@ -84,25 +79,69 @@ export function loadGenerator(name: string, dirname: string): { filepath: string
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveQuiet(name: string, dirname: string): string|null {
|
function standardizeName(type: "plugin"|"preset", name: string) {
|
||||||
try {
|
// Let absolute and relative paths through.
|
||||||
return resolve.sync(name, { basedir: dirname });
|
if (path.isAbsolute(name)) return name;
|
||||||
} catch (e) {
|
|
||||||
// The 'resolve' module can currently throw ENOTDIR
|
|
||||||
// https://github.com/substack/node-resolve/issues/121
|
|
||||||
if (e.code !== "MODULE_NOT_FOUND" && e.code !== "ENOTDIR") throw e;
|
|
||||||
|
|
||||||
// Silently fail and move to the next item.
|
const isPreset = type === "preset";
|
||||||
}
|
|
||||||
return null;
|
return name
|
||||||
|
// foo -> babel-preset-foo
|
||||||
|
.replace(isPreset ? BABEL_PRESET_PREFIX_RE : BABEL_PLUGIN_PREFIX_RE, `babel-${type}-`)
|
||||||
|
// @babel/es2015 -> @babel/preset-es2015
|
||||||
|
.replace(isPreset ? BABEL_PRESET_ORG_RE : BABEL_PLUGIN_ORG_RE, `$1${type}-`)
|
||||||
|
// @foo/mypreset -> @foo/babel-preset-mypreset
|
||||||
|
.replace(isPreset ? OTHER_PRESET_ORG_RE : OTHER_PLUGIN_ORG_RE, `$1babel-${type}-`)
|
||||||
|
// module:mypreset -> mypreset
|
||||||
|
.replace(EXACT_RE, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveFromPossibleNames(possibleNames: Array<string>, dirname: string): string|null {
|
function resolveStandardizedName(type: "plugin"|"preset", name: string, dirname: string = process.cwd()) {
|
||||||
for (const name of possibleNames) {
|
const standardizedName = standardizeName(type, name);
|
||||||
const result = resolveQuiet(name, dirname);
|
|
||||||
if (result !== null) return result;
|
try {
|
||||||
|
return resolve.sync(standardizedName, { basedir: dirname });
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code !== "MODULE_NOT_FOUND") throw e;
|
||||||
|
|
||||||
|
if (standardizedName !== name) {
|
||||||
|
let resolvedOriginal = false;
|
||||||
|
try {
|
||||||
|
resolve.sync(name, { basedir: dirname });
|
||||||
|
resolvedOriginal = true;
|
||||||
|
} catch (e2) { }
|
||||||
|
|
||||||
|
if (resolvedOriginal) {
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
e.message += `\n- If you want to resolve "${name}", use "module:${name}"`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let resolvedBabel = false;
|
||||||
|
try {
|
||||||
|
resolve.sync(standardizeName(type, "@babel/" + name), { basedir: dirname });
|
||||||
|
resolvedBabel = true;
|
||||||
|
} catch (e2) { }
|
||||||
|
|
||||||
|
if (resolvedBabel) {
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
e.message += `\n- Did you mean "@babel/${name}"?`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let resolvedOppositeType = false;
|
||||||
|
const oppositeType = type === "preset" ? "plugin" : "preset";
|
||||||
|
try {
|
||||||
|
resolve.sync(standardizeName(oppositeType, name), { basedir: dirname });
|
||||||
|
resolvedOppositeType = true;
|
||||||
|
} catch (e2) { }
|
||||||
|
|
||||||
|
if (resolvedOppositeType) {
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
e.message += `\n- Did you accidentally pass a ${type} as a ${oppositeType}?`;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireModule(name: string): mixed {
|
function requireModule(name: string): mixed {
|
||||||
|
|||||||
@ -116,11 +116,13 @@ describe("api", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("exposes the resolvePlugin method", function() {
|
it("exposes the resolvePlugin method", function() {
|
||||||
assert.equal(babel.resolvePlugin("nonexistent-plugin"), null);
|
assert.throws(() => babel.resolvePlugin("nonexistent-plugin"),
|
||||||
|
/Cannot find module 'babel-plugin-nonexistent-plugin'/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("exposes the resolvePreset method", function() {
|
it("exposes the resolvePreset method", function() {
|
||||||
assert.equal(babel.resolvePreset("nonexistent-preset"), null);
|
assert.throws(() => babel.resolvePreset("nonexistent-preset"),
|
||||||
|
/Cannot find module 'babel-preset-nonexistent-preset'/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("transformFile", function (done) {
|
it("transformFile", function (done) {
|
||||||
|
|||||||
0
packages/babel-core/test/fixtures/resolution/babel-org-paths/node_modules/@babel/plugin-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-org-paths/node_modules/@babel/plugin-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-org-paths/node_modules/@babel/preset-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-org-paths/node_modules/@babel/preset-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-scoped-nested-module-paths/node_modules/@babel/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-scoped-nested-module-paths/node_modules/@babel/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-scoped-nested-module-paths/node_modules/@babel/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/babel-scoped-nested-module-paths/node_modules/@babel/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/foo-org-paths/node_modules/@foo/babel-plugin-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/foo-org-paths/node_modules/@foo/babel-plugin-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/foo-org-paths/node_modules/@foo/babel-preset-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/foo-org-paths/node_modules/@foo/babel-preset-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/module-paths/node_modules/plugin/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/module-paths/node_modules/plugin/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/module-paths/node_modules/preset/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/module-paths/node_modules/preset/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/nested-module-paths/node_modules/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/nested-module-paths/node_modules/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/nested-module-paths/node_modules/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/nested-module-paths/node_modules/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/relative-paths/dir/plugin.js
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/relative-paths/dir/plugin.js
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/relative-paths/dir/preset.js
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/relative-paths/dir/preset.js
vendored
Normal file
@ -1 +0,0 @@
|
|||||||
!node_modules/
|
|
||||||
@ -1 +0,0 @@
|
|||||||
var x = "before";
|
|
||||||
@ -1 +0,0 @@
|
|||||||
var x = "AFTER";
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
module.exports = function () {
|
|
||||||
return {
|
|
||||||
visitor: {
|
|
||||||
StringLiteral: function (path) {
|
|
||||||
path.node.value = "after";
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
module.exports = function (context, options, fileContext) {
|
|
||||||
if (/resolve-addons-relative-to-file$/.test(fileContext.dirname)) {
|
|
||||||
return {
|
|
||||||
plugins: [plugin],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
};
|
|
||||||
|
|
||||||
function plugin () {
|
|
||||||
return {
|
|
||||||
visitor: {
|
|
||||||
StringLiteral: function (path) {
|
|
||||||
path.node.value =
|
|
||||||
path.node.value.toUpperCase();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
0
packages/babel-core/test/fixtures/resolution/scoped-nested-module-paths/node_modules/@foo/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/scoped-nested-module-paths/node_modules/@foo/mod/plugin.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/scoped-nested-module-paths/node_modules/@foo/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/scoped-nested-module-paths/node_modules/@foo/mod/preset.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/standard-paths/node_modules/babel-plugin-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/standard-paths/node_modules/babel-plugin-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/standard-paths/node_modules/babel-preset-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/standard-paths/node_modules/babel-preset-mod/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-babel-paths/node_modules/@babel/plugin-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-babel-paths/node_modules/@babel/plugin-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-babel-paths/node_modules/@babel/preset-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-babel-paths/node_modules/@babel/preset-foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-missing-paths/.gitignore
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-missing-paths/.gitignore
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-module-paths/node_modules/foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-module-paths/node_modules/foo/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-opposite-paths/node_modules/babel-plugin-testplugin/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-opposite-paths/node_modules/babel-plugin-testplugin/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-opposite-paths/node_modules/babel-preset-testpreset/index.js
generated
vendored
Normal file
0
packages/babel-core/test/fixtures/resolution/throw-opposite-paths/node_modules/babel-preset-testpreset/index.js
generated
vendored
Normal file
@ -1,56 +1,398 @@
|
|||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import async from "async";
|
|
||||||
import * as babel from "../lib/index";
|
import * as babel from "../lib/index";
|
||||||
import fs from "fs";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
// Test that plugins & presets are resolved relative to `filename`.
|
|
||||||
describe("addon resolution", function () {
|
describe("addon resolution", function () {
|
||||||
it("addon resolution", function (done) {
|
const base = path.join(__dirname, "fixtures", "resolution");
|
||||||
const fixtures = {};
|
|
||||||
const paths = {};
|
|
||||||
|
|
||||||
paths.fixtures = path.join(
|
beforeEach(function() {
|
||||||
__dirname,
|
this.cwd = process.cwd();
|
||||||
"fixtures",
|
process.chdir(base);
|
||||||
"resolution",
|
|
||||||
"resolve-addons-relative-to-file"
|
|
||||||
);
|
|
||||||
|
|
||||||
async.each(
|
|
||||||
["actual", "expected"],
|
|
||||||
function (key, mapDone) {
|
|
||||||
paths[key] = path.join(paths.fixtures, key + ".js");
|
|
||||||
fs.readFile(paths[key], { encoding: "utf8" }, function (err, data) {
|
|
||||||
if (err) return mapDone(err);
|
|
||||||
fixtures[key] = data.trim();
|
|
||||||
mapDone();
|
|
||||||
});
|
});
|
||||||
},
|
|
||||||
fixturesReady
|
|
||||||
);
|
|
||||||
|
|
||||||
function fixturesReady (err) {
|
afterEach(function() {
|
||||||
if (err) return done(err);
|
process.chdir(this.cwd);
|
||||||
|
});
|
||||||
|
|
||||||
const orignalCwd = process.cwd();
|
it("should find module: presets", function() {
|
||||||
try {
|
process.chdir("module-paths");
|
||||||
process.chdir(paths.fixtures);
|
|
||||||
|
|
||||||
const actual = babel.transform(fixtures.actual, {
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
filename: paths.actual,
|
presets: [
|
||||||
plugins: ["addons/plugin"],
|
"module:preset",
|
||||||
presets: ["addons/preset"],
|
],
|
||||||
}).code;
|
});
|
||||||
|
});
|
||||||
|
|
||||||
assert.equal(actual, fixtures.expected);
|
it("should find module: plugins", function() {
|
||||||
} finally {
|
process.chdir("module-paths");
|
||||||
process.chdir(orignalCwd);
|
|
||||||
}
|
|
||||||
|
|
||||||
done();
|
babel.transform("", {
|
||||||
}
|
filename: "filename.js",
|
||||||
// fixturesReady
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"module:plugin",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find standard presets", function() {
|
||||||
|
process.chdir("standard-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find standard plugins", function() {
|
||||||
|
process.chdir("standard-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find standard presets with an existing prefix", function() {
|
||||||
|
process.chdir("standard-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"babel-preset-mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find standard plugins with an existing prefix", function() {
|
||||||
|
process.chdir("standard-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"babel-plugin-mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped presets", function() {
|
||||||
|
process.chdir("babel-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@babel/foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped plugins", function() {
|
||||||
|
process.chdir("babel-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@babel/foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped presets with an existing prefix", function() {
|
||||||
|
process.chdir("babel-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@babel/preset-foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped plugins", function() {
|
||||||
|
process.chdir("babel-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@babel/plugin-foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped presets", function() {
|
||||||
|
process.chdir("foo-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@foo/mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped plugins", function() {
|
||||||
|
process.chdir("foo-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@foo/mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped presets with an existing prefix", function() {
|
||||||
|
process.chdir("foo-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@foo/babel-preset-mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped plugins with an existing prefix", function() {
|
||||||
|
process.chdir("foo-org-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@foo/babel-plugin-mod",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find relative path presets", function() {
|
||||||
|
process.chdir("relative-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"./dir/preset.js",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find relative path plugins", function() {
|
||||||
|
process.chdir("relative-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"./dir/plugin.js",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find module file presets", function() {
|
||||||
|
process.chdir("nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"mod/preset",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find module file plugins", function() {
|
||||||
|
process.chdir("nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"mod/plugin",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped module file presets", function() {
|
||||||
|
process.chdir("scoped-nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@foo/mod/preset",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @foo scoped module file plugins", function() {
|
||||||
|
process.chdir("scoped-nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@foo/mod/plugin",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped module file presets", function() {
|
||||||
|
process.chdir("babel-scoped-nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"@babel/mod/preset",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should find @babel scoped module file plugins", function() {
|
||||||
|
process.chdir("babel-scoped-nested-module-paths");
|
||||||
|
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"@babel/mod/plugin",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about module: usage for presets", function() {
|
||||||
|
process.chdir("throw-module-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about module: usage for plugins", function() {
|
||||||
|
process.chdir("throw-module-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about @babel usage for presets", function() {
|
||||||
|
process.chdir("throw-babel-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about @babel usage for plugins", function() {
|
||||||
|
process.chdir("throw-babel-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about passing a preset as a plugin", function() {
|
||||||
|
process.chdir("throw-opposite-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"testplugin",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-preset-testplugin'.*\n- Did you accidentally pass a preset as a plugin\?/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about passing a plugin as a preset", function() {
|
||||||
|
process.chdir("throw-opposite-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"testpreset",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
}, /Cannot find module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a plugin as a preset\?/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about missing presets", function() {
|
||||||
|
process.chdir("throw-missing-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
presets: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}, /Cannot find module 'babel-preset-foo'/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw about missing plugins", function() {
|
||||||
|
process.chdir("throw-missing-paths");
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
babel.transform("", {
|
||||||
|
filename: "filename.js",
|
||||||
|
babelrc: false,
|
||||||
|
plugins: [
|
||||||
|
"foo",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}, /Cannot find module 'babel-plugin-foo'/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user