diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 571d1705530d..50bb169a263b 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -2,6 +2,13 @@ import path from "path"; import type { ResolvedConfig } from "../config/index.ts"; export default function normalizeOptions(config: ResolvedConfig): {} { + // TODO: Everything in this function is basically typed as `any`. Improve it. + + // TODO(@nicolo-ribaudo): Currently, soure map's `sources` is generated taking + // into account both the options passed to the parser and the options passed + // to the generator. If they disagree, both are included. Clean this up, so + // that there is a single source of thruth. + const { filename, cwd, @@ -15,7 +22,9 @@ export default function normalizeOptions(config: ResolvedConfig): {} { ? undefined : config.options.moduleRoot, - sourceFileName = path.basename(filenameRelative), + sourceFileName = filenameRelative === "unknown" + ? undefined + : filenameRelative, comments = true, compact = "auto", @@ -30,7 +39,10 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceType: path.extname(filenameRelative) === ".mjs" ? "module" : sourceType, - sourceFileName: filename, + // TODO: @babel/parser uses sourceFilename, while @babel/generator and + // @babel/core use sourceFileName. Eventualy align them. + // https://github.com/babel/babel/pull/13518 + sourceFilename: sourceFileName, plugins: [], ...opts.parserOpts, }, @@ -51,7 +63,18 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceMaps, sourceRoot, - sourceFileName, + sourceFileName: + // If there is no filename, we use `"unknown"` in the source map + // `sources` array as a fallback. Due to how @babel/generator works, + // if we passed `undefined` there would be no generated mappings. + // Additionally, `undefined` isn't JSON-serializable. + sourceFileName == null + ? "unknown" + : sourceRoot == null + ? sourceFileName + : // @babel/generator will prepend sourceFileName with sourceRoot, + // so we need to remove it here. + path.relative(sourceRoot, sourceFileName), ...opts.generatorOpts, }, }; diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 79a0d9ae7973..c223313b837a 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -648,14 +648,14 @@ describe("api", function () { }); }); - it("default source map filename", function () { - return transformAsync("var a = 10;", { + it("default source map filename", async function () { + const result = await transformAsync("var a = 10;", { cwd: "/some/absolute", filename: "/some/absolute/file/path.js", sourceMaps: true, - }).then(function (result) { - expect(result.map.sources).toEqual(["path.js"]); }); + + expect(result.map.sources).toEqual(["file/path.js"]); }); it("code option false", function () { diff --git a/packages/babel-core/test/fixtures/parse/output-with-filename.json b/packages/babel-core/test/fixtures/parse/output-with-filename.json new file mode 100644 index 000000000000..591ee626ebe9 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/output-with-filename.json @@ -0,0 +1,339 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 7, + "column": 0, + "index": 91 + }, + "filename": "input.js" + }, + "errors": [], + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 7, + "column": 0, + "index": 91 + }, + "filename": "input.js" + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 2, + "column": 17, + "index": 29 + }, + "filename": "input.js" + }, + "decorators": [ + { + "type": "Decorator", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 11, + "index": 11 + }, + "filename": "input.js" + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 11, + "index": 11 + }, + "filename": "input.js", + "identifierName": "annotation" + }, + "name": "annotation" + } + } + ], + "id": { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 6, + "index": 18 + }, + "end": { + "line": 2, + "column": 13, + "index": 25 + }, + "filename": "input.js", + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14, + "index": 26 + }, + "end": { + "line": 2, + "column": 17, + "index": 29 + }, + "filename": "input.js" + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 31, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 0, + "index": 31 + }, + "end": { + "line": 6, + "column": 1, + "index": 90 + }, + "filename": "input.js" + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 9, + "index": 40 + }, + "end": { + "line": 4, + "column": 19, + "index": 50 + }, + "filename": "input.js", + "identifierName": "annotation" + }, + "name": "annotation" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 20, + "index": 51 + }, + "end": { + "line": 4, + "column": 26, + "index": 57 + }, + "filename": "input.js", + "identifierName": "target" + }, + "name": "target" + } + ], + "body": { + "type": "BlockStatement", + "start": 59, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 28, + "index": 59 + }, + "end": { + "line": 6, + "column": 1, + "index": 90 + }, + "filename": "input.js" + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 64, + "end": 88, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 27, + "index": 88 + }, + "filename": "input.js" + }, + "expression": { + "type": "AssignmentExpression", + "start": 64, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 26, + "index": 87 + }, + "filename": "input.js" + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 64, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 19, + "index": 80 + }, + "filename": "input.js" + }, + "object": { + "type": "Identifier", + "start": 64, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 9, + "index": 70 + }, + "filename": "input.js", + "identifierName": "target" + }, + "name": "target" + }, + "computed": false, + "property": { + "type": "Identifier", + "start": 71, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 10, + "index": 71 + }, + "end": { + "line": 5, + "column": 19, + "index": 80 + }, + "filename": "input.js", + "identifierName": "annotated" + }, + "name": "annotated" + } + }, + "right": { + "type": "BooleanLiteral", + "start": 83, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 22, + "index": 83 + }, + "end": { + "line": 5, + "column": 26, + "index": 87 + }, + "filename": "input.js" + }, + "value": true + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [] +} diff --git a/packages/babel-core/test/fixtures/parse/output.json b/packages/babel-core/test/fixtures/parse/output.json index 1a400f953757..1af0d0d0d23c 100644 --- a/packages/babel-core/test/fixtures/parse/output.json +++ b/packages/babel-core/test/fixtures/parse/output.json @@ -2,7 +2,6 @@ "type": "File", "start": 0, "end": 91, - "errors": [], "loc": { "start": { "line": 1, @@ -15,6 +14,7 @@ "index": 91 } }, + "errors": [], "program": { "type": "Program", "start": 0, @@ -31,8 +31,8 @@ "index": 91 } }, - "interpreter": null, "sourceType": "module", + "interpreter": null, "body": [ { "type": "ClassDeclaration", @@ -270,6 +270,7 @@ }, "name": "target" }, + "computed": false, "property": { "type": "Identifier", "start": 71, @@ -288,8 +289,7 @@ "identifierName": "annotated" }, "name": "annotated" - }, - "computed": false + } }, "right": { "type": "BooleanLiteral", diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index eeec4ce7869d..dcfa1813d4e8 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -18,7 +18,7 @@ function fixture(...args) { describe("parseSync", function () { it("should parse using configuration from .babelrc when a filename is provided", function () { const input = fs.readFileSync(fixture("input.js"), "utf8"); - const output = require(fixture("output")); + const output = require(fixture("output-with-filename.json")); const result = parseSync(input, { filename: fixture("input.js"),