forked from requirejs/r.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wiring up existing source maps of transpiled codes
When `generateSourceMaps` option is enabled, r.js now detects if the module to be concatenated already declares a source map. It tries to load the existing source map, and translates line numbers of all mappings in that source map to those of the final output. This fixes most of requirejs#470, and works well as long as `optimize` option is set to `none`. Some work still remains to pass a correct `--in-source-map` option to UglifyJS when also uglifying. Currently, UglifyJS ignores r.js's carefully generated source map producing a bogus one if `optimize` option is set to `uglify2`. Used https://github.com/lydell/source-map-url (v0.2.0) for detecting sourceMappingURL= comments from the code.
- Loading branch information
Showing
3 changed files
with
138 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2014 Simon Lydell | ||
|
||
void (function(root, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(factory) | ||
} else if (typeof exports === "object") { | ||
module.exports = factory() | ||
} else { | ||
root.sourceMappingURL = factory() | ||
} | ||
}(this, function(undefined) { | ||
|
||
var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/ | ||
var newlineRegex = /\r\n?|\n/ | ||
|
||
var regex = RegExp( | ||
"(^|(?:" + newlineRegex.source + "))" + | ||
"(?:" + | ||
"/\\*" + | ||
"(?:\\s*(?:" + newlineRegex.source + ")(?://)?)?" + | ||
"(?:" + innerRegex.source + ")" + | ||
"\\s*" + | ||
"\\*/" + | ||
"|" + | ||
"//(?:" + innerRegex.source + ")" + | ||
")" + | ||
"\\s*(?:$|(?:" + newlineRegex.source + "))" | ||
) | ||
|
||
function SourceMappingURL(commentSyntax) { | ||
this._commentSyntax = commentSyntax | ||
} | ||
|
||
SourceMappingURL.prototype.regex = regex | ||
SourceMappingURL.prototype._innerRegex = innerRegex | ||
SourceMappingURL.prototype._newlineRegex = newlineRegex | ||
|
||
SourceMappingURL.prototype.get = function(code) { | ||
var match = code.match(this.regex) | ||
if (!match) { | ||
return null | ||
} | ||
return match[2] || match[3] || "" | ||
} | ||
|
||
SourceMappingURL.prototype.set = function(code, url, commentSyntax) { | ||
if (!commentSyntax) { | ||
commentSyntax = this._commentSyntax | ||
} | ||
// Use a newline present in the code, or fall back to '\n'. | ||
var newline = String(code.match(this._newlineRegex) || "\n") | ||
var open = commentSyntax[0], close = commentSyntax[1] || "" | ||
code = this.remove(code) | ||
return code + newline + open + "# sourceMappingURL=" + url + close | ||
} | ||
|
||
SourceMappingURL.prototype.remove = function(code) { | ||
return code.replace(this.regex, "") | ||
} | ||
|
||
SourceMappingURL.prototype.insertBefore = function(code, string) { | ||
var match = code.match(this.regex) | ||
if (match) { | ||
var hasNewline = Boolean(match[1]) | ||
return code.slice(0, match.index) + | ||
string + | ||
(hasNewline ? "" : "\n") + | ||
code.slice(match.index) | ||
} else { | ||
return code + string | ||
} | ||
} | ||
|
||
SourceMappingURL.prototype.SourceMappingURL = SourceMappingURL | ||
|
||
return new SourceMappingURL(["/*", " */"]) | ||
|
||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters