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

Remove sourceMappingURL from output #12

Closed
jbrantly opened this issue Feb 25, 2015 · 5 comments
Closed

Remove sourceMappingURL from output #12

jbrantly opened this issue Feb 25, 2015 · 5 comments

Comments

@jbrantly
Copy link
Member

Reported by @schlaup in #8. Opening as a new issue here.

From @schlaup:

Ok, thanks, I just discovered an additional problem:

Typescript places its own "//# sourceMappingURL=SomeFile.ts" in the JavaScript output.

So the bundle.js files ends up with multiple "# sourceMappingURL" declarations. (Of course, only the last "# sourceMappingURL=bundle.js.map" is valid)

It seems that Chrome does not care, but other tools do. (For example "NPM source-map-support")

The solution is to remove the redundant and incorrect "sourceMappingURL" declaration.

I added another line:

if (options.sourceMap) {
    var sourceFilename = loaderUtils.getRemainingRequest(this);
    var current = loaderUtils.getCurrentRequest(this);
    sourceMap = JSON.parse(output.outputFiles[0].text);
    sourceMap.sources = [sourceFilename];
    sourceMap.file = current;
    sourceMap.sourcesContent = [contents];
    contents = output.outputFiles[1].text;

This line here:

    contents = contents.replace("# sourceMappingURL=", " ");
}

This solves the problem.

@jbrantly
Copy link
Member Author

I had originally thought about removing the sourceMappingURL but, as you said, it worked in Chrome so I didn't bother. I'm not an expert in the sourcemap specification but one potential issue did occur to me: will removing the content from the source without updating the associated sourcemap cause problems. Do you know offhand?

@jgoz
Copy link

jgoz commented Feb 25, 2015

@jbrantly

will removing the content from the source without updating the associated sourcemap cause problems. Do you know offhand?

This is safe. The sourcemap data itself will not have a line mapping for the sourceMappingURL line, so removing it from the contents will not cause any problems.

Ideally, typescript would have an option to suppress the comment line, but removing it should work for now.

@schlaup
Copy link

schlaup commented Feb 25, 2015

I am not 100% certain but I don't think that removing "# sourceMappingURL=" would cause a problem, because:

  1. "//# sourceMappingURL=" is at the end of the contents so it does not change any offsets in the source map.
  2. Nothing in the source map references the "# sourceMappingURL=" line, because it does not have code.
  3. In my solution I do not remove the line, so the number of lines does not change in the contents. (I leave a comment - "//# sourceMappingURL=somefile.ts" becomes "// somefile.ts")

I have been working with my solution and I have not experienced any problems.

If you really do not want to change the length of the contents, you could replace "# sourceMappingURL=" with some random characters or underline characters, or whatever.

By the way, my single line solution is a bit of a hack, because it could remove any "# sourceMappingURL=" text from the file. You might want to improve it by only changing the last line of the contents.

@jbrantly
Copy link
Member Author

Sorry for the delay on this. I implemented it last night but I'm trying to get a couple more things in before pushing/publishing.

@jbrantly
Copy link
Member Author

Thanks a ton for the report and for the fix! This has been published in v0.3.3.

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

3 participants