diff --git a/packages/babel-core/src/transformation/normalize-opts.js b/packages/babel-core/src/transformation/normalize-opts.js index 4c53b7aa3517..d4a91d83acc6 100644 --- a/packages/babel-core/src/transformation/normalize-opts.js +++ b/packages/babel-core/src/transformation/normalize-opts.js @@ -3,21 +3,48 @@ import path from "path"; import type { ResolvedConfig } from "../config"; +function normalizePath(filepath: string) { + return filepath && filepath.replace(/\\/g, "/"); +} + +function pickMap(obj, keys, fn) { + const res = {}; + for (const k of keys) res[k] = fn(obj[k]); + return res; +} + export default function normalizeOptions(config: ResolvedConfig): {} { + const normalizedPaths = pickMap( + config.options, + [ + "filename", + "cwd", + "filenameRelative", + "moduleRoot", + "sourceRoot", + "sourceFileName", + ], + normalizePath, + ); + const { filename, cwd, + filenameRelative = typeof filename === "string" - ? path.relative(cwd, filename) + ? path.posix.relative(cwd, filename) : "unknown", - sourceType = "module", - inputSourceMap, - sourceMaps = !!inputSourceMap, moduleRoot, sourceRoot = moduleRoot, sourceFileName = path.basename(filenameRelative), + } = normalizedPaths; + + const { + sourceType = "module", + inputSourceMap, + sourceMaps = !!inputSourceMap, comments = true, compact = "auto", @@ -27,6 +54,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { const options = { ...opts, + ...normalizedPaths, parserOpts: { sourceType: @@ -39,7 +67,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { generatorOpts: { // General generator flags. - filename, + filename: filename, auxiliaryCommentBefore: opts.auxiliaryCommentBefore, auxiliaryCommentAfter: opts.auxiliaryCommentAfter,