evanw/esbuild

Linked sourcemap annotations are incorrect when publicPath is not absolute

simon-paris opened this issue · 0 comments

When publicPath is relative, it gets prepended to the sourcemap annotation, and I think that's incorrect.

Example:

  • Public path: ./build/
  • Script tag: <script src="./build/out.js"/>
  • Sourcemap annotation: //# sourceMappingURL=./build/out.js.map
  • Resolved sourcemap location: ./build/build/out.js.map

The correct annotation would be //# sourceMappingURL=out.js.map because sourcemaps are relative to the script tag src. The same issue happens if publicPath is build or /build.

I think the correct behavior is to prepend the publicPath only if it is absolute.

Sourcemap spec: https://github.com/tc39/source-map-spec/blob/main/source-map-rev3.md

When the source mapping URL is not absolute, then it is relative to the generated code’s “source origin”. The source origin is determined by one of the following cases:

  • If the generated source is not associated with a script element that has a “src” attribute and there exists a //# sourceURL comment in the generated code, that comment should be used to determine the source origin. Note: Previously, this was “//@ sourceURL”, as with “//@ sourceMappingURL”, it is reasonable to accept both but //# is preferred.
  • If the generated code is associated with a script element and the script element has a “src” attribute, the “src” attribute of the script element will be the source origin.
  • If the generated code is associated with a script element and the script element does not have a “src” attribute, then the source origin will be the page’s origin.
  • If the generated code is being evaluated as a string with the eval() function or via new Function(), then the source origin will be the page’s origin.

Related issues:

  • Same issue reported here: rails/sprockets-rails#501
  • This closed issue is about adding an API to allow arbitrary sourcemap prefixes, but the last comment complains about a similar issue but using entryNames instead of publicPath. I could not reproduce their issue though. #2254 (comment).