automate option generation
This commit is contained in:
@@ -82,52 +82,7 @@ export default class File {
|
||||
"self-global"
|
||||
];
|
||||
|
||||
static validOptions = [
|
||||
"filename",
|
||||
"filenameRelative",
|
||||
|
||||
"blacklist",
|
||||
"whitelist",
|
||||
"optional",
|
||||
|
||||
"loose",
|
||||
"playground",
|
||||
"experimental",
|
||||
|
||||
"modules",
|
||||
"moduleIds",
|
||||
"moduleId",
|
||||
"resolveModuleSource",
|
||||
"keepModuleIdExtensions",
|
||||
|
||||
"code",
|
||||
"ast",
|
||||
|
||||
"comments",
|
||||
"compact",
|
||||
|
||||
"auxiliaryComment",
|
||||
"externalHelpers",
|
||||
"returnUsedHelpers",
|
||||
|
||||
"inputSourceMap",
|
||||
"sourceMap",
|
||||
"sourceMapName",
|
||||
"sourceFileName",
|
||||
"sourceRoot",
|
||||
"moduleRoot",
|
||||
|
||||
"ignore",
|
||||
"only",
|
||||
|
||||
// legacy
|
||||
"format",
|
||||
"reactCompat",
|
||||
|
||||
// these are used by plugins
|
||||
"extensions",
|
||||
"accept"
|
||||
];
|
||||
static options = require("./options");
|
||||
|
||||
normalizeOptions(opts: Object) {
|
||||
opts = assign({}, opts);
|
||||
@@ -136,36 +91,32 @@ export default class File {
|
||||
opts = resolveRc(opts.filename, opts);
|
||||
}
|
||||
|
||||
for (var key in opts) {
|
||||
if (key[0] !== "_" && File.validOptions.indexOf(key) < 0) {
|
||||
throw new ReferenceError(`Unknown option: ${key}`);
|
||||
}
|
||||
//
|
||||
|
||||
for (let key in opts) {
|
||||
if (key[0] === "_") continue;
|
||||
|
||||
let option = File.options[key];
|
||||
if (!option) throw new ReferenceError(`Unknown option: ${key}`);
|
||||
}
|
||||
|
||||
for (let key in File.options) {
|
||||
let option = File.options[key];
|
||||
|
||||
var val = opts[key];
|
||||
if (val == null) val = option.default || null;
|
||||
opts[key] = val;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
defaults(opts, {
|
||||
keepModuleIdExtensions: false,
|
||||
resolveModuleSource: null,
|
||||
returnUsedHelpers: false,
|
||||
externalHelpers: false,
|
||||
auxilaryComment: "",
|
||||
inputSourceMap: null,
|
||||
experimental: false,
|
||||
reactCompat: false,
|
||||
playground: false,
|
||||
moduleIds: false,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
optional: [],
|
||||
comments: true,
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
compact: "auto",
|
||||
loose: [],
|
||||
ignore: [],
|
||||
only: [],
|
||||
code: true,
|
||||
ast: true
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
optional: [],
|
||||
loose: [],
|
||||
ignore: [],
|
||||
only: [],
|
||||
});
|
||||
|
||||
if (opts.inputSourceMap) {
|
||||
@@ -213,9 +164,7 @@ export default class File {
|
||||
sourceMapName: opts.filenameRelative
|
||||
});
|
||||
|
||||
if (opts.playground) {
|
||||
opts.experimental = true;
|
||||
}
|
||||
//
|
||||
|
||||
if (opts.externalHelpers) {
|
||||
this.set("helpersNamespace", t.identifier("babelHelpers"));
|
||||
@@ -226,15 +175,6 @@ export default class File {
|
||||
opts.optional = transform._ensureTransformerNames("optional", opts.optional);
|
||||
opts.loose = transform._ensureTransformerNames("loose", opts.loose);
|
||||
|
||||
var ensureEnabled = function (key) {
|
||||
var namespace = transform.transformerNamespaces[key];
|
||||
if (namespace === "es7") opts.experimental = true;
|
||||
if (namespace === "playground") opts.playground = true;
|
||||
};
|
||||
|
||||
each(opts.whitelist, ensureEnabled);
|
||||
each(opts.optional, ensureEnabled);
|
||||
|
||||
return opts;
|
||||
};
|
||||
|
||||
@@ -242,6 +182,10 @@ export default class File {
|
||||
return includes(this.opts.loose, key);
|
||||
}
|
||||
|
||||
buildPlugins(stack) {
|
||||
|
||||
}
|
||||
|
||||
buildTransformers() {
|
||||
var file = this;
|
||||
|
||||
@@ -250,6 +194,8 @@ export default class File {
|
||||
var secondaryStack = [];
|
||||
var stack = [];
|
||||
|
||||
this.buildPlugins(stack);
|
||||
|
||||
each(transform.transformers, function (transformer, key) {
|
||||
var pass = transformers[key] = transformer.buildPass(file);
|
||||
|
||||
@@ -551,6 +497,7 @@ export default class File {
|
||||
}
|
||||
|
||||
generate(): {
|
||||
usedHelpers?: Array<string>;
|
||||
code: string;
|
||||
map?: Object;
|
||||
ast?: Object;
|
||||
@@ -560,8 +507,8 @@ export default class File {
|
||||
|
||||
var result = {
|
||||
code: "",
|
||||
map: null,
|
||||
ast: null
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
|
||||
if (this.opts.returnUsedHelpers) {
|
||||
|
||||
152
src/babel/transformation/options.json
Normal file
152
src/babel/transformation/options.json
Normal file
@@ -0,0 +1,152 @@
|
||||
{
|
||||
"filename": {
|
||||
"type": "string",
|
||||
"description": "Filename to use when reading from stdin - this will be used in source-maps, errors etc",
|
||||
"default": "unknown",
|
||||
"shorthand": "f"
|
||||
},
|
||||
|
||||
"filenameRelative": {
|
||||
"hidden": true,
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
"inputSourceMap": {
|
||||
"hidden": true
|
||||
},
|
||||
|
||||
"moduleId": {
|
||||
|
||||
},
|
||||
|
||||
"resolveModuleSource": {
|
||||
"hidden": true
|
||||
},
|
||||
|
||||
"experimental": {
|
||||
"description": "Enable all ES7+ transformers",
|
||||
"shorthand": "e",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
|
||||
"playground": {
|
||||
"description": "Enable all playground transformers",
|
||||
"shorthand": "p",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
|
||||
"blacklist": {
|
||||
"type": "transformerList",
|
||||
"description": "Blacklist of transformers to NOT use",
|
||||
"shorthand": "b"
|
||||
},
|
||||
|
||||
"whitelist": {
|
||||
"type": "transformerList",
|
||||
"description": "Whitelist of transformers to ONLY use",
|
||||
"shorthand": "l"
|
||||
},
|
||||
|
||||
"optional": {
|
||||
"type": "transformerList",
|
||||
"description": "List of optional transformers to enable"
|
||||
},
|
||||
|
||||
"modules": {
|
||||
"type": "string",
|
||||
"description": "Module formatter type to use [common]",
|
||||
"default": "common",
|
||||
"shorthand": "m"
|
||||
},
|
||||
|
||||
"moduleIds": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"shorthand": "M"
|
||||
},
|
||||
|
||||
"loose": {
|
||||
"type": "transformerList",
|
||||
"description": "List of transformers to enable loose mode ON",
|
||||
"shorthand": "L"
|
||||
},
|
||||
|
||||
"ignore": {
|
||||
"type": "list"
|
||||
},
|
||||
|
||||
"only": {
|
||||
"type": "list"
|
||||
},
|
||||
|
||||
"code": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"ast": {
|
||||
"hidden": true,
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
"comments": {
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
},
|
||||
|
||||
"compact": {
|
||||
"type": "string",
|
||||
"default": "auto"
|
||||
},
|
||||
|
||||
"keepModuleIdExtensions": {
|
||||
"type": "boolean",
|
||||
"description": "Keep extensions when generating module ids",
|
||||
"default": false,
|
||||
"shorthand": "k"
|
||||
},
|
||||
|
||||
"auxiliaryComment": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"shorthand": "a"
|
||||
},
|
||||
|
||||
"externalHelpers": {
|
||||
"type": "string",
|
||||
"default": false,
|
||||
"shorthand": "r"
|
||||
},
|
||||
|
||||
"returnUsedHelpers": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"hidden": true
|
||||
},
|
||||
|
||||
"sourceMap": {
|
||||
"type": "string",
|
||||
"default": false,
|
||||
"shorthand": "s"
|
||||
},
|
||||
|
||||
"sourceMapName": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
"sourceFileName": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
"sourceRoot": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
"moduleRoot": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
@@ -32,14 +32,14 @@ export default class TransformerPass {
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length) return includes(whitelist, key);
|
||||
|
||||
// optional
|
||||
if (transformer.optional && !includes(opts.optional, key)) return false;
|
||||
|
||||
// experimental
|
||||
if (transformer.experimental && !opts.experimental) return false;
|
||||
if (transformer.experimental && opts.experimental) return true;
|
||||
|
||||
// playground
|
||||
if (transformer.playground && !opts.playground) return false;
|
||||
if (transformer.playground && opts.playground) return true;
|
||||
|
||||
// optional
|
||||
if (transformer.optional && !includes(opts.optional, key)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user