Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using banner option results in an incorrect source-map #362

Open
viddo opened this issue Oct 20, 2015 · 4 comments
Open

Using banner option results in an incorrect source-map #362

viddo opened this issue Oct 20, 2015 · 4 comments

Comments

@viddo
Copy link

viddo commented Oct 20, 2015

Really the same issue as @lcrespom wrote on the merged&closed PR #253, just creating a separate issue for this:

When using the banner option in combination with source maps, the banner option adds a line without updating the source map.

This results in all source code to be one line away from its real position, or more if the banner has multiple lines.

Sounds like a bug to me, does anyone have a solution/workaround for this?

@viddo
Copy link
Author

viddo commented Oct 20, 2015

Looking into the code, the problem is that the banner is added after the bundle is created (and thus the source-map) which explains why the source code appears to be offset from the real position.

I don't know browserify internals enough to give a solution right now, but imagine the banner should be added to the buffer earlier on, probably before creating the source-map.

@viddo
Copy link
Author

viddo commented Oct 20, 2015

Having a quick look, a better solution would probably be to change the browerify bundle pipeline (could probably be done in the prebundlecb option) and inject a "banner" handler before the debug handler to add the banner.

If I get some time over later I'll have a 🔪 at this.

@viddo
Copy link
Author

viddo commented Nov 2, 2015

To whoever who encounters this issue here is a workaround until I get time to implement a PR, or if someone beats me to it 🙏

Provide a custom prelude option, it's passed to the pack transform in the pipeline, which is where the source-map is adapted to the surrounding wrapper code, see https://github.com/substack/browser-pack/blob/master/index.js#L35-L39

var path = require('path');
var defaultPreludePath = grunt.file.read(
  path.join('node_modules', 'browserify', 'node_modules', 'browser-pack', '_prelude.js')
);
  ...

  browserifyOptions: {
    debug: true, // to generate source maps
    prelude: [
      '// the custom header',
      '// another line',
      defaultPreludePath
    ].join("\n")

    ...

@WickyNilliams
Copy link

WickyNilliams commented Sep 19, 2016

I've just encountered this problem also.

I worked around it by adding a banner only in my production build, and enabling source maps only in my dev build. This met my needs, since I don't want source maps in production and I don't care about a banner when i'm developing.

Hopefully this is helpful to someone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants