From 1a6855eff23e003e03a99439d66829f6b66cc874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Fri, 27 Apr 2018 22:28:23 +0200 Subject: [PATCH] Fix default sourceFileName. (#7764) * Fix default sourceFileName. This deals with a problem mentioned in [babel/babelify#255][0]. I'm not super sure about the implications, but it seems this may have been a regression from Babel 6. In babel@6, the default `sourceFileName` was the basename of the input file: ```js require('babel-core').transform('var a = 10', { filename: __filename, sourceMaps: true }).map // { version: 3, // sources: [ 'index.js' ], // names: [ 'a' ], // mappings: 'AAAA,IAAIA,IAAI,EAAR', // file: 'index.js', // sourcesContent: [ 'var a = 10' ] } } ``` Currently however, the full file path is used: ```js require('@babel/core').transformSync('var a = 10', { filename: __filename, sourceMaps: true }).map // { version: 3, // sources: [ '/home/goto-bus-stop/Code/babel/repro-babelify-255/index.js' ], // names: [ 'a' ], // mappings: 'AAAA,IAAIA,IAAI,EAAR', // file: '/home/goto-bus-stop/Code/babel/repro-babelify-255/index.js', // sourcesContent: [ 'var a = 10' ] } } ``` This patch adds the `path.basename()` call that [Babel 6 used][1] to @babel/core's default options, so it's the same as back then. ```js require('../babel/packages/babel-core').transform('var a = 10', { filename: __filename, sourceMaps: true }).map // { version: 3, // sources: [ 'index.js' ], // names: [ 'a' ], // mappings: 'AAAA,IAAIA,IAAI,EAAR', // sourcesContent: [ 'var a = 10' ] } ``` This is the desired behaviour for browserify at least, as it expects relative paths in the source maps and rebases them to a root directory when generating the final source map. [0]: https://github.com/babel/babelify/pull/255 [1]: https://github.com/babel/babel/blob/6689d2d23cdb607c326ed5a06855bfb84c050c56/packages/babel-core/src/transformation/file/index.js#L163-L172 * Use cwd-relative path for sourceFileName. * Revert sourceMap `file` property change. * fixup! Revert sourceMap `file` property change. * Fix whitespace change from merge conflict * Revert to using basename in source map outputs. --- .../babel-core/src/transformation/normalize-opts.js | 7 +++++-- packages/babel-core/test/api.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.js b/packages/babel-core/src/transformation/normalize-opts.js index 0b5dedffb2d7..4c53b7aa3517 100644 --- a/packages/babel-core/src/transformation/normalize-opts.js +++ b/packages/babel-core/src/transformation/normalize-opts.js @@ -6,7 +6,10 @@ import type { ResolvedConfig } from "../config"; export default function normalizeOptions(config: ResolvedConfig): {} { const { filename, - filenameRelative = filename || "unknown", + cwd, + filenameRelative = typeof filename === "string" + ? path.relative(cwd, filename) + : "unknown", sourceType = "module", inputSourceMap, sourceMaps = !!inputSourceMap, @@ -14,7 +17,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { moduleRoot, sourceRoot = moduleRoot, - sourceFileName = filenameRelative, + sourceFileName = path.basename(filenameRelative), comments = true, compact = "auto", diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 0977bad100c8..621b170af4eb 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -394,6 +394,16 @@ describe("api", function() { }); }); + it("default source map filename", function() { + return 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"]); + }); + }); + it("code option false", function() { return transformAsync("foo('bar');", { code: false }).then(function( result,