webpack-sources
Advanced tools
Comparing version 1.3.0 to 2.0.0-beta.0
@@ -14,29 +14,34 @@ /* | ||
this._cachedSource = undefined; | ||
this._cachedBuffer = undefined; | ||
this._cachedSize = undefined; | ||
this._cachedMaps = {}; | ||
if(source.node) this.node = function(options) { | ||
return this._source.node(options); | ||
}; | ||
if(source.listMap) this.listMap = function(options) { | ||
return this._source.listMap(options); | ||
}; | ||
} | ||
source() { | ||
if(typeof this._cachedSource !== "undefined") return this._cachedSource; | ||
return this._cachedSource = this._source.source(); | ||
if (typeof this._cachedSource !== "undefined") return this._cachedSource; | ||
return (this._cachedSource = this._source.source()); | ||
} | ||
buffer() { | ||
if (typeof this._cachedBuffer !== "undefined") return this._cachedBuffer; | ||
if (typeof this._cachedSource !== "undefined") { | ||
if (Buffer.isBuffer(this._cachedSource)) { | ||
return (this._cachedBuffer = this._cachedSource); | ||
} | ||
return (this._cachedBuffer = Buffer.from(this._cachedSource, "utf-8")); | ||
} | ||
return (this._cachedBuffer = this._source.buffer()); | ||
} | ||
size() { | ||
if(typeof this._cachedSize !== "undefined") return this._cachedSize; | ||
if(typeof this._cachedSource !== "undefined") | ||
return this._cachedSize = this._cachedSource.length; | ||
return this._cachedSize = this._source.size(); | ||
if (typeof this._cachedSize !== "undefined") return this._cachedSize; | ||
if (typeof this._cachedSource !== "undefined") { | ||
return (this._cachedSize = Buffer.byteLength(this._cachedSource)); | ||
} | ||
return (this._cachedSize = this._source.size()); | ||
} | ||
sourceAndMap(options) { | ||
const key = JSON.stringify(options); | ||
if(typeof this._cachedSource !== "undefined" && key in this._cachedMaps) | ||
const key = options ? JSON.stringify(options) : "{}"; | ||
if (typeof this._cachedSource !== "undefined" && key in this._cachedMaps) | ||
return { | ||
@@ -46,10 +51,10 @@ source: this._cachedSource, | ||
}; | ||
else if(typeof this._cachedSource !== "undefined") { | ||
else if (typeof this._cachedSource !== "undefined") { | ||
return { | ||
source: this._cachedSource, | ||
map: this._cachedMaps[key] = this._source.map(options) | ||
map: (this._cachedMaps[key] = this._source.map(options)) | ||
}; | ||
} else if(key in this._cachedMaps) { | ||
} else if (key in this._cachedMaps) { | ||
return { | ||
source: this._cachedSource = this._source.source(), | ||
source: (this._cachedSource = this._source.source()), | ||
map: this._cachedMaps[key] | ||
@@ -68,7 +73,5 @@ }; | ||
map(options) { | ||
if(!options) options = {}; | ||
const key = JSON.stringify(options); | ||
if(key in this._cachedMaps) | ||
return this._cachedMaps[key]; | ||
return this._cachedMaps[key] = this._source.map(); | ||
const key = options ? JSON.stringify(options) : "{}"; | ||
if (key in this._cachedMaps) return this._cachedMaps[key]; | ||
return (this._cachedMaps[key] = this._source.map()); | ||
} | ||
@@ -75,0 +78,0 @@ |
@@ -7,5 +7,6 @@ /* | ||
const SourceNode = require("source-map").SourceNode; | ||
const SourceListMap = require("source-list-map").SourceListMap; | ||
const Source = require("./Source"); | ||
const { SourceNode, SourceMapConsumer } = require("source-map"); | ||
const { SourceListMap, fromStringWithSourceMap } = require("source-list-map"); | ||
const { getSourceAndMap, getMap } = require("./helpers"); | ||
@@ -16,8 +17,9 @@ class ConcatSource extends Source { | ||
this.children = []; | ||
for(var i = 0; i < arguments.length; i++) { | ||
var item = arguments[i]; | ||
if(item instanceof ConcatSource) { | ||
var children = item.children; | ||
for(var j = 0; j < children.length; j++) | ||
for (let i = 0; i < arguments.length; i++) { | ||
const item = arguments[i]; | ||
if (item instanceof ConcatSource) { | ||
const children = item.children; | ||
for (let j = 0; j < children.length; j++) { | ||
this.children.push(children[j]); | ||
} | ||
} else { | ||
@@ -30,6 +32,7 @@ this.children.push(item); | ||
add(item) { | ||
if(item instanceof ConcatSource) { | ||
var children = item.children; | ||
for(var j = 0; j < children.length; j++) | ||
if (item instanceof ConcatSource) { | ||
const children = item.children; | ||
for (let j = 0; j < children.length; j++) { | ||
this.children.push(children[j]); | ||
} | ||
} else { | ||
@@ -43,3 +46,3 @@ this.children.push(item); | ||
const children = this.children; | ||
for(let i = 0; i < children.length; i++) { | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
@@ -54,5 +57,8 @@ source += typeof child === "string" ? child : child.source(); | ||
const children = this.children; | ||
for(let i = 0; i < children.length; i++) { | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
size += typeof child === "string" ? child.length : child.size(); | ||
size += | ||
typeof child === "string" | ||
? Buffer.byteLength(child, "utf-8") | ||
: child.size(); | ||
} | ||
@@ -62,6 +68,29 @@ return size; | ||
map(options) { | ||
return getMap(this, options); | ||
} | ||
sourceAndMap(options) { | ||
return getSourceAndMap(this, options); | ||
} | ||
node(options) { | ||
const node = new SourceNode(null, null, null, this.children.map(function(item) { | ||
return typeof item === "string" ? item : item.node(options); | ||
})); | ||
const node = new SourceNode( | ||
null, | ||
null, | ||
null, | ||
this.children.map(function(item) { | ||
if (typeof item === "string") return item; | ||
if (typeof item.node === "function") return item.node(options); | ||
const sourceAndMap = item.sourceAndMap(options); | ||
if (sourceAndMap.map) { | ||
return SourceNode.fromStringWithSourceMap( | ||
sourceAndMap.source, | ||
new SourceMapConsumer(sourceAndMap.map) | ||
); | ||
} else { | ||
return sourceAndMap.source; | ||
} | ||
}) | ||
); | ||
return node; | ||
@@ -72,9 +101,19 @@ } | ||
const map = new SourceListMap(); | ||
var children = this.children; | ||
for(var i = 0; i < children.length; i++) { | ||
var item = children[i]; | ||
if(typeof item === "string") | ||
const children = this.children; | ||
for (let i = 0; i < children.length; i++) { | ||
const item = children[i]; | ||
if (typeof item === "string") { | ||
map.add(item); | ||
else | ||
} else if (typeof item.listMap === "function") { | ||
map.add(item.listMap(options)); | ||
} else { | ||
const sourceAndMap = item.sourceAndMap(options); | ||
if (sourceAndMap.map) { | ||
map.add( | ||
fromStringWithSourceMap(sourceAndMap.source, sourceAndMap.map) | ||
); | ||
} else { | ||
map.add(sourceAndMap.source); | ||
} | ||
} | ||
} | ||
@@ -85,9 +124,7 @@ return map; | ||
updateHash(hash) { | ||
var children = this.children; | ||
for(var i = 0; i < children.length; i++) { | ||
var item = children[i]; | ||
if(typeof item === "string") | ||
hash.update(item); | ||
else | ||
item.updateHash(hash); | ||
const children = this.children; | ||
for (let i = 0; i < children.length; i++) { | ||
const item = children[i]; | ||
if (typeof item === "string") hash.update(item); | ||
else item.updateHash(hash); | ||
} | ||
@@ -97,4 +134,2 @@ } | ||
require("./SourceAndMapMixin")(ConcatSource.prototype); | ||
module.exports = ConcatSource; |
@@ -5,12 +5,26 @@ /* | ||
*/ | ||
exports.Source = require("./Source"); | ||
exports.RawSource = require("./RawSource"); | ||
exports.OriginalSource = require("./OriginalSource"); | ||
exports.SourceMapSource = require("./SourceMapSource"); | ||
exports.LineToLineMappedSource = require("./LineToLineMappedSource"); | ||
const defineExport = (name, fn) => { | ||
let value; | ||
Object.defineProperty(exports, name, { | ||
get: () => { | ||
if (fn !== undefined) { | ||
value = fn(); | ||
fn = undefined; | ||
} | ||
return value; | ||
}, | ||
configurable: true | ||
}); | ||
}; | ||
exports.CachedSource = require("./CachedSource"); | ||
exports.ConcatSource = require("./ConcatSource"); | ||
exports.ReplaceSource = require("./ReplaceSource"); | ||
exports.PrefixSource = require("./PrefixSource"); | ||
defineExport("Source", () => require("./Source")); | ||
defineExport("RawSource", () => require("./RawSource")); | ||
defineExport("OriginalSource", () => require("./OriginalSource")); | ||
defineExport("SourceMapSource", () => require("./SourceMapSource")); | ||
defineExport("CachedSource", () => require("./CachedSource")); | ||
defineExport("ConcatSource", () => require("./ConcatSource")); | ||
defineExport("ReplaceSource", () => require("./ReplaceSource")); | ||
defineExport("PrefixSource", () => require("./PrefixSource")); | ||
defineExport("SizeOnlySource", () => require("./SizeOnlySource")); |
@@ -7,8 +7,8 @@ /* | ||
var SourceNode = require("source-map").SourceNode; | ||
var SourceMapConsumer = require("source-map").SourceMapConsumer; | ||
var SourceListMap = require("source-list-map").SourceListMap; | ||
var Source = require("./Source"); | ||
const Source = require("./Source"); | ||
const { SourceNode } = require("source-map"); | ||
const { SourceListMap } = require("source-list-map"); | ||
const { getSourceAndMap, getMap } = require("./helpers"); | ||
var SPLIT_REGEX = /(?!$)[^\n\r;{}]*[\n\r;{}]*/g; | ||
const SPLIT_REGEX = /(?!$)[^\n\r;{}]*[\n\r;{}]*/g; | ||
@@ -30,25 +30,39 @@ function _splitCode(code) { | ||
map(options) { | ||
return getMap(this, options); | ||
} | ||
sourceAndMap(options) { | ||
return getSourceAndMap(this, options); | ||
} | ||
node(options) { | ||
options = options || {}; | ||
var sourceMap = this._sourceMap; | ||
var value = this._value; | ||
var name = this._name; | ||
var lines = value.split("\n"); | ||
var node = new SourceNode(null, null, null, | ||
const value = this._value; | ||
const name = this._name; | ||
const lines = value.split("\n"); | ||
const node = new SourceNode( | ||
null, | ||
null, | ||
null, | ||
lines.map(function(line, idx) { | ||
var pos = 0; | ||
if(options.columns === false) { | ||
var content = line + (idx != lines.length - 1 ? "\n" : ""); | ||
let pos = 0; | ||
if (options && options.columns === false) { | ||
const content = line + (idx !== lines.length - 1 ? "\n" : ""); | ||
return new SourceNode(idx + 1, 0, name, content); | ||
} | ||
return new SourceNode(null, null, null, | ||
_splitCode(line + (idx != lines.length - 1 ? "\n" : "")).map(function(item) { | ||
if(/^\s*$/.test(item)) { | ||
return new SourceNode( | ||
null, | ||
null, | ||
null, | ||
_splitCode(line + (idx !== lines.length - 1 ? "\n" : "")).map( | ||
function(item) { | ||
if (/^\s*$/.test(item)) { | ||
pos += item.length; | ||
return item; | ||
} | ||
const res = new SourceNode(idx + 1, pos, name, item); | ||
pos += item.length; | ||
return item; | ||
return res; | ||
} | ||
var res = new SourceNode(idx + 1, pos, name, item); | ||
pos += item.length; | ||
return res; | ||
}) | ||
) | ||
); | ||
@@ -62,6 +76,7 @@ }) | ||
listMap(options) { | ||
return new SourceListMap(this._value, this._name, this._value) | ||
return new SourceListMap(this._value, this._name, this._value); | ||
} | ||
updateHash(hash) { | ||
hash.update("OriginalSource"); | ||
hash.update(this._value); | ||
@@ -71,4 +86,2 @@ } | ||
require("./SourceAndMapMixin")(OriginalSource.prototype); | ||
module.exports = OriginalSource; |
@@ -7,15 +7,16 @@ /* | ||
var Source = require("./Source"); | ||
var SourceNode = require("source-map").SourceNode; | ||
const Source = require("./Source"); | ||
const { SourceNode } = require("source-map"); | ||
const { getSourceAndMap, getMap } = require("./helpers"); | ||
var REPLACE_REGEX = /\n(?=.|\s)/g; | ||
const REPLACE_REGEX = /\n(?=.|\s)/g; | ||
function cloneAndPrefix(node, prefix, append) { | ||
if(typeof node === "string") { | ||
var result = node.replace(REPLACE_REGEX, "\n" + prefix); | ||
if(append.length > 0) result = append.pop() + result; | ||
if(/\n$/.test(node)) append.push(prefix); | ||
if (typeof node === "string") { | ||
let result = node.replace(REPLACE_REGEX, "\n" + prefix); | ||
if (append.length > 0) result = append.pop() + result; | ||
if (/\n$/.test(node)) append.push(prefix); | ||
return result; | ||
} else { | ||
var newNode = new SourceNode( | ||
const newNode = new SourceNode( | ||
node.line, | ||
@@ -32,3 +33,3 @@ node.column, | ||
} | ||
}; | ||
} | ||
@@ -43,10 +44,19 @@ class PrefixSource extends Source { | ||
source() { | ||
var node = typeof this._source === "string" ? this._source : this._source.source(); | ||
var prefix = this._prefix; | ||
const node = | ||
typeof this._source === "string" ? this._source : this._source.source(); | ||
const prefix = this._prefix; | ||
return prefix + node.replace(REPLACE_REGEX, "\n" + prefix); | ||
} | ||
map(options) { | ||
return getMap(this, options); | ||
} | ||
sourceAndMap(options) { | ||
return getSourceAndMap(this, options); | ||
} | ||
node(options) { | ||
var node = this._source.node(options); | ||
var append = [this._prefix]; | ||
const node = this._source.node(options); | ||
const append = [this._prefix]; | ||
return new SourceNode(null, null, null, [ | ||
@@ -58,6 +68,10 @@ cloneAndPrefix(node, this._prefix, append) | ||
listMap(options) { | ||
var prefix = this._prefix; | ||
var map = this._source.listMap(options); | ||
const prefix = this._prefix; | ||
const map = this._source.listMap(options); | ||
let prefixNextLine = true; | ||
return map.mapGeneratedCode(function(code) { | ||
return prefix + code.replace(REPLACE_REGEX, "\n" + prefix); | ||
let updatedCode = code.replace(REPLACE_REGEX, "\n" + prefix); | ||
if (prefixNextLine) updatedCode = prefix + updatedCode; | ||
prefixNextLine = code.charCodeAt(code.length - 1) === 10; // === /\n$/.test(code) | ||
return updatedCode; | ||
}); | ||
@@ -67,15 +81,9 @@ } | ||
updateHash(hash) { | ||
if(typeof this._source === "string") | ||
hash.update(this._source); | ||
else | ||
this._source.updateHash(hash); | ||
if(typeof this._prefix === "string") | ||
hash.update(this._prefix); | ||
else | ||
this._prefix.updateHash(hash); | ||
hash.update("PrefixSource"); | ||
if (typeof this._source === "string") hash.update(this._source); | ||
else this._source.updateHash(hash); | ||
hash.update(this._prefix); | ||
} | ||
} | ||
require("./SourceAndMapMixin")(PrefixSource.prototype); | ||
module.exports = PrefixSource; |
@@ -7,5 +7,5 @@ /* | ||
var Source = require("./Source"); | ||
var SourceNode = require("source-map").SourceNode; | ||
var SourceListMap = require("source-list-map").SourceListMap; | ||
const Source = require("./Source"); | ||
const { SourceNode } = require("source-map"); | ||
const { SourceListMap } = require("source-list-map"); | ||
@@ -35,2 +35,3 @@ class RawSource extends Source { | ||
updateHash(hash) { | ||
hash.update("RawSource"); | ||
hash.update(this._value); | ||
@@ -37,0 +38,0 @@ } |
@@ -7,7 +7,5 @@ /* | ||
var Source = require("./Source"); | ||
var SourceNode = require("source-map").SourceNode; | ||
var SourceListMap = require("source-list-map").SourceListMap; | ||
var fromStringWithSourceMap = require("source-list-map").fromStringWithSourceMap; | ||
var SourceMapConsumer = require("source-map").SourceMapConsumer; | ||
const Source = require("./Source"); | ||
const { SourceNode } = require("source-map"); | ||
const { getSourceAndMap, getMap, getNode, getListMap } = require("./helpers"); | ||
@@ -30,21 +28,49 @@ class Replacement { | ||
/** @type {Replacement[]} */ | ||
this.replacements = []; | ||
this._replacements = []; | ||
this._isSorted = true; | ||
} | ||
replace(start, end, newValue, name) { | ||
if(typeof newValue !== "string") | ||
throw new Error("insertion must be a string, but is a " + typeof newValue); | ||
this.replacements.push(new Replacement(start, end, newValue, this.replacements.length, name)); | ||
if (typeof newValue !== "string") | ||
throw new Error( | ||
"insertion must be a string, but is a " + typeof newValue | ||
); | ||
this._replacements.push( | ||
new Replacement(start, end, newValue, this._replacements.length, name) | ||
); | ||
this._isSorted = false; | ||
} | ||
insert(pos, newValue, name) { | ||
if(typeof newValue !== "string") | ||
throw new Error("insertion must be a string, but is a " + typeof newValue + ": " + newValue); | ||
this.replacements.push(new Replacement(pos, pos - 1, newValue, this.replacements.length, name)); | ||
if (typeof newValue !== "string") | ||
throw new Error( | ||
"insertion must be a string, but is a " + | ||
typeof newValue + | ||
": " + | ||
newValue | ||
); | ||
this._replacements.push( | ||
new Replacement(pos, pos - 1, newValue, this._replacements.length, name) | ||
); | ||
this._isSorted = false; | ||
} | ||
source(options) { | ||
source() { | ||
return this._replaceString(this._source.source()); | ||
} | ||
map(options) { | ||
if (this._replacements.length === 0) { | ||
return this._source.map(options); | ||
} | ||
return getMap(this, options); | ||
} | ||
sourceAndMap(options) { | ||
if (this._replacements.length === 0) { | ||
return this._source.sourceAndMap(options); | ||
} | ||
return getSourceAndMap(this, options); | ||
} | ||
original() { | ||
@@ -55,22 +81,24 @@ return this._source; | ||
_sortReplacements() { | ||
this.replacements.sort(function(a, b) { | ||
var diff = b.end - a.end; | ||
if(diff !== 0) | ||
return diff; | ||
diff = b.start - a.start; | ||
if(diff !== 0) | ||
return diff; | ||
if (this._isSorted) return; | ||
this._replacements.sort(function(a, b) { | ||
const diff1 = b.end - a.end; | ||
if (diff1 !== 0) return diff1; | ||
const diff2 = b.start - a.start; | ||
if (diff2 !== 0) return diff2; | ||
return b.insertIndex - a.insertIndex; | ||
}); | ||
this._isSorted = true; | ||
} | ||
_replaceString(str) { | ||
if(typeof str !== "string") | ||
throw new Error("str must be a string, but is a " + typeof str + ": " + str); | ||
if (typeof str !== "string") | ||
throw new Error( | ||
"str must be a string, but is a " + typeof str + ": " + str | ||
); | ||
this._sortReplacements(); | ||
var result = [str]; | ||
this.replacements.forEach(function(repl) { | ||
var remSource = result.pop(); | ||
var splitted1 = this._splitString(remSource, Math.floor(repl.end + 1)); | ||
var splitted2 = this._splitString(splitted1[0], Math.floor(repl.start)); | ||
const result = [str]; | ||
this._replacements.forEach(function(repl) { | ||
const remSource = result.pop(); | ||
const splitted1 = this._splitString(remSource, Math.floor(repl.end + 1)); | ||
const splitted2 = this._splitString(splitted1[0], Math.floor(repl.start)); | ||
result.push(splitted1[1], repl.content, splitted2[0]); | ||
@@ -81,3 +109,3 @@ }, this); | ||
let resultStr = ""; | ||
for(let i = result.length - 1; i >= 0; --i) { | ||
for (let i = result.length - 1; i >= 0; --i) { | ||
resultStr += result[i]; | ||
@@ -89,12 +117,12 @@ } | ||
node(options) { | ||
var node = this._source.node(options); | ||
if(this.replacements.length === 0) { | ||
const node = getNode(this._source, options); | ||
if (this._replacements.length === 0) { | ||
return node; | ||
} | ||
this._sortReplacements(); | ||
var replace = new ReplacementEnumerator(this.replacements); | ||
var output = []; | ||
var position = 0; | ||
var sources = Object.create(null); | ||
var sourcesInLines = Object.create(null); | ||
const replace = new ReplacementEnumerator(this._replacements); | ||
const output = []; | ||
let position = 0; | ||
const sources = Object.create(null); | ||
const sourcesInLines = Object.create(null); | ||
@@ -104,3 +132,3 @@ // We build a new list of SourceNodes in "output" | ||
var result = new SourceNode(); | ||
const result = new SourceNode(); | ||
@@ -114,18 +142,23 @@ // We need to add source contents manually | ||
var replaceInStringNode = this._replaceInStringNode.bind(this, output, replace, function getOriginalSource(mapping) { | ||
var key = "$" + mapping.source; | ||
var lines = sourcesInLines[key]; | ||
if(!lines) { | ||
var source = sources[key]; | ||
if(!source) return null; | ||
lines = source.split("\n").map(function(line) { | ||
return line + "\n"; | ||
}); | ||
sourcesInLines[key] = lines; | ||
const replaceInStringNode = this._replaceInStringNode.bind( | ||
this, | ||
output, | ||
replace, | ||
function getOriginalSource(mapping) { | ||
const key = "$" + mapping.source; | ||
let lines = sourcesInLines[key]; | ||
if (!lines) { | ||
const source = sources[key]; | ||
if (!source) return null; | ||
lines = source.split("\n").map(function(line) { | ||
return line + "\n"; | ||
}); | ||
sourcesInLines[key] = lines; | ||
} | ||
// line is 1-based | ||
if (mapping.line > lines.length) return null; | ||
const line = lines[mapping.line - 1]; | ||
return line.substr(mapping.column); | ||
} | ||
// line is 1-based | ||
if(mapping.line > lines.length) return null; | ||
var line = lines[mapping.line - 1]; | ||
return line.substr(mapping.column); | ||
}); | ||
); | ||
@@ -138,4 +171,4 @@ node.walk(function(chunk, mapping) { | ||
// directly to the end of the output | ||
var remaining = replace.footer(); | ||
if(remaining) { | ||
const remaining = replace.footer(); | ||
if (remaining) { | ||
output.push(remaining); | ||
@@ -150,15 +183,15 @@ } | ||
listMap(options) { | ||
let map = getListMap(this._source, options); | ||
this._sortReplacements(); | ||
var map = this._source.listMap(options); | ||
var currentIndex = 0; | ||
var replacements = this.replacements; | ||
var idxReplacement = replacements.length - 1; | ||
var removeChars = 0; | ||
let currentIndex = 0; | ||
const replacements = this._replacements; | ||
let idxReplacement = replacements.length - 1; | ||
let removeChars = 0; | ||
map = map.mapGeneratedCode(function(str) { | ||
var newCurrentIndex = currentIndex + str.length; | ||
if(removeChars > str.length) { | ||
const newCurrentIndex = currentIndex + str.length; | ||
if (removeChars > str.length) { | ||
removeChars -= str.length; | ||
str = ""; | ||
} else { | ||
if(removeChars > 0) { | ||
if (removeChars > 0) { | ||
str = str.substr(removeChars); | ||
@@ -168,10 +201,13 @@ currentIndex += removeChars; | ||
} | ||
var finalStr = ""; | ||
while(idxReplacement >= 0 && replacements[idxReplacement].start < newCurrentIndex) { | ||
var repl = replacements[idxReplacement]; | ||
var start = Math.floor(repl.start); | ||
var end = Math.floor(repl.end + 1); | ||
var before = str.substr(0, Math.max(0, start - currentIndex)); | ||
if(end <= newCurrentIndex) { | ||
var after = str.substr(Math.max(0, end - currentIndex)); | ||
let finalStr = ""; | ||
while ( | ||
idxReplacement >= 0 && | ||
replacements[idxReplacement].start < newCurrentIndex | ||
) { | ||
const repl = replacements[idxReplacement]; | ||
const start = Math.floor(repl.start); | ||
const end = Math.floor(repl.end + 1); | ||
const before = str.substr(0, Math.max(0, start - currentIndex)); | ||
if (end <= newCurrentIndex) { | ||
const after = str.substr(Math.max(0, end - currentIndex)); | ||
finalStr += before + repl.content; | ||
@@ -192,8 +228,8 @@ str = after; | ||
}); | ||
var extraCode = ""; | ||
while(idxReplacement >= 0) { | ||
let extraCode = ""; | ||
while (idxReplacement >= 0) { | ||
extraCode += replacements[idxReplacement].content; | ||
idxReplacement--; | ||
} | ||
if(extraCode) { | ||
if (extraCode) { | ||
map.add(extraCode); | ||
@@ -205,18 +241,27 @@ } | ||
_splitString(str, position) { | ||
return position <= 0 ? ["", str] : [str.substr(0, position), str.substr(position)]; | ||
return position <= 0 | ||
? ["", str] | ||
: [str.substr(0, position), str.substr(position)]; | ||
} | ||
_replaceInStringNode(output, replace, getOriginalSource, node, position, mapping) { | ||
var original = undefined; | ||
_replaceInStringNode( | ||
output, | ||
replace, | ||
getOriginalSource, | ||
node, | ||
position, | ||
mapping | ||
) { | ||
let original = undefined; | ||
do { | ||
var splitPosition = replace.position - position; | ||
let splitPosition = replace.position - position; | ||
// If multiple replaces occur in the same location then the splitPosition may be | ||
// before the current position for the subsequent splits. Ensure it is >= 0 | ||
if(splitPosition < 0) { | ||
if (splitPosition < 0) { | ||
splitPosition = 0; | ||
} | ||
if(splitPosition >= node.length || replace.done) { | ||
if(replace.emit) { | ||
var nodeEnd = new SourceNode( | ||
if (splitPosition >= node.length || replace.done) { | ||
if (replace.emit) { | ||
const nodeEnd = new SourceNode( | ||
mapping.line, | ||
@@ -233,3 +278,3 @@ mapping.column, | ||
var originalColumn = mapping.column; | ||
const originalColumn = mapping.column; | ||
@@ -240,9 +285,13 @@ // Try to figure out if generated code matches original code of this segement | ||
var nodePart; | ||
if(splitPosition > 0) { | ||
let nodePart; | ||
if (splitPosition > 0) { | ||
nodePart = node.slice(0, splitPosition); | ||
if(original === undefined) { | ||
if (original === undefined) { | ||
original = getOriginalSource(mapping); | ||
} | ||
if(original && original.length >= splitPosition && original.startsWith(nodePart)) { | ||
if ( | ||
original && | ||
original.length >= splitPosition && | ||
original.startsWith(nodePart) | ||
) { | ||
mapping.column += splitPosition; | ||
@@ -253,8 +302,8 @@ original = original.substr(splitPosition); | ||
var emit = replace.next(); | ||
if(!emit) { | ||
const emit = replace.next(); | ||
if (!emit) { | ||
// Stop emitting when we have found the beginning of the string to replace. | ||
// Emit the part of the string before splitPosition | ||
if(splitPosition > 0) { | ||
var nodeStart = new SourceNode( | ||
if (splitPosition > 0) { | ||
const nodeStart = new SourceNode( | ||
mapping.line, | ||
@@ -270,10 +319,12 @@ originalColumn, | ||
// Emit the replacement value | ||
if(replace.value) { | ||
output.push(new SourceNode( | ||
mapping.line, | ||
mapping.column, | ||
mapping.source, | ||
replace.value, | ||
mapping.name || replace.name | ||
)); | ||
if (replace.value) { | ||
output.push( | ||
new SourceNode( | ||
mapping.line, | ||
mapping.column, | ||
mapping.source, | ||
replace.value, | ||
mapping.name || replace.name | ||
) | ||
); | ||
} | ||
@@ -285,4 +336,19 @@ } | ||
position += splitPosition; | ||
// eslint-disable-next-line no-constant-condition | ||
} while (true); | ||
} | ||
updateHash(hash) { | ||
this._sortReplacements(); | ||
hash.update("ReplaceSource"); | ||
this._source.updateHash(hash); | ||
hash.update(this._name || ""); | ||
for (const repl of this._replacements) { | ||
hash.update(`${repl.start}`); | ||
hash.update(`${repl.end}`); | ||
hash.update(`${repl.content}`); | ||
hash.update(`${repl.insertIndex}`); | ||
hash.update(`${repl.name}`); | ||
} | ||
} | ||
} | ||
@@ -304,8 +370,7 @@ | ||
next() { | ||
if(this.done) | ||
return true; | ||
if(this.emit) { | ||
if (this.done) return true; | ||
if (this.emit) { | ||
// Start point found. stop emitting. set position to find end | ||
var repl = this.replacements[this.index]; | ||
var end = Math.floor(repl.end + 1); | ||
const repl = this.replacements[this.index]; | ||
const end = Math.floor(repl.end + 1); | ||
this.position = end; | ||
@@ -317,12 +382,11 @@ this.value = repl.content; | ||
this.index--; | ||
if(this.index < 0) { | ||
if (this.index < 0) { | ||
this.done = true; | ||
} else { | ||
var nextRepl = this.replacements[this.index]; | ||
var start = Math.floor(nextRepl.start); | ||
const nextRepl = this.replacements[this.index]; | ||
const start = Math.floor(nextRepl.start); | ||
this.position = start; | ||
} | ||
} | ||
if(this.position < 0) | ||
this.position = 0; | ||
if (this.position < 0) this.position = 0; | ||
this.emit = !this.emit; | ||
@@ -333,10 +397,9 @@ return this.emit; | ||
footer() { | ||
if(!this.done && !this.emit) | ||
this.next(); // If we finished _replaceInNode mid emit we advance to next entry | ||
if(this.done) { | ||
if (!this.done && !this.emit) this.next(); // If we finished _replaceInNode mid emit we advance to next entry | ||
if (this.done) { | ||
return []; | ||
} else { | ||
var resultStr = ""; | ||
for(var i = this.index; i >= 0; i--) { | ||
var repl = this.replacements[i]; | ||
let resultStr = ""; | ||
for (let i = this.index; i >= 0; i--) { | ||
const repl = this.replacements[i]; | ||
// this doesn't need to handle repl.name, because in SourceMaps generated code | ||
@@ -351,4 +414,2 @@ // without pointer to original source can't have a name | ||
require("./SourceAndMapMixin")(ReplaceSource.prototype); | ||
module.exports = ReplaceSource; |
@@ -7,7 +7,3 @@ /* | ||
var SourceNode = require("source-map").SourceNode; | ||
var SourceMapConsumer = require("source-map").SourceMapConsumer; | ||
class Source { | ||
source() { | ||
@@ -17,4 +13,10 @@ throw new Error("Abstract"); | ||
buffer() { | ||
const source = this.source(); | ||
if (Buffer.isBuffer(source)) return source; | ||
return Buffer.from(source, "utf-8"); | ||
} | ||
size() { | ||
return this.source().length; | ||
return Buffer.byteLength(this.source()); | ||
} | ||
@@ -29,20 +31,11 @@ | ||
source: this.source(), | ||
map: this.map() | ||
map: this.map(options) | ||
}; | ||
} | ||
node() { | ||
updateHash(hash) { | ||
throw new Error("Abstract"); | ||
} | ||
listNode() { | ||
throw new Error("Abstract"); | ||
} | ||
updateHash(hash) { | ||
var source = this.source(); | ||
hash.update(source || ""); | ||
} | ||
} | ||
module.exports = Source; |
@@ -7,8 +7,10 @@ /* | ||
var SourceNode = require("source-map").SourceNode; | ||
var SourceMapConsumer = require("source-map").SourceMapConsumer; | ||
var SourceMapGenerator = require("source-map").SourceMapGenerator; | ||
var SourceListMap = require("source-list-map").SourceListMap; | ||
var fromStringWithSourceMap = require("source-list-map").fromStringWithSourceMap; | ||
var Source = require("./Source"); | ||
const Source = require("./Source"); | ||
const { | ||
SourceNode, | ||
SourceMapConsumer, | ||
SourceMapGenerator | ||
} = require("source-map"); | ||
const { SourceListMap, fromStringWithSourceMap } = require("source-list-map"); | ||
const { getSourceAndMap, getMap } = require("./helpers"); | ||
@@ -29,14 +31,41 @@ class SourceMapSource extends Source { | ||
map(options) { | ||
if (!this._innerSourceMap) { | ||
return typeof this._sourceMap === "string" | ||
? JSON.parse(this._sourceMap) | ||
: this._sourceMap; | ||
} | ||
return getMap(this, options); | ||
} | ||
sourceAndMap(options) { | ||
if (!this._innerSourceMap) { | ||
return { | ||
source: this._value, | ||
map: | ||
typeof this._sourceMap === "string" | ||
? JSON.parse(this._sourceMap) | ||
: this._sourceMap | ||
}; | ||
} | ||
return getSourceAndMap(this, options); | ||
} | ||
node(options) { | ||
var innerSourceMap = this._innerSourceMap; | ||
var sourceMap = this._sourceMap; | ||
if(innerSourceMap) { | ||
sourceMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap)); | ||
if(this._originalSource) | ||
sourceMap.setSourceContent(this._name, this._originalSource); | ||
innerSourceMap = new SourceMapConsumer(innerSourceMap); | ||
sourceMap.applySourceMap(innerSourceMap, this._name); | ||
sourceMap = sourceMap.toJSON(); | ||
const innerSourceMap = this._innerSourceMap; | ||
let sourceMap = this._sourceMap; | ||
if (innerSourceMap) { | ||
const sourceMapGen = SourceMapGenerator.fromSourceMap( | ||
new SourceMapConsumer(sourceMap) | ||
); | ||
if (this._originalSource) | ||
sourceMapGen.setSourceContent(this._name, this._originalSource); | ||
const innerSourceMapConsumer = new SourceMapConsumer(innerSourceMap); | ||
sourceMapGen.applySourceMap(innerSourceMapConsumer, this._name); | ||
sourceMap = sourceMapGen.toJSON(); | ||
} | ||
return SourceNode.fromStringWithSourceMap(this._value, new SourceMapConsumer(sourceMap)); | ||
return SourceNode.fromStringWithSourceMap( | ||
this._value, | ||
new SourceMapConsumer(sourceMap) | ||
); | ||
} | ||
@@ -46,5 +75,10 @@ | ||
options = options || {}; | ||
if(options.module === false) | ||
if (options.module === false) | ||
return new SourceListMap(this._value, this._name, this._value); | ||
return fromStringWithSourceMap(this._value, typeof this._sourceMap === "string" ? JSON.parse(this._sourceMap) : this._sourceMap); | ||
return fromStringWithSourceMap( | ||
this._value, | ||
typeof this._sourceMap === "string" | ||
? JSON.parse(this._sourceMap) | ||
: this._sourceMap | ||
); | ||
} | ||
@@ -54,9 +88,6 @@ | ||
hash.update(this._value); | ||
if(this._originalSource) | ||
hash.update(this._originalSource); | ||
if (this._originalSource) hash.update(this._originalSource); | ||
} | ||
} | ||
require("./SourceAndMapMixin")(SourceMapSource.prototype); | ||
module.exports = SourceMapSource; |
{ | ||
"name": "webpack-sources", | ||
"version": "1.3.0", | ||
"version": "2.0.0-beta.0", | ||
"description": "Source code handling classes for webpack", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"pretest": "npm run lint && npm run beautify-lint", | ||
"pretest": "yarn lint", | ||
"test": "mocha --full-trace --check-leaks", | ||
"travis": "npm run cover -- --report lcovonly", | ||
"travis": "yarn cover --report lcovonly", | ||
"lint": "eslint lib test", | ||
"beautify-lint": "beautify-lint lib/**.js test/**.js", | ||
"beautify": "beautify-rewrite lib/**.js test/**.js", | ||
"precover": "npm run lint && npm run beautify-lint", | ||
"cover": "istanbul cover node_modules/mocha/bin/_mocha", | ||
"publish-patch": "npm test && npm version patch && git push && git push --tags && npm publish" | ||
"precover": "yarn lint", | ||
"cover": "istanbul cover node_modules/mocha/bin/_mocha" | ||
}, | ||
"dependencies": { | ||
"source-list-map": "^2.0.0", | ||
"source-list-map": "^2.0.1", | ||
"source-map": "~0.6.1" | ||
}, | ||
"devDependencies": { | ||
"beautify-lint": "^1.0.3", | ||
"codecov.io": "^0.1.6", | ||
"coveralls": "^2.11.6", | ||
"eslint": "^3.19.0", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.12.0", | ||
"eslint-config-prettier": "^3.5.0", | ||
"eslint-plugin-mocha": "^5.2.1", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-nodeca": "^1.0.3", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
"istanbul": "^0.4.1", | ||
"js-beautify": "^1.5.10", | ||
"mocha": "^3.4.2", | ||
"should": "^11.2.1", | ||
"mocha": "^5.2.0", | ||
"prettier": "^1.15.3", | ||
"should": "^13.2.3", | ||
"sourcemap-validator": "^1.1.0" | ||
@@ -36,2 +35,5 @@ }, | ||
], | ||
"engines": { | ||
"node": ">=6.11.5" | ||
}, | ||
"repository": { | ||
@@ -38,0 +40,0 @@ "type": "git", |
114
README.md
@@ -15,20 +15,28 @@ # webpack-sources | ||
``` js | ||
Source.prototype.source() -> String | ||
```js | ||
Source.prototype.source() -> String | Buffer | ||
``` | ||
Returns the represented source code as string. | ||
Returns the represented source code as string or Buffer (for binary Sources). | ||
#### `buffer` | ||
```js | ||
Source.prototype.buffer() -> Buffer | ||
``` | ||
Returns the represented source code as Buffer. Strings are converted to utf-8. | ||
#### `size` | ||
``` js | ||
```js | ||
Source.prototype.size() -> Number | ||
``` | ||
Returns the size in chars of the represented source code. | ||
Returns the size in bytes of the represented source code. | ||
#### `map` | ||
``` js | ||
Source.prototype.map(options: Object) -> Object | null | ||
```js | ||
Source.prototype.map(options?: Object) -> Object | null | ||
``` | ||
@@ -40,10 +48,10 @@ | ||
* `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns. | ||
* `module: Boolean` (default `true`): If set to false the implementation may omit inner mappings for modules. | ||
- `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns. | ||
- `module: Boolean` (default `true`): If set to false the implementation may omit inner mappings for modules. | ||
#### `sourceAndMap` | ||
``` js | ||
Source.prototype.sourceAndMap(options: Object) -> { | ||
code: String, | ||
```js | ||
Source.prototype.sourceAndMap(options?: Object) -> { | ||
source: String | Buffer, | ||
map: Object | ||
@@ -59,3 +67,3 @@ } | ||
``` js | ||
```js | ||
Source.prototype.updateHash(hash: Hash) -> void | ||
@@ -66,26 +74,2 @@ ``` | ||
#### `node` (optional) | ||
``` js | ||
Source.prototype.node(options: Object) -> SourceNode | ||
``` | ||
This is an optional method. It may be `null` if not implemented. | ||
Returns a `SourceNode` (see source-map library) for the represented source code. | ||
See `map()` for `options`. | ||
#### `listNode` (optional) | ||
``` js | ||
Source.prototype.listNode(options: Object) -> SourceNode | ||
``` | ||
This is an optional method. It may be `null` if not implemented. | ||
Returns a `SourceListMap` (see source-list-map library) for the represented source code. | ||
See `map()` for `options`. | ||
## `RawSource` | ||
@@ -95,3 +79,3 @@ | ||
``` js | ||
```js | ||
new RawSource(sourceCode: String) | ||
@@ -104,3 +88,3 @@ ``` | ||
``` js | ||
```js | ||
new OriginalSource( | ||
@@ -112,4 +96,4 @@ sourceCode: String, | ||
* `sourceCode`: The source code. | ||
* `name`: The filename of the original source code. | ||
- `sourceCode`: The source code. | ||
- `name`: The filename of the original source code. | ||
@@ -122,3 +106,3 @@ OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`). | ||
``` js | ||
```js | ||
new SourceMapSource( | ||
@@ -133,29 +117,14 @@ sourceCode: String, | ||
* `sourceCode`: The source code. | ||
* `name`: The filename of the original source code. | ||
* `sourceMap`: The SourceMap for the source code. | ||
* `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code. | ||
* `innerSourceMap`: The SourceMap for the `originalSource`/`name`. | ||
- `sourceCode`: The source code. | ||
- `name`: The filename of the original source code. | ||
- `sourceMap`: The SourceMap for the source code. | ||
- `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code. | ||
- `innerSourceMap`: The SourceMap for the `originalSource`/`name`. | ||
## `LineToLineMappedSource` | ||
Represents source code, which is mapped line by line to the original file. | ||
``` js | ||
new LineToLineMappedSource( | ||
sourceCode: String, | ||
name: String, | ||
originalSource: String | ||
) | ||
``` | ||
* `sourceCode`: The source code. | ||
* `name`: The filename of the original source code. | ||
* `originalSource`: The original source code. | ||
## `CachedSource` | ||
Decorates a `Source` and caches returned results of `map`, `source`, `size` and `sourceAndMap` in memory. Every other operation is delegated to the wrapped `Source`. | ||
Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached. | ||
It tries to reused cached results from other methods to avoid calculations, i. e. when `source` is already cached, calling `size` will get the size from the cached source, calling `sourceAndMap` will only call `map` on the wrapped Source. | ||
``` js | ||
```js | ||
new CachedSource(source: Source) | ||
@@ -168,3 +137,3 @@ ``` | ||
``` js | ||
```js | ||
new PrefixSource( | ||
@@ -178,5 +147,5 @@ prefix: String, | ||
Concatenate mulitple `Source`s or strings to a single source. | ||
Concatenate multiple `Source`s or strings to a single source. | ||
``` js | ||
```js | ||
new ConcatSource( | ||
@@ -191,7 +160,7 @@ ...items?: Source | String | ||
``` js | ||
```js | ||
ConcatSource.prototype.add(item: Source | String) | ||
``` | ||
Adds an item to the source. | ||
Adds an item to the source. | ||
@@ -206,3 +175,3 @@ ## `ReplaceSource` | ||
``` js | ||
```js | ||
ReplaceSource.prototype.replace( | ||
@@ -221,3 +190,3 @@ start: Number, | ||
``` js | ||
```js | ||
ReplaceSource.prototype.insert( | ||
@@ -236,2 +205,1 @@ pos: Number, | ||
Get decorated `Source`. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32584
949
12
1
191
Updatedsource-list-map@^2.0.1