Source-map path resolution when CoffeeScript 2.x output is restored to a different directory
0 reputation · 23 Jul 2022, 11:11 UTC
0 reputation · 23 Jul 2022, 11:11 UTC
The goal is to confirm that a restored .js.map file still maps runtime positions to the original .coffee sources when the compiled JavaScript, map file, and source tree are returned to different directory paths. CoffeeScript 2.x retains the source-map option and implicit-return semantics, but the compiler writes source locations relative to the build environment. Moving any one of the three files independently can break the mapping, and constructors that end in a bare expression may return an unexpected value at runtime.
Version assumption: behavior differs between CoffeeScript 1.x and 2.x, so the compiler line should be identified before relying on module syntax or source-map output. Practical verification concerns whether stack traces and breakpoints resolve to .coffee lines after restoration.
35775 reputation · 23 Jul 2022, 19:13 UTC
In CoffeeScript 2.x, source-map paths are generated relative to the compiled JavaScript file (unless a specific sourceRoot was defined during compilation). If you restore the .js and .js.map files to a directory structure that differs from the original build environment, the relative paths stored in the map will no longer point to the correct .coffee source files, breaking debugger breakpoints and stack trace resolution.
To address the specific constraints of restoring build output to new locations, consider the following behaviors:
sources array in the map contains relative paths. Moving the map file independently of the JavaScript file, or moving both to a new root without updating the source tree, invalidates these references.sourcesContent enabled, the original CoffeeScript source code is embedded directly within the .js.map file. This allows debuggers to display the original code even if the .coffee files are missing from the restored environment. However, this does not "reconstruct" the source tree on disk; it only enables in-browser/IDE visualization.sourceRoot property in the JSON map file to point to the new location of the source files. This is a safe alternative to regeneration if the relative distance between the source files themselves has not changed.To verify if your restoration has broken the mapping, perform these checks:
.js.map file in a text editor. Locate the "sources": [...] array. If the paths are ../src/file.coffee but your restored structure is ../old_src/file.coffee, the mapping is broken..js file instead of the .coffee file, the path resolution is failing.If the directory depth has changed significantly, the most reliable fix is to regenerate the maps using the CoffeeScript 2.x compiler in the new environment. If regeneration is impossible, manually updating the sourceRoot is the primary recovery path.
Missing Diagnostic: To provide a more precise path correction, please specify if the sourcesContent field is currently present in your restored .js.map files.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.