Remove config.js file in favor of config code.

This commit is contained in:
Logan Smyth 2017-03-13 00:37:24 -07:00
parent 4f72232ca9
commit 5b50b73d8b
3 changed files with 64 additions and 161 deletions

View File

@ -573,7 +573,7 @@ export default class File extends Store {
if (!opts.code) return this.makeResult(result);
let gen = generate;
if (opts.generatorOpts.generator) {
if (opts.generatorOpts && opts.generatorOpts.generator) {
gen = opts.generatorOpts.generator;
if (typeof gen === "string") {

View File

@ -1,125 +0,0 @@
/* eslint max-len: "off" */
export default {
filename: {
default: "unknown",
},
filenameRelative: {},
inputSourceMap: {},
env: {
default: {},
},
mode: {},
retainLines: {
default: false,
},
highlightCode: {
default: true,
},
suppressDeprecationMessages: {
default: false,
},
presets: {
default: [],
},
plugins: {
default: [],
},
ignore: {
default: [],
},
only: {},
code: {
default: true,
},
metadata: {
default: true,
},
ast: {
default: true,
},
extends: {},
comments: {
default: true,
},
shouldPrintComment: {},
wrapPluginVisitorMethod: {},
compact: {
default: "auto",
},
minified: {
default: false,
},
sourceMap: {
alias: "sourceMaps",
},
sourceMaps: {
default: false,
},
sourceMapTarget: {},
sourceFileName: {},
sourceRoot: {},
babelrc: {
default: true,
},
sourceType: {
default: "module",
},
auxiliaryCommentBefore: {},
auxiliaryCommentAfter: {},
resolveModuleSource: {},
getModuleId: {},
moduleRoot: {},
moduleIds: {
default: false,
},
moduleId: {},
passPerPreset: {
default: false,
},
// Deprecate top level parserOpts
parserOpts: {
default: false,
},
// Deprecate top level generatorOpts
generatorOpts: {
default: false,
},
};

View File

@ -4,9 +4,7 @@ import * as messages from "babel-messages";
import resolvePlugin from "../../../helpers/resolve-plugin";
import resolvePreset from "../../../helpers/resolve-preset";
import cloneDeepWith from "lodash/cloneDeepWith";
import clone from "lodash/clone";
import merge from "../../../helpers/merge";
import config from "./config";
import removed from "./removed";
import buildConfigChain from "./build-config-chain";
import path from "path";
@ -33,6 +31,48 @@ type MergeOptions = {
dirname?: string
};
const optionNames = new Set([
"filename",
"filenameRelative",
"inputSourceMap",
"env",
"mode",
"retainLines",
"highlightCode",
"suppressDeprecationMessages",
"presets",
"plugins",
"ignore",
"only",
"code",
"metadata",
"ast",
"extends",
"comments",
"shouldPrintComment",
"wrapPluginVisitorMethod",
"compact",
"minified",
"sourceMaps",
"sourceMapTarget",
"sourceFileName",
"sourceRoot",
"babelrc",
"sourceType",
"auxiliaryCommentBefore",
"auxiliaryCommentAfter",
"resolveModuleSource",
"getModuleId",
"moduleRoot",
"moduleIds",
"moduleId",
"passPerPreset",
// Deprecate top level parserOpts
"parserOpts",
// Deprecate top level generatorOpts
"generatorOpts",
]);
export default class OptionManager {
constructor() {
this.resolvedConfigs = [];
@ -73,14 +113,17 @@ export default class OptionManager {
}
static createBareOptions() {
const opts = {};
for (const key in config) {
const opt = config[key];
opts[key] = clone(opt.default);
}
return opts;
return {
sourceType: "module",
babelrc: true,
filename: "unknown",
code: true,
metadata: true,
ast: true,
comments: true,
compact: "auto",
highlightCode: true,
};
}
static normalisePlugin(plugin, loc, i, alias) {
@ -169,11 +212,18 @@ export default class OptionManager {
dirname = dirname || process.cwd();
loc = loc || alias;
for (const key in opts) {
const option = config[key];
if (opts.sourceMap !== undefined) {
if (opts.sourceMaps !== undefined) {
throw new Error(`Both ${alias}.sourceMap and .sourceMaps have been set`);
}
opts.sourceMaps = opts.sourceMap;
delete opts.sourceMap;
}
for (const key in opts) {
// check for an unknown option
if (!option) {
if (!optionNames.has(key)) {
if (removed[key]) {
throw new ReferenceError(`Using removed Babel 5 option: ${alias}.${key} - ${removed[key].message}`);
} else {
@ -321,33 +371,11 @@ export default class OptionManager {
return presetFactory(context, options, meta);
}
normaliseOptions() {
const opts = this.options;
for (const key in config) {
const option = config[key];
const val = opts[key];
// optional
if (!val && option.optional) continue;
// aliases
if (option.alias) {
opts[option.alias] = opts[option.alias] || val;
} else {
opts[key] = val;
}
}
}
init(opts: Object = {}): Object {
for (const config of buildConfigChain(opts)) {
this.mergeOptions(config);
}
// normalise
this.normaliseOptions(opts);
return this.options;
}
}