Skip to content

Sourcemap URLs break when inlined #6

@andybarron

Description

@andybarron

Issue

By default, Webpack generates sourcemap URLs relative to the output file.

dist/
   index.html
   scripts/
      bundle.js
      bundle.js.map

Sourcemap URL: 'bundle.js.map' 
Relative to 'bundle.js', resolves to URL '/scripts/bundle.js.map'
OK!

Once we inline the output file, the sourcemaps will be loaded relative to the HTML file, which may not work:

dist/
   index.html
   scripts/
      bundle.js
      bundle.js.map

Sourcemap URL: 'bundle.js.map' 
Relative to 'index.html', resolves to URL '/bundle.js.map'
NOT FOUND! :(

Even with source and maps in the top-level dist/ directory, sourcemaps won't work properly for URL paths when using HTML5 history routing:

dist/
   index.html
   bundle.js
   bundle.js.map

Route 'some/route' serves 'index.html'
Sourcemap URL: 'bundle.js.map'
Relative to 'some/route', resolves to URL '/some/route/bundle.js.map'
NOT FOUND! :(

Solution

When inlining the JS, we should change the source map URL to be relative to the Webpack output.publicPath option.

dist/
   index.html
   scripts/
      bundle.js
      bundle.js.map

Sourcemap URL: '/scripts/bundle.js.map' 
Absolute URL served from 'dist/' will correctly resolve to sourcemap file
FIXED! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions