Merge pull request #6777 from loganfsmyth/options-simplified
Add an official 'state.filename' and be more explicit about option passing.
This commit is contained in:
@@ -163,17 +163,19 @@ class OptionManager {
|
||||
filenameRelative: opts.filename,
|
||||
});
|
||||
|
||||
const basenameRelative = path.basename(opts.filenameRelative);
|
||||
if (typeof opts.filenameRelative === "string") {
|
||||
const basenameRelative = path.basename(opts.filenameRelative);
|
||||
|
||||
if (path.extname(opts.filenameRelative) === ".mjs") {
|
||||
opts.sourceType = "module";
|
||||
if (path.extname(opts.filenameRelative) === ".mjs") {
|
||||
opts.sourceType = "module";
|
||||
}
|
||||
|
||||
defaults(opts, {
|
||||
sourceFileName: basenameRelative,
|
||||
sourceMapTarget: basenameRelative,
|
||||
});
|
||||
}
|
||||
|
||||
defaults(opts, {
|
||||
sourceFileName: basenameRelative,
|
||||
sourceMapTarget: basenameRelative,
|
||||
});
|
||||
|
||||
return {
|
||||
options: opts,
|
||||
passes: this.passes,
|
||||
@@ -461,7 +463,6 @@ function createInitialOptions() {
|
||||
return {
|
||||
sourceType: "module",
|
||||
babelrc: true,
|
||||
filename: "unknown",
|
||||
code: true,
|
||||
ast: true,
|
||||
comments: true,
|
||||
|
||||
@@ -212,7 +212,9 @@ export default class File {
|
||||
column: loc.column + 1,
|
||||
},
|
||||
},
|
||||
this.opts,
|
||||
{
|
||||
highlightCode: this.opts.highlightCode,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,7 @@ export default function generateCode(
|
||||
gen = opts.generatorOpts.generator;
|
||||
}
|
||||
|
||||
let { code: outputCode, map: outputMap } = gen(
|
||||
ast,
|
||||
opts.generatorOpts ? Object.assign(opts, opts.generatorOpts) : opts,
|
||||
code,
|
||||
);
|
||||
let { code: outputCode, map: outputMap } = gen(ast, opts.generatorOpts, code);
|
||||
|
||||
if (shebang) {
|
||||
// add back shebang
|
||||
|
||||
@@ -70,7 +70,7 @@ function parser(options, code) {
|
||||
if (loc) {
|
||||
err.loc = null;
|
||||
err.message =
|
||||
`${options.filename}: ${err.message}\n` +
|
||||
`${options.filename || "unknown"}: ${err.message}\n` +
|
||||
codeFrameColumns(
|
||||
code,
|
||||
{
|
||||
|
||||
@@ -3,19 +3,41 @@
|
||||
import type { ResolvedConfig } from "../config";
|
||||
|
||||
export default function normalizeOptions(config: ResolvedConfig): {} {
|
||||
const options = Object.assign({}, config.options, {
|
||||
const opts = config.options;
|
||||
|
||||
const options = Object.assign({}, opts, {
|
||||
parserOpts: Object.assign(
|
||||
{
|
||||
sourceType: config.options.sourceType,
|
||||
sourceFileName: config.options.filename,
|
||||
sourceType: opts.sourceType,
|
||||
sourceFileName: opts.filename || "unknown",
|
||||
plugins: [],
|
||||
},
|
||||
config.options.parserOpts,
|
||||
opts.parserOpts,
|
||||
),
|
||||
generatorOpts: Object.assign(
|
||||
{
|
||||
// General generator flags.
|
||||
filename: opts.filename || "unknown",
|
||||
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
|
||||
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
|
||||
retainLines: opts.retainLines,
|
||||
comments: opts.comments,
|
||||
compact: opts.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",
|
||||
},
|
||||
opts.generatorOpts,
|
||||
),
|
||||
});
|
||||
|
||||
for (const pluginPairs of config.passes) {
|
||||
for (const plugin of pluginPairs) {
|
||||
for (const plugins of config.passes) {
|
||||
for (const plugin of plugins) {
|
||||
if (plugin.manipulateOptions) {
|
||||
plugin.manipulateOptions(options, options.parserOpts);
|
||||
}
|
||||
|
||||
@@ -7,11 +7,14 @@ export default class PluginPass {
|
||||
key: ?string;
|
||||
file: File;
|
||||
opts: Object;
|
||||
filename: string | void;
|
||||
|
||||
constructor(file: File, key: ?string, options: ?Object) {
|
||||
this.key = key;
|
||||
this.file = file;
|
||||
this.opts = options || {};
|
||||
this.filename =
|
||||
typeof file.opts.filename === "string" ? file.opts.filename : undefined;
|
||||
}
|
||||
|
||||
set(key: mixed, val: mixed) {
|
||||
|
||||
@@ -44,10 +44,15 @@ export default function({ types: t }, options) {
|
||||
/**
|
||||
* Build the assignment statements that initialize the UMD global.
|
||||
*/
|
||||
function buildBrowserInit(browserGlobals, exactGlobals, file, moduleName) {
|
||||
function buildBrowserInit(
|
||||
browserGlobals,
|
||||
exactGlobals,
|
||||
filename,
|
||||
moduleName,
|
||||
) {
|
||||
const moduleNameOrBasename = moduleName
|
||||
? moduleName.value
|
||||
: basename(file.opts.filename, extname(file.opts.filename));
|
||||
: basename(filename, extname(filename));
|
||||
let globalToAssign = t.memberExpression(
|
||||
t.identifier("global"),
|
||||
t.identifier(t.toIdentifier(moduleNameOrBasename)),
|
||||
@@ -204,7 +209,7 @@ export default function({ types: t }, options) {
|
||||
GLOBAL_TO_ASSIGN: buildBrowserInit(
|
||||
browserGlobals,
|
||||
exactGlobals,
|
||||
this.file,
|
||||
this.filename || "unknown",
|
||||
moduleName,
|
||||
),
|
||||
}),
|
||||
|
||||
@@ -52,14 +52,13 @@ export default function({ types: t }) {
|
||||
visitor: {
|
||||
ExportDefaultDeclaration({ node }, state) {
|
||||
if (isCreateClass(node.declaration)) {
|
||||
let displayName = path.basename(
|
||||
state.file.opts.filename,
|
||||
path.extname(state.file.opts.filename),
|
||||
);
|
||||
const filename = state.filename || "unknown";
|
||||
|
||||
let displayName = path.basename(filename, path.extname(filename));
|
||||
|
||||
// ./{module name}/index.js
|
||||
if (displayName === "index") {
|
||||
displayName = path.basename(path.dirname(state.file.opts.filename));
|
||||
displayName = path.basename(path.dirname(filename));
|
||||
}
|
||||
|
||||
addDisplayName(displayName, node.declaration);
|
||||
|
||||
@@ -50,7 +50,7 @@ export default function({ types: t }) {
|
||||
}
|
||||
|
||||
if (!state.fileNameIdentifier) {
|
||||
const fileName = state.file.opts.filename || "";
|
||||
const fileName = state.filename || "";
|
||||
|
||||
const fileNameIdentifier = path.scope.generateUidIdentifier(
|
||||
FILE_NAME_VAR,
|
||||
|
||||
Reference in New Issue
Block a user