Linked sourcemap annotations are incorrect when publicPath is not absolute
- Dominant language
- Go
- Stars
- 40.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
When publicPath is relative, it gets prepended to the sourcemap annotation, and I think that's incorrect.
Example:
* Public path: `./build/`
* Script tag: ``
* 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: https://github.com/rails/sprockets-rails/pull/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. https://github.com/evanw/esbuild/issues/2254#issuecomment-1956690026.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the publicPath and linked sourcemap annotation behavior described in the example, then consult the referenced source map specification. Trace how relative and absolute publicPath values produce the annotation; done means a relative annotation resolves relative to the script src while an absolute publicPath is still prepended.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, javascript
- Domain
- build-system, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100