Merge pull request #3564 from jamestalmage/lazy-require-hook
Extract config file resolution from OptionsManager
This commit is contained in:
commit
210c3f7da9
@ -0,0 +1,179 @@
|
|||||||
|
|
||||||
|
import type Logger from "../logger";
|
||||||
|
import resolve from "../../../helpers/resolve";
|
||||||
|
import json5 from "json5";
|
||||||
|
import isAbsolute from "path-is-absolute";
|
||||||
|
import pathExists from "path-exists";
|
||||||
|
import path from "path";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
let existsCache = {};
|
||||||
|
let jsonCache = {};
|
||||||
|
|
||||||
|
const BABELIGNORE_FILENAME = ".babelignore";
|
||||||
|
const BABELRC_FILENAME = ".babelrc";
|
||||||
|
const PACKAGE_FILENAME = "package.json";
|
||||||
|
|
||||||
|
function exists(filename) {
|
||||||
|
let cached = existsCache[filename];
|
||||||
|
if (cached == null) {
|
||||||
|
return existsCache[filename] = pathExists.sync(filename);
|
||||||
|
} else {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function buildConfigChain(opts: Object = {}, log?: Logger) {
|
||||||
|
let filename = opts.filename;
|
||||||
|
let builder = new ConfigChainBuilder(log);
|
||||||
|
|
||||||
|
// resolve all .babelrc files
|
||||||
|
if (opts.babelrc !== false) {
|
||||||
|
builder.findConfigs(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.mergeConfig({
|
||||||
|
options: opts,
|
||||||
|
alias: "base",
|
||||||
|
dirname: filename && path.dirname(filename)
|
||||||
|
});
|
||||||
|
|
||||||
|
return builder.configs;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConfigChainBuilder {
|
||||||
|
constructor(log?: Logger) {
|
||||||
|
this.resolvedConfigs = [];
|
||||||
|
this.configs = [];
|
||||||
|
this.log = log;
|
||||||
|
}
|
||||||
|
|
||||||
|
findConfigs(loc) {
|
||||||
|
if (!loc) return;
|
||||||
|
|
||||||
|
if (!isAbsolute(loc)) {
|
||||||
|
loc = path.join(process.cwd(), loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
let foundConfig = false;
|
||||||
|
let foundIgnore = false;
|
||||||
|
|
||||||
|
while (loc !== (loc = path.dirname(loc))) {
|
||||||
|
if (!foundConfig) {
|
||||||
|
let configLoc = path.join(loc, BABELRC_FILENAME);
|
||||||
|
if (exists(configLoc)) {
|
||||||
|
this.addConfig(configLoc);
|
||||||
|
foundConfig = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pkgLoc = path.join(loc, PACKAGE_FILENAME);
|
||||||
|
if (!foundConfig && exists(pkgLoc)) {
|
||||||
|
foundConfig = this.addConfig(pkgLoc, "babel", JSON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!foundIgnore) {
|
||||||
|
let ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
||||||
|
if (exists(ignoreLoc)) {
|
||||||
|
this.addIgnoreConfig(ignoreLoc);
|
||||||
|
foundIgnore = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundIgnore && foundConfig) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addIgnoreConfig(loc) {
|
||||||
|
let file = fs.readFileSync(loc, "utf8");
|
||||||
|
let lines = file.split("\n");
|
||||||
|
|
||||||
|
lines = lines
|
||||||
|
.map((line) => line.replace(/#(.*?)$/, "").trim())
|
||||||
|
.filter((line) => !!line);
|
||||||
|
|
||||||
|
if (lines.length) {
|
||||||
|
this.mergeConfig({
|
||||||
|
options: { ignore: lines },
|
||||||
|
alias: loc,
|
||||||
|
dirname: path.dirname(loc)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addConfig(loc: string, key?: string, json = json5): boolean {
|
||||||
|
if (this.resolvedConfigs.indexOf(loc) >= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.resolvedConfigs.push(loc);
|
||||||
|
|
||||||
|
let content = fs.readFileSync(loc, "utf8");
|
||||||
|
let options;
|
||||||
|
|
||||||
|
try {
|
||||||
|
options = jsonCache[content] = jsonCache[content] || json.parse(content);
|
||||||
|
if (key) options = options[key];
|
||||||
|
} catch (err) {
|
||||||
|
err.message = `${loc}: Error while parsing JSON - ${err.message}`;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.mergeConfig({
|
||||||
|
options,
|
||||||
|
alias: loc,
|
||||||
|
dirname: path.dirname(loc)
|
||||||
|
});
|
||||||
|
|
||||||
|
return !!options;
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeConfig({
|
||||||
|
options,
|
||||||
|
alias,
|
||||||
|
loc,
|
||||||
|
dirname
|
||||||
|
}) {
|
||||||
|
if (!options) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
options = Object.assign({}, options);
|
||||||
|
|
||||||
|
dirname = dirname || process.cwd();
|
||||||
|
loc = loc || alias;
|
||||||
|
|
||||||
|
// add extends clause
|
||||||
|
if (options.extends) {
|
||||||
|
let extendsLoc = resolve(options.extends, dirname);
|
||||||
|
if (extendsLoc) {
|
||||||
|
this.addConfig(extendsLoc);
|
||||||
|
} else {
|
||||||
|
if (this.log) this.log.error(`Couldn't resolve extends clause of ${options.extends} in ${alias}`);
|
||||||
|
}
|
||||||
|
delete options.extends;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.configs.push({
|
||||||
|
options,
|
||||||
|
alias,
|
||||||
|
loc,
|
||||||
|
dirname
|
||||||
|
});
|
||||||
|
|
||||||
|
// env
|
||||||
|
let envOpts;
|
||||||
|
let envKey = process.env.BABEL_ENV || process.env.NODE_ENV || "development";
|
||||||
|
if (options.env) {
|
||||||
|
envOpts = options.env[envKey];
|
||||||
|
delete options.env;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.mergeConfig({
|
||||||
|
options: envOpts,
|
||||||
|
alias: `${alias}.env.${envKey}`,
|
||||||
|
dirname: dirname
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -6,32 +6,13 @@ 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 resolve from "../../../helpers/resolve";
|
||||||
import json5 from "json5";
|
|
||||||
import isAbsolute from "path-is-absolute";
|
|
||||||
import pathExists from "path-exists";
|
|
||||||
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";
|
||||||
import config from "./config";
|
import config from "./config";
|
||||||
import removed from "./removed";
|
import removed from "./removed";
|
||||||
|
import buildConfigChain from "./build-config-chain";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import fs from "fs";
|
|
||||||
|
|
||||||
let existsCache = {};
|
|
||||||
let jsonCache = {};
|
|
||||||
|
|
||||||
const BABELIGNORE_FILENAME = ".babelignore";
|
|
||||||
const BABELRC_FILENAME = ".babelrc";
|
|
||||||
const PACKAGE_FILENAME = "package.json";
|
|
||||||
|
|
||||||
function exists(filename) {
|
|
||||||
let cached = existsCache[filename];
|
|
||||||
if (cached == null) {
|
|
||||||
return existsCache[filename] = pathExists.sync(filename);
|
|
||||||
} else {
|
|
||||||
return cached;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type PluginObject = {
|
type PluginObject = {
|
||||||
pre?: Function;
|
pre?: Function;
|
||||||
@ -156,32 +137,6 @@ export default class OptionManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addConfig(loc: string, key?: string, json = json5): boolean {
|
|
||||||
if (this.resolvedConfigs.indexOf(loc) >= 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let content = fs.readFileSync(loc, "utf8");
|
|
||||||
let opts;
|
|
||||||
|
|
||||||
try {
|
|
||||||
opts = jsonCache[content] = jsonCache[content] || json.parse(content);
|
|
||||||
if (key) opts = opts[key];
|
|
||||||
} catch (err) {
|
|
||||||
err.message = `${loc}: Error while parsing JSON - ${err.message}`;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mergeOptions({
|
|
||||||
options: opts,
|
|
||||||
alias: loc,
|
|
||||||
dirname: path.dirname(loc)
|
|
||||||
});
|
|
||||||
this.resolvedConfigs.push(loc);
|
|
||||||
|
|
||||||
return !!opts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is called when we want to merge the input `opts` into the
|
* This is called when we want to merge the input `opts` into the
|
||||||
* base options (passed as the `extendingOpts`: at top-level it's the
|
* base options (passed as the `extendingOpts`: at top-level it's the
|
||||||
@ -241,17 +196,6 @@ export default class OptionManager {
|
|||||||
opts.plugins = OptionManager.normalisePlugins(loc, dirname, opts.plugins);
|
opts.plugins = OptionManager.normalisePlugins(loc, dirname, opts.plugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add extends clause
|
|
||||||
if (opts.extends) {
|
|
||||||
let extendsLoc = resolve(opts.extends, dirname);
|
|
||||||
if (extendsLoc) {
|
|
||||||
this.addConfig(extendsLoc);
|
|
||||||
} else {
|
|
||||||
if (this.log) this.log.error(`Couldn't resolve extends clause of ${opts.extends} in ${alias}`);
|
|
||||||
}
|
|
||||||
delete opts.extends;
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolve presets
|
// resolve presets
|
||||||
if (opts.presets) {
|
if (opts.presets) {
|
||||||
// If we're in the "pass per preset" mode, we resolve the presets
|
// If we're in the "pass per preset" mode, we resolve the presets
|
||||||
@ -273,14 +217,6 @@ export default class OptionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// env
|
|
||||||
let envOpts;
|
|
||||||
let envKey = process.env.BABEL_ENV || process.env.NODE_ENV || "development";
|
|
||||||
if (opts.env) {
|
|
||||||
envOpts = opts.env[envKey];
|
|
||||||
delete opts.env;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge them into current extending options in case of top-level
|
// Merge them into current extending options in case of top-level
|
||||||
// options. In case of presets, just re-assign options which are got
|
// options. In case of presets, just re-assign options which are got
|
||||||
// normalized during the `mergeOptions`.
|
// normalized during the `mergeOptions`.
|
||||||
@ -289,14 +225,6 @@ export default class OptionManager {
|
|||||||
} else {
|
} else {
|
||||||
merge(extendingOpts || this.options, opts);
|
merge(extendingOpts || this.options, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge in env options
|
|
||||||
this.mergeOptions({
|
|
||||||
options: envOpts,
|
|
||||||
extending: extendingOpts,
|
|
||||||
alias: `${alias}.env.${envKey}`,
|
|
||||||
dirname: dirname
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -338,56 +266,6 @@ export default class OptionManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addIgnoreConfig(loc) {
|
|
||||||
let file = fs.readFileSync(loc, "utf8");
|
|
||||||
let lines = file.split("\n");
|
|
||||||
|
|
||||||
lines = lines
|
|
||||||
.map((line) => line.replace(/#(.*?)$/, "").trim())
|
|
||||||
.filter((line) => !!line);
|
|
||||||
|
|
||||||
this.mergeOptions({
|
|
||||||
options: { ignore: lines },
|
|
||||||
loc
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
findConfigs(loc) {
|
|
||||||
if (!loc) return;
|
|
||||||
|
|
||||||
if (!isAbsolute(loc)) {
|
|
||||||
loc = path.join(process.cwd(), loc);
|
|
||||||
}
|
|
||||||
|
|
||||||
let foundConfig = false;
|
|
||||||
let foundIgnore = false;
|
|
||||||
|
|
||||||
while (loc !== (loc = path.dirname(loc))) {
|
|
||||||
if (!foundConfig) {
|
|
||||||
let configLoc = path.join(loc, BABELRC_FILENAME);
|
|
||||||
if (exists(configLoc)) {
|
|
||||||
this.addConfig(configLoc);
|
|
||||||
foundConfig = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let pkgLoc = path.join(loc, PACKAGE_FILENAME);
|
|
||||||
if (!foundConfig && exists(pkgLoc)) {
|
|
||||||
foundConfig = this.addConfig(pkgLoc, "babel", JSON);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!foundIgnore) {
|
|
||||||
let ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
|
||||||
if (exists(ignoreLoc)) {
|
|
||||||
this.addIgnoreConfig(ignoreLoc);
|
|
||||||
foundIgnore = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (foundIgnore && foundConfig) return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
normaliseOptions() {
|
normaliseOptions() {
|
||||||
let opts = this.options;
|
let opts = this.options;
|
||||||
|
|
||||||
@ -408,20 +286,10 @@ export default class OptionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init(opts: Object = {}): Object {
|
init(opts: Object = {}): Object {
|
||||||
let filename = opts.filename;
|
for (let config of buildConfigChain(opts, this.log)) {
|
||||||
|
this.mergeOptions(config);
|
||||||
// resolve all .babelrc files
|
|
||||||
if (opts.babelrc !== false) {
|
|
||||||
this.findConfigs(filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge in base options
|
|
||||||
this.mergeOptions({
|
|
||||||
options: opts,
|
|
||||||
alias: "base",
|
|
||||||
dirname: filename && path.dirname(filename)
|
|
||||||
});
|
|
||||||
|
|
||||||
// normalise
|
// normalise
|
||||||
this.normaliseOptions(opts);
|
this.normaliseOptions(opts);
|
||||||
|
|
||||||
|
|||||||
297
packages/babel-core/test/config-chain.js
Normal file
297
packages/babel-core/test/config-chain.js
Normal file
@ -0,0 +1,297 @@
|
|||||||
|
var assert = require("assert");
|
||||||
|
var path = require("path");
|
||||||
|
var buildConfigChain = require("../lib/transformation/file/options/build-config-chain");
|
||||||
|
|
||||||
|
function fixture() {
|
||||||
|
var args = [__dirname, "fixtures", "config"];
|
||||||
|
for (var i = 0; i < arguments.length; i ++) {
|
||||||
|
args.push(arguments[i]);
|
||||||
|
}
|
||||||
|
return path.join.apply(path, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
suite("buildConfigChain", function () {
|
||||||
|
var oldBabelEnv;
|
||||||
|
var oldNodeEnv;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
oldBabelEnv = process.env.BABEL_ENV;
|
||||||
|
oldNodeEnv = process.env.NODE_ENV;
|
||||||
|
|
||||||
|
delete process.env.BABEL_ENV;
|
||||||
|
delete process.env.NODE_ENV;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
process.env.BABEL_ENV = oldBabelEnv;
|
||||||
|
process.env.NODE_ENV = oldNodeEnv;
|
||||||
|
});
|
||||||
|
|
||||||
|
test("dir1", function () {
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("dir1", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"extended"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("extended.babelrc.json"),
|
||||||
|
loc: fixture("extended.babelrc.json"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"root"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelrc"),
|
||||||
|
loc: fixture(".babelrc"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: [
|
||||||
|
"root-ignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelignore"),
|
||||||
|
loc: fixture(".babelignore"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("dir1", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("dir1")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("dir2", function () {
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("dir2", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"dir2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("dir2", ".babelrc"),
|
||||||
|
loc: fixture("dir2", ".babelrc"),
|
||||||
|
dirname: fixture("dir2")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: [
|
||||||
|
"root-ignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelignore"),
|
||||||
|
loc: fixture(".babelignore"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("dir2", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("dir2")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("env - base", function () {
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"env-base"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("env", ".babelrc"),
|
||||||
|
loc: fixture("env", ".babelrc"),
|
||||||
|
dirname: fixture("env")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: [
|
||||||
|
"root-ignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelignore"),
|
||||||
|
loc: fixture(".babelignore"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("env")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("env - foo", function () {
|
||||||
|
process.env.NODE_ENV = "foo";
|
||||||
|
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"env-base"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("env", ".babelrc"),
|
||||||
|
loc: fixture("env", ".babelrc"),
|
||||||
|
dirname: fixture("env")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"env-foo"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("env", ".babelrc.env.foo"),
|
||||||
|
loc: fixture("env", ".babelrc.env.foo"),
|
||||||
|
dirname: fixture("env")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: [
|
||||||
|
"root-ignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelignore"),
|
||||||
|
loc: fixture(".babelignore"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("env")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("env - bar", function () {
|
||||||
|
process.env.NODE_ENV = "foo"; // overridden
|
||||||
|
process.env.NODE_ENV = "bar";
|
||||||
|
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"env-base"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("env", ".babelrc"),
|
||||||
|
loc: fixture("env", ".babelrc"),
|
||||||
|
dirname: fixture("env")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: [
|
||||||
|
"env-bar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture("env", ".babelrc.env.bar"),
|
||||||
|
loc: fixture("env", ".babelrc.env.bar"),
|
||||||
|
dirname: fixture("env")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: [
|
||||||
|
"root-ignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
alias: fixture(".babelignore"),
|
||||||
|
loc: fixture(".babelignore"),
|
||||||
|
dirname: fixture()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("env", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("env")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("env - foo", function () {
|
||||||
|
process.env.NODE_ENV = "foo";
|
||||||
|
|
||||||
|
var chain = buildConfigChain({
|
||||||
|
filename: fixture("pkg", "src.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
var expected = [
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
plugins: ["pkg-plugin"]
|
||||||
|
},
|
||||||
|
alias: fixture("pkg", "package.json"),
|
||||||
|
loc: fixture("pkg", "package.json"),
|
||||||
|
dirname: fixture("pkg")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
ignore: ["pkg-ignore"]
|
||||||
|
},
|
||||||
|
alias: fixture("pkg", ".babelignore"),
|
||||||
|
loc: fixture("pkg", ".babelignore"),
|
||||||
|
dirname: fixture("pkg")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
options: {
|
||||||
|
filename: fixture("pkg", "src.js")
|
||||||
|
},
|
||||||
|
alias: "base",
|
||||||
|
loc: "base",
|
||||||
|
dirname: fixture("pkg")
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(chain, expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
1
packages/babel-core/test/fixtures/config/.babelignore
vendored
Normal file
1
packages/babel-core/test/fixtures/config/.babelignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
root-ignore
|
||||||
4
packages/babel-core/test/fixtures/config/.babelrc
vendored
Normal file
4
packages/babel-core/test/fixtures/config/.babelrc
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["root"],
|
||||||
|
"extends": "./extended.babelrc.json"
|
||||||
|
}
|
||||||
1
packages/babel-core/test/fixtures/config/dir1/src.js
vendored
Normal file
1
packages/babel-core/test/fixtures/config/dir1/src.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
// empty
|
||||||
5
packages/babel-core/test/fixtures/config/dir2/.babelrc
vendored
Normal file
5
packages/babel-core/test/fixtures/config/dir2/.babelrc
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"dir2"
|
||||||
|
]
|
||||||
|
}
|
||||||
1
packages/babel-core/test/fixtures/config/dir2/src.js
vendored
Normal file
1
packages/babel-core/test/fixtures/config/dir2/src.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
// empty
|
||||||
11
packages/babel-core/test/fixtures/config/env/.babelrc
vendored
Normal file
11
packages/babel-core/test/fixtures/config/env/.babelrc
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["env-base"],
|
||||||
|
"env": {
|
||||||
|
"foo": {
|
||||||
|
"plugins": ["env-foo"]
|
||||||
|
},
|
||||||
|
"bar": {
|
||||||
|
"plugins": ["env-bar"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-core/test/fixtures/config/env/src.js
vendored
Normal file
1
packages/babel-core/test/fixtures/config/env/src.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
// empty
|
||||||
5
packages/babel-core/test/fixtures/config/extended.babelrc.json
vendored
Normal file
5
packages/babel-core/test/fixtures/config/extended.babelrc.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"extended"
|
||||||
|
]
|
||||||
|
}
|
||||||
1
packages/babel-core/test/fixtures/config/pkg/.babelignore
vendored
Normal file
1
packages/babel-core/test/fixtures/config/pkg/.babelignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
pkg-ignore
|
||||||
7
packages/babel-core/test/fixtures/config/pkg/package.json
vendored
Normal file
7
packages/babel-core/test/fixtures/config/pkg/package.json
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"name": "application-name",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"babel": {
|
||||||
|
"plugins": ["pkg-plugin"]
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-core/test/fixtures/config/pkg/src.js
vendored
Normal file
1
packages/babel-core/test/fixtures/config/pkg/src.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
// empty
|
||||||
Loading…
x
Reference in New Issue
Block a user