hoge1e3 hoge1e3 - 6 days ago 5
Javascript Question

Which is the base url of sourcemappingurl: relative from html or js?

Suppose that there is a js file with source-map in subdirectory of a html file.


  • index.html (uses js/myjs.js)

  • js/myjs.js

  • js/myjs.js.map



then which is the sourceMappingURL comment of myjs.js ?

//# sourceMappingURL=myjs.js.map


or

//# sourceMappingURL=js/myjs.js.map

Answer

I lookup to the specification.

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.

So if you use <script src="js/myjs.js"> in index.html the js/myjs.js.map is used.