New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix default sourceFileName. #7764
Fix default sourceFileName. #7764
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7784/ |
@@ -14,7 +14,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { | |||
moduleRoot, | |||
sourceRoot = moduleRoot, | |||
|
|||
sourceFileName = filenameRelative, | |||
sourceFileName = path.basename(filenameRelative), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking the basename here is not right. The sourceFileName should include at least part of the file path.
Perhaps what we should do is change
filenameRelative = filename || "unknown",
to
filenameRelative = typeof filename === "string" ? path.relative(config.options.cwd, filename) || "unknown",
I'd also probably be in favor of removing unknown
in favor of null
but that could probably wait.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that sounds right, taking the actual relative path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that'd be ideal. We didn't have cwd
in 6.x so it wasn't as nice of an option. Once we land the config PR it might even make sense to use the root
value, but cwd works for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, that makes sense. will check if browserify+babelify deals well with that too and update tonight.
Updated to use cwd-relative paths, but it seems like it's not compatible with browserify's source map rebasing. I thought adding a |
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]: babel/babelify#255 [1]: https://github.com/babel/babel/blob/6689d2d23cdb607c326ed5a06855bfb84c050c56/packages/babel-core/src/transformation/file/index.js#L163-L172
cba1ec1
to
7546ac7
Compare
packages/babel-core/test/api.js
Outdated
sourceMaps: true, | ||
}).then(function(result) { | ||
expect(result.map.sources).toEqual(["file/path.js"]); | ||
expect(result.map.file).toEqual("file/path.js"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this needs to be removed since you reverted that change.
Are you familiar with how Browserify does that kind of thing? I'm not, but I'd be happy to try to figure it out. What specifically are you seeing that's broken with the cwd-relative-path with Browserify? |
I'm somewhat familiar, but find the details hard to follow. The file paths I was seeing in the source map output after babelifying Double checking now, it's actually a single test that's failingβwhen passing a different But making babelify pass browserify's basedir to babel as the cwd option fixes it! I think it would make sense to do that, since the basedir option is really exactly the same thing as babel's cwd option. babel/babelify#264 works for me with the changes in this PR. |
Ugh, I'm sorry, I was wrong. I see now why it was using You want the path of the filename in the I still think the code in this PR is ideal, and the other PR you linked is sounds like a good idea to me too. If you want to re-add that |
Gotcha! Updated. Babelify's sourcemap tests also pass without the other PR, if the basename is used. (I guess the basedirβcwd mapping still makes sense regardless.) |
This deals with a problem mentioned in babel/babelify#255. 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 inputfile:
Currently however, the full file path is used:
This patch adds the
path.basename()
call that Babel 6 used to@babel/core's default options, so it's the same as back then.
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.