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

Sourcemap link is not the last thing in file. #3148

Open
MicahZoltu opened this issue Oct 8, 2019 · 4 comments
Open

Sourcemap link is not the last thing in file. #3148

MicahZoltu opened this issue Oct 8, 2019 · 4 comments

Comments

@MicahZoltu
Copy link
Contributor

MicahZoltu commented Oct 8, 2019

Rollup is generating a file that has a trailing newline. This MAY go against an implementation of the source map specification.

https://sourcemaps.info/spec.html#h.9ppdoan5f016

The generated code may include a line at the end of the source, with the following form:

//# sourceMappingURL=

One could interpret this to mean that the last characters of the file are the sourcemap comment, without a newline. If one did (as es-module-shims does currently), then the sourcemap comment in a rollup generated file would be invalid.

  • Rollup Version: 1.21.4
  • Operating System (or Browser): Windows Firefox
  • Node Version: v12.3.0

How Do We Reproduce?

https://github.com/just-dont/sourcemaps-with-shims

Expected Behavior

There is no newline at the end of the sourceMappingURL comment.

Actual Behavior

There is a newline at the end of the sourceMappingURL comment.

Suggested fix:
Don't append a newline in order to maintain maximum compatibility.

@MicahZoltu
Copy link
Contributor Author

Sorry to anyone subscribed to this repository, I accidentally hit ctrl-enter which submitted the issue before filling it in. I have updated the issue description now though.

@kzc
Copy link
Contributor

kzc commented Oct 8, 2019

Rollup is generating a file that has a trailing newline. This MAY go against an implementation of the source map specification.

I disagree. The source map spec is informal and editors commonly add either \n or \r\n to the end of files if a source map URL is added manually. Any good source map URL reading implementation would handle the presence or absence of newlines as all browsers currently do.

@MicahZoltu
Copy link
Contributor Author

@kzc While I tend to agree that sourcemap users should be permissive in what they accept (and I believe there is an open PR for es-module-shims to make it more permissive), I also believe that Rollup should follow the strictest interpretation of the specification to obtain maximum compatibility. This follows the Robustness Principle design guideline:

Be conservative in what you send, be liberal in what you accept

@kzc
Copy link
Contributor

kzc commented Oct 9, 2019

The source map spec provided states:

There is an existing convention that should be supported for the use of source maps with eval’d code, it has the following form:

//@ sourceURL=foo.js

It is described here:

http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ 

It appears to be a dead link, but here's an archived copy of that web page:

https://web.archive.org/web/20190203041919/http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/

An actual code fragment with a regular expression is provided:

    const reURIinComment = ///@ssourceURL=s*(S*?)s*$/m;

I assume that the web archive dropped the necessary backslashes to make this valid JS. If one fixes the regexp you can see that optional trailing whitespace - including newlines - is permissible:

    const reURIinComment = /\/\/@\ssourceURL=\s*(\S*?)\s*$/m;

Just to back up that assertion, I see that that web page makes mention of:

https://web.archive.org/web/20190203041940/https://pmuellr.blogspot.com/2009/06/debugger-friendly.html

which in turn mentions:

https://web.archive.org/web/20161221094120/https://bugs.webkit.org/show_bug.cgi?id=25475

which provides this valid JS:

    // if no URL, look for "//@ sourceURL=" decorator     
    if (!sourceURL) {
        var pattern = /.*\s*\/\/\s*@\s*sourceURL\s*=\s*(\S+)\s*/;
        var match = pattern.exec(source);
        
        if (match) {
            this.sourceURL = "eval:" + match[1];
        }
    }

So any source map URI at the end of the source code with or without trailing whitespace - including newlines - are conforming according to the sourcemap spec. es-module-shims does not conform to this spec.

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