- Notifications
You must be signed in to change notification settings - Fork 366
Add a third parameter to applySourceMap #93
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
Conversation
Use case: `util.join('a', '../b')` should be `'b'`, not `'a/../b'`. | Thanks for the detailed description. I'll take a look at this tomorrow. |
| Crap. I just found a mistake, that somehow slipped through the tests. I'll fix it ASAP, hopefully this weekend. |
| I would favor to not have an object
Additionally an absolute url should be supported as 3rd argument. |
| I actually considered that first—but the other way around: A directory either absolute or relative from B to A. Isn't that what you meant? I don't understand how to construct the source paths otherwise. The reason I didn't go that way was because using Consider this directory structure:
Using Or this directory structure:
But those kind of cases are the only cases I can think of that would produce less than optimal source paths: When you first "climb up" a bit, and then a source happens to "climb down" a bit again. And that doesn't feel like a common use case. A single path to the dirname of map A relative to map B would solve autoprefixer's use cases/issues as well as the test case of this PR exactly as well as So now it feels like a silly optimization. @sokra's idea makes a much simpler and easier to understand API, and it would also solve the mistake I mentioned in a much simpler way than I had in mind. @fitzgen, I'm gonna make this change, rebase and force push, so don't waste any time reviewing this right now (although a lot will of course be the same). |
| Done. Ready for feedback again :) |
test/source-map/test-util.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't //b a protocol-relative url and should be threaded as absolute url? @fitzgen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's possible. When I wrote these tests I played with the cd command and typing URLs in Firefox.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @sokra. Yes, this should result in "/b". Can you add this assertion as well:
assert.equal(libUtil.join("http://example.com/foo", "//bar"), "http://example.com/bar"); Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually should be this way:
assert.equal(libUtil.join('a/', '/b'), '/b'); assert.equal(libUtil.join("http://example.com/foo", "/bar"), "http://example.com/bar"); assert.equal(libUtil.join("http://example.com/foo", "//bar"), "http://bar"); assert.equal(libUtil.join("https://example.com/foo", "//bar.com/foo"), "https://bar.com/foo");| Thanks for the PR :) Will merge it when you've updated the patch based on comments above. |
The paths in the `sources` property of source maps may be relative. If so, they are relative to the source map itself. When you apply a source map A to another source map B, the source in map B that is associated with map A will be replaced with sources from map A. If those sources are relative, they need to be rewritten to be relative to map B instead of map A. In order to do that, the `applySourceMap` method needs more information: The missing piece between map B and map A. It is now possible to give this information, using a third parameter: The path to map A, relative to map B. (It can also be absolute, of course.) This should be backwards compatible. If the third parameter is omitted, the method works exactly like before: It assumes that both maps are in the same directory, which makes all sources relative to both maps, which in turn requires no rewriting.
| I've amended fixes for the nitpicks/code style stuff. Starting to work on |
- If aPath is a URL or a data URI, aPath is returned, unless aPath is a scheme-relative URL: Then the scheme of aRoot, if any, is prepended first. - Otherwise aPath is a path. If aRoot is a URL, then its path portion is updated with the result and aRoot is returned. Otherwise the result is returned. - If aPath is absolute, the result is aPath. - Otherwise the two paths are joined with a slash. - Joining for example 'http://' and 'www.example.com' is also supported.
| There we go, I've addressed everything except #93 (comment) which needs clarification and discussion. Ready for another round of feedback :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be simplified to if (aSourceMapPath).
| I might have addressed #93 (comment) now. |
| lib/source-map/util.js is possibly a bit long now; Should we move urlParse, urlGenerate, normalize, join and relative into its own file (called perhaps url.js or path.js)? |
| Just a note: I probably won't be able to get to this pull request this week because I'm at a team offsite all week. |
| Hmm ... it would really be a lot easier if we could use the Node.js 'url' module. It’s on npm: https://www.npmjs.org/package/url. Is this out of question? |
| Really, it would be cool if we could use |
| Thinking a bit more about it, I think we should do one of the following:
And then we should adjust the third argument of the |
| It would make sense to have a pluggable join/resolve thing going on, but it would need to be integrated with the build system. Feel free to file an issue to track progress. |
The paths in the
sourcesproperty of source maps may be relative. Ifso, they are relative to the source map itself. When you apply a source
map A to another source map B, the source in map B that is associated
with map A will be replaced with sources from map A. If those sources
are relative, they need to be rewritten to be relative to map B instead
of map A. In order to do that, the
applySourceMapmethod needs moreinformation: The missing piece between map B and map A. It is now
possible to give this information, using a third parameter: The path to
map A, relative to map B. (It can also be absolute, of course.)
This should be backwards compatible. If the third parameter is omitted,
the method works exactly like before: It assumes that both maps are in
the same directory, which makes all sources relative to both maps, which
in turn requires no rewriting.