79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
// 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*$"
|
|
)
|
|
|
|
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(["/*", " */"])
|
|
|
|
}));
|