From 2bfa2eb5cd08e92ddba0d71edf946e9556176b36 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 9 Nov 2017 12:03:33 -0800 Subject: [PATCH 1/2] Apply option defaults when transforming, not up front. --- .../babel-core/src/config/option-manager.js | 49 +-------------- .../src/transformation/file/file.js | 62 ++++++++++--------- .../babel-core/src/transformation/index.js | 7 +-- .../src/transformation/normalize-opts.js | 37 ++++++++--- packages/babel-core/test/option-manager.js | 11 ---- 5 files changed, 64 insertions(+), 102 deletions(-) diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index 6162b47df9..9f65f4762d 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -2,10 +2,8 @@ import * as context from "../index"; import Plugin, { validatePluginObject } from "./plugin"; -import defaults from "lodash/defaults"; import merge from "lodash/merge"; import buildConfigChain, { type ConfigItem } from "./build-config-chain"; -import path from "path"; import traverse from "@babel/traverse"; import clone from "lodash/clone"; import { makeWeakCache } from "./caching"; @@ -118,7 +116,7 @@ class OptionManager { throw e; } - const opts: Object = merge(createInitialOptions(), this.options); + const opts: Object = this.options; // Tack the passes onto the object itself so that, if this object is passed back to Babel a second time, // it will be in the right structure to not change behavior. @@ -129,39 +127,6 @@ class OptionManager { .map(plugins => ({ plugins })); opts.passPerPreset = opts.presets.length > 0; - if (opts.inputSourceMap) { - opts.sourceMaps = true; - } - - if (opts.moduleId) { - opts.moduleIds = true; - } - - defaults(opts, { - moduleRoot: opts.sourceRoot, - }); - - defaults(opts, { - sourceRoot: opts.moduleRoot, - }); - - defaults(opts, { - filenameRelative: opts.filename, - }); - - if (typeof opts.filenameRelative === "string") { - const basenameRelative = path.basename(opts.filenameRelative); - - if (path.extname(opts.filenameRelative) === ".mjs") { - opts.sourceType = "module"; - } - - defaults(opts, { - sourceFileName: basenameRelative, - sourceMapTarget: basenameRelative, - }); - } - return { options: opts, passes: this.passes, @@ -394,15 +359,3 @@ function chain(a, b) { } }; } - -function createInitialOptions() { - return { - sourceType: "module", - babelrc: true, - code: true, - ast: true, - comments: true, - compact: "auto", - highlightCode: true, - }; -} diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index f9fa1f2f5d..71186978a9 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -61,44 +61,46 @@ export default class File { } getModuleName(): ?string { - const opts = this.opts; - if (!opts.moduleIds) { - return null; - } + const { + filename, + filenameRelative = filename, + + moduleId, + moduleIds = !!moduleId, + + getModuleId, + + sourceRoot: sourceRootTmp, + moduleRoot = sourceRootTmp, + sourceRoot = moduleRoot, + } = this.opts; + + if (!moduleIds) return null; // moduleId is n/a if a `getModuleId()` is provided - if (opts.moduleId != null && !opts.getModuleId) { - return opts.moduleId; + if (moduleId != null && !getModuleId) { + return moduleId; } - let filenameRelative = opts.filenameRelative; - let moduleName = ""; + let moduleName = moduleRoot != null ? moduleRoot + "/" : ""; - if (opts.moduleRoot != null) { - moduleName = opts.moduleRoot + "/"; + if (filenameRelative) { + const sourceRootReplacer = + sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : ""; + + moduleName += filenameRelative + // remove sourceRoot from filename + .replace(sourceRootReplacer, "") + // remove extension + .replace(/\.(\w*?)$/, ""); } - if (!opts.filenameRelative) { - return moduleName + opts.filename.replace(/^\//, ""); - } - - if (opts.sourceRoot != null) { - // remove sourceRoot from filename - const sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "/?"); - filenameRelative = filenameRelative.replace(sourceRootRegEx, ""); - } - - // remove extension - filenameRelative = filenameRelative.replace(/\.(\w*?)$/, ""); - - moduleName += filenameRelative; - // normalize path separators moduleName = moduleName.replace(/\\/g, "/"); - if (opts.getModuleId) { + if (getModuleId) { // If return is falsy, assume they want us to use our generated default name - return opts.getModuleId(moduleName) || moduleName; + return getModuleId(moduleName) || moduleName; } else { return moduleName; } @@ -202,6 +204,8 @@ export default class File { } if (loc) { + const { highlightCode = true } = this.opts; + msg += "\n" + codeFrameColumns( @@ -212,9 +216,7 @@ export default class File { column: loc.column + 1, }, }, - { - highlightCode: this.opts.highlightCode, - }, + { highlightCode }, ); } diff --git a/packages/babel-core/src/transformation/index.js b/packages/babel-core/src/transformation/index.js index ecb2a30b12..ea8554857c 100644 --- a/packages/babel-core/src/transformation/index.js +++ b/packages/babel-core/src/transformation/index.js @@ -58,14 +58,13 @@ export function runSync( transformFile(file, config.passes); const opts = file.opts; - const { outputCode, outputMap } = opts.code - ? generateCode(config.passes, file) - : {}; + const { outputCode, outputMap } = + opts.code !== false ? generateCode(config.passes, file) : {}; return { metadata: file.metadata, options: opts, - ast: opts.ast ? file.ast : null, + ast: opts.ast !== false ? file.ast : null, code: outputCode === undefined ? null : outputCode, map: outputMap === undefined ? null : outputMap, }; diff --git a/packages/babel-core/src/transformation/normalize-opts.js b/packages/babel-core/src/transformation/normalize-opts.js index 5b08e4f9a2..d3577fe1f4 100644 --- a/packages/babel-core/src/transformation/normalize-opts.js +++ b/packages/babel-core/src/transformation/normalize-opts.js @@ -1,15 +1,34 @@ // @flow +import path from "path"; import type { ResolvedConfig } from "../config"; export default function normalizeOptions(config: ResolvedConfig): {} { + const { + filename, + filenameRelative = filename || "unknown", + sourceType = "module", + inputSourceMap, + sourceMaps = !!inputSourceMap, + + moduleRoot, + sourceRoot = moduleRoot, + + sourceFileName = filenameRelative, + sourceMapTarget = filenameRelative, + + comments = true, + compact = "auto", + } = config.options; + const opts = config.options; const options = Object.assign({}, opts, { parserOpts: Object.assign( { - sourceType: opts.sourceType, - sourceFileName: opts.filename || "unknown", + sourceType: + path.extname(filenameRelative) === ".mjs" ? "module" : sourceType, + sourceFileName: filename, plugins: [], }, opts.parserOpts, @@ -17,20 +36,20 @@ export default function normalizeOptions(config: ResolvedConfig): {} { generatorOpts: Object.assign( { // General generator flags. - filename: opts.filename || "unknown", + filename, auxiliaryCommentBefore: opts.auxiliaryCommentBefore, auxiliaryCommentAfter: opts.auxiliaryCommentAfter, retainLines: opts.retainLines, - comments: opts.comments, - compact: opts.compact, + comments, + compact, minified: opts.minified, concise: opts.concise, // Source-map generation flags. - sourceMaps: opts.sourceMaps, - sourceMapTarget: opts.sourceMapTarget || "unknown", - sourceRoot: opts.sourceRoot, - sourceFileName: opts.sourceFileName || "unknown", + sourceMaps, + sourceMapTarget, + sourceRoot, + sourceFileName, }, opts.generatorOpts, ), diff --git a/packages/babel-core/test/option-manager.js b/packages/babel-core/test/option-manager.js index f6429615f5..210c70dc84 100644 --- a/packages/babel-core/test/option-manager.js +++ b/packages/babel-core/test/option-manager.js @@ -44,17 +44,6 @@ describe("option-manager", () => { }); }); - describe("source type", function() { - it("should set module for .mjs extension", () => { - const config = manageOptions({ - sourceType: "script", - filename: "foo.mjs", - }); - - assert.equal(config.options.sourceType, "module"); - }); - }); - describe("presets", function() { function presetTest(name) { it(name, function() { From 4a5d5513708f36e8ed235e03338417b8ea814b80 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 9 Nov 2017 12:06:08 -0800 Subject: [PATCH 2/2] Set babelrc:false on config load for more consistent loading. --- packages/babel-core/src/config/option-manager.js | 1 + packages/babel-register/src/node.js | 15 +++++---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/babel-core/src/config/option-manager.js b/packages/babel-core/src/config/option-manager.js index 9f65f4762d..4d8cac0395 100644 --- a/packages/babel-core/src/config/option-manager.js +++ b/packages/babel-core/src/config/option-manager.js @@ -120,6 +120,7 @@ class OptionManager { // Tack the passes onto the object itself so that, if this object is passed back to Babel a second time, // it will be in the right structure to not change behavior. + opts.babelrc = false; opts.plugins = this.passes[0]; opts.presets = this.passes .slice(1) diff --git a/packages/babel-register/src/node.js b/packages/babel-register/src/node.js index f4715ff35d..21729f4fdb 100644 --- a/packages/babel-register/src/node.js +++ b/packages/babel-register/src/node.js @@ -63,16 +63,11 @@ function compile(code, filename) { } } - const result = babel.transform( - code, - Object.assign(opts, { - // Do not process config files since has already been done with the OptionManager - // calls above and would introduce duplicates. - babelrc: false, - sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps, - ast: false, - }), - ); + const result = babel.transform(code, { + ...opts, + sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps, + ast: false, + }); if (cache) { cache[cacheKey] = result;