Extract resolvePreset method to babel-core public API
This encapsulates the logic for turning an acceptable preset name into the absolute path for that preset. It can be used to preprocess a presets list to map each preset to its absolute path, which is necessary if `babel.transform` is going to be executed on a file outside the directory subtree where the presets are installed. This adds a getPossiblePresetNames helper encapsulating the logic for what preset names we should try to resolve, and the resolvePreset method just calls this helper and actually resolves them.
This commit is contained in:
parent
f4389a1886
commit
e24f07dfda
@ -6,6 +6,7 @@ export { default as options } from "../transformation/file/options/config";
|
|||||||
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
|
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
|
||||||
export { default as template } from "babel-template";
|
export { default as template } from "babel-template";
|
||||||
export { default as resolvePlugin } from "../helpers/resolve-plugin";
|
export { default as resolvePlugin } from "../helpers/resolve-plugin";
|
||||||
|
export { default as resolvePreset } from "../helpers/resolve-preset";
|
||||||
export { version } from "../../package";
|
export { version } from "../../package";
|
||||||
|
|
||||||
import * as util from "../util";
|
import * as util from "../util";
|
||||||
|
|||||||
13
packages/babel-core/src/helpers/get-possible-preset-names.js
Normal file
13
packages/babel-core/src/helpers/get-possible-preset-names.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
export default function getPossiblePresetNames(presetName: string): Array<string> {
|
||||||
|
let possibleNames = [`babel-preset-${presetName}`, presetName];
|
||||||
|
|
||||||
|
// trying to resolve @organization shortcat
|
||||||
|
// @foo/es2015 -> @foo/babel-preset-es2015
|
||||||
|
let matches = presetName.match(/^(@[^/]+)\/(.+)$/);
|
||||||
|
if (matches) {
|
||||||
|
let [, orgName, presetPath] = matches;
|
||||||
|
possibleNames.push(`${orgName}/babel-preset-${presetPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return possibleNames;
|
||||||
|
}
|
||||||
6
packages/babel-core/src/helpers/resolve-preset.js
Normal file
6
packages/babel-core/src/helpers/resolve-preset.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import resolve from "./resolve";
|
||||||
|
import getPossiblePresetNames from "./get-possible-preset-names";
|
||||||
|
|
||||||
|
export default function resolvePreset(presetName: string, dirname: string = process.cwd()): ?string {
|
||||||
|
return getPossiblePresetNames(presetName).reduce((accum, curr) => accum || resolve(curr, dirname), null);
|
||||||
|
}
|
||||||
@ -5,8 +5,8 @@ import type Logger from "../logger";
|
|||||||
import Plugin from "../../plugin";
|
import Plugin from "../../plugin";
|
||||||
import * as messages from "babel-messages";
|
import * as messages from "babel-messages";
|
||||||
import { normaliseOptions } from "./index";
|
import { normaliseOptions } from "./index";
|
||||||
import resolve from "../../../helpers/resolve";
|
|
||||||
import resolvePlugin from "../../../helpers/resolve-plugin";
|
import resolvePlugin from "../../../helpers/resolve-plugin";
|
||||||
|
import resolvePreset from "../../../helpers/resolve-preset";
|
||||||
import cloneDeepWith from "lodash/cloneDeepWith";
|
import cloneDeepWith from "lodash/cloneDeepWith";
|
||||||
import clone from "lodash/clone";
|
import clone from "lodash/clone";
|
||||||
import merge from "../../../helpers/merge";
|
import merge from "../../../helpers/merge";
|
||||||
@ -262,18 +262,7 @@ export default class OptionManager {
|
|||||||
let presetLoc;
|
let presetLoc;
|
||||||
try {
|
try {
|
||||||
if (typeof val === "string") {
|
if (typeof val === "string") {
|
||||||
presetLoc = resolve(`babel-preset-${val}`, dirname) || resolve(val, dirname);
|
presetLoc = resolvePreset(val, dirname);
|
||||||
|
|
||||||
// trying to resolve @organization shortcat
|
|
||||||
// @foo/es2015 -> @foo/babel-preset-es2015
|
|
||||||
if (!presetLoc) {
|
|
||||||
let matches = val.match(/^(@[^/]+)\/(.+)$/);
|
|
||||||
if (matches) {
|
|
||||||
let [, orgName, presetPath] = matches;
|
|
||||||
val = `${orgName}/babel-preset-${presetPath}`;
|
|
||||||
presetLoc = resolve(val, dirname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!presetLoc) {
|
if (!presetLoc) {
|
||||||
throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` +
|
throw new Error(`Couldn't find preset ${JSON.stringify(val)} relative to directory ` +
|
||||||
|
|||||||
@ -118,6 +118,10 @@ describe("api", function () {
|
|||||||
assert.equal(babel.resolvePlugin("nonexistent-plugin"), null);
|
assert.equal(babel.resolvePlugin("nonexistent-plugin"), null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("exposes the resolvePreset method", function() {
|
||||||
|
assert.equal(babel.resolvePreset("nonexistent-preset"), null);
|
||||||
|
});
|
||||||
|
|
||||||
it("transformFile", function (done) {
|
it("transformFile", function (done) {
|
||||||
babel.transformFile(__dirname + "/fixtures/api/file.js", {}, function (err, res) {
|
babel.transformFile(__dirname + "/fixtures/api/file.js", {}, function (err, res) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
@ -266,7 +270,7 @@ describe("api", function () {
|
|||||||
presets: ["@babel/es2015"]
|
presets: ["@babel/es2015"]
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/Couldn\'t find preset \"\@babel\/babel\-preset\-es2015\" relative to directory/
|
/Couldn\'t find preset \"\@babel\/es2015\" relative to directory/
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -277,7 +281,7 @@ describe("api", function () {
|
|||||||
presets: ["@babel/react/optimizations"]
|
presets: ["@babel/react/optimizations"]
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/Couldn\'t find preset \"\@babel\/babel\-preset\-react\/optimizations\" relative to directory/
|
/Couldn\'t find preset \"\@babel\/react\/optimizations\" relative to directory/
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
22
packages/babel-core/test/get-possible-preset-names.js
Normal file
22
packages/babel-core/test/get-possible-preset-names.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
let assert = require("assert");
|
||||||
|
let getPossiblePresetNames = require("../lib/helpers/get-possible-preset-names");
|
||||||
|
|
||||||
|
describe("getPossiblePresetNames", function () {
|
||||||
|
it("adds the babel-preset prefix", function() {
|
||||||
|
assert.deepEqual(getPossiblePresetNames("foobar"), ["babel-preset-foobar", "foobar"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("inserts babel-preset after @org/", function() {
|
||||||
|
assert.deepEqual(getPossiblePresetNames("@babel/es2015"), [
|
||||||
|
"babel-preset-@babel/es2015",
|
||||||
|
"@babel/es2015",
|
||||||
|
"@babel/babel-preset-es2015"
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(getPossiblePresetNames("@babel/react/optimizations"), [
|
||||||
|
"babel-preset-@babel/react/optimizations",
|
||||||
|
"@babel/react/optimizations",
|
||||||
|
"@babel/babel-preset-react/optimizations"
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user