-
Notifications
You must be signed in to change notification settings - Fork 634
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
Respect/Merge Input Source Maps With Generated Source Map Data #104
Comments
Hoping this could help serve as a starting point. Could use some direction on desired way to pass the input source map through the pipeline, optimizations, etc. |
I'm also experiencing this issue. This is listed as a feature. I've seen people online talk about how this used to work, but is no longer working. Does anyone know of a way to work around this for now? |
@ScottPierce what powered the feature in the past? Metro never had code for this. @RyanThomas73 Last time I checked that code in babel it did not work for all cases, but I can’t recall the details. In any case, supporting that would add considerable performance overhead, as we would have to parse all mappings in input source mappings into our internal format. I am not sure this should be the default. Are there any example packages you can point us at? |
@davidaurelio I've been reading that after a specific version it stopped working. Perhaps I'm wrong? Here is a package that does this and is kept up-to-date specifically for typescript: https://github.com/ds300/react-native-typescript-transformer Here is a package that is no longer up-to-date, but did this for all js files with .map files. |
@davidaurelio The performance overhead is definitely a concern that will have to be figured out; I don't think you would want to parse them into the compact format used by metro. The compact format does not support tracking the original source file, unless you're going to update that part to include it as an additional element in the 'tuple'. Metro's structure has changed somewhat since I first entered this ticket; with metro-source-map now being a separate package. From what I know of the current metro pipeline now there are two ways this could be achieved:
Since the full input source map requires considerable overhead it would be ideal to delay reading/parsing it until as close to the merge site as possible but, to my knowledge, these later stage call sites in the pipeline aren't async and I think they're outside of the transformer workers. @ScottPierce As David mentioned metro has never had code for applying input source maps. I'm guessing the issues you reading were related to the creation of output source maps generated by metro not working. |
Damn, I'm running into this as well. I'd love to hear if there are any updates about this. My situation:
|
I'm working on shadow-cljs which is a build tool for ClojureScript and I'm adding proper support for Any plans to make this a possibility? |
Bump. This deserves more attention from the Metro team. What does Facebook do internally for this? |
I don't think Metro can easily handle this if babel/generator does not support merging inputSourceMap by default. This was submitted as an issue Before babel/generator makes this easier for us, I think we can perhaps pursuit a workaround by leveraging this source map merging function in babel: https://github.com/babel/babel/blob/fced5cea430cc00e916876b663a8d2a84a5dad1f/packages/babel-core/src/transformation/file/merge-map.js Note that the above function expects two JSON source map form, however Metro uses the rawMappings constructed by babel. So we would have to fiddle a bit with the merging logic to work with rawMappings:
|
That sounds like a great workaround @raejin -- what would it take to integrate that into Metro, since Babel doesn't seem super interested in this fix/feature? It feels like beyond my level of understanding of either tool, but I'd be happy to beta metro to see if it works for my project? |
@raejin I hesitate to resurrect a potentially dead ticket, but we’re finding even more reasons why we’d love this feature to exist. Is there any hope? |
I noticed in this Pull Request (facebook/react-native#25613) there was a nifty new file: https://github.com/facebook/react-native/blob/master/scripts/compose-source-maps.js I'm going to try to mess with this script and see if I can put anything together. Ideally, metro would automatically consume source maps and produce a merged output file internally, however. |
@fbartho We pursued using babel to transform our TypeScript files completely so this isn't an issue for us anymore. Sorry about that! I hope this carries out soon. |
Has anyone gotten this working? |
is there any potential update to this? We're using a monorepo structure to create a module which is then consumed by a react-native app in the same structure. Module is transpiled using babel with sourcemaps but the native app shows the transpiled code as the source. We're working with |
Quite frustraing to see yet another feature that's been standard for years in community bundlers missing from metro. In my case I have a requirement that I need to compile my sources with a customized version of @rnx-kit/metro-serializer-esbuild looks promising, but apparently it doesn't work in dev mode. I suppose I'll see if I can find a way to hack this into metro, there are a number of npm packages for merging source maps. |
Hey 👋 I think this thread might be obsolete— maybe we just need to enable A babel team member (in the issue mentioned above) said babel-core already merges source maps very well. Below you can see how it already does what I think @RyanThomas73 had planned to do manually in the // from: https://github.com/babel/babel/blob/v7.19.3/packages/babel-core/src/transformation/file/generate.ts#L57-L61
...
if (inputMap) { // inputMap => parsed from `options.inputSourceMap`
outputMap = mergeSourceMap(
inputMap.toObject(),
outputMap,
generatorOpts.sourceFileName,
); Maybe I’m wrong? Can a maintainer confirm this? Thank you. 🙏 |
If anyone wants to laugh at what I’ve tried so far:
When Metro finally calls Babel, it is explicitly passing (edit: as mentioned here already, it’s metro-source-map— using custom, compact sourcemaps. Referring to the comment above for more direction.) Please help if you can! |
I'm also interested in this, using shadow-cljs to write my React Native app in Clojurescript. It's been a while; has there been a change or any updates? @shaunlebron Did you end up getting it to work? Thanks!! |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
The transform pipeline does not respect/load/merge existing input source maps with generated source map data.
What is the expected behavior?
If an input module has an existing source map file, especially if the input module includes a source map url for the existing source map file, metro should load that input source map and merge that mapping with the output mapping generated by metro.
Related:
#10
facebook/react-native#12705
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
Details
The transform pipeline does not load existing input source maps, and thus does not merge them with the generated source map data.
If the input module code contains a
//# sourceMappingURL=
reference to the existing input source map, then the existing input source map file contents should be loaded early in the process, possibly in theModule
class where it can be cached with the other module data and should flow through the transform pipeline.If an input source map object is present it should be merged with/transform the output source map.
The transformer should pass the input source map to the
babel.transform(..)
call via thebabelConfig.inputSourceMap
property. The post transform logic should merge the input source map with the generated one.For Reference.
https://github.com/babel/babel/blob/7d37017a5f85260c0c95580731585916e791265c/packages/babel-core/src/transformation/file/index.js#L290
I played around with modified version of
transform(..)
andpostTransform(..)
methods. It worked for my very basic proof of concept. I was hoping one of the maintainers familiar with the source map optimizations and recent overhaul of the transform pipeline (e.g. @davidaurelio ) could weight in on what else would be needed for an acceptable PR.Something like this for example (
Module.js
):Pass the input source map data through...
For my proof of concept with the raw mappings I was doing this:
src/transformer.js
src/JSTransformer/worker/index.js
The text was updated successfully, but these errors were encountered: