webpack-sources
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -8,3 +8,7 @@ /* | ||
const Source = require("./Source"); | ||
const { SourceMapConsumer, SourceNode } = require("source-map"); | ||
const { SourceListMap, fromStringWithSourceMap } = require("source-list-map"); | ||
const LIST_MAP_OPTIONS = { columns: false }; | ||
const mapToBufferedMap = map => { | ||
@@ -38,2 +42,21 @@ if (typeof map !== "object" || !map) return map; | ||
const sourceAndMapToNode = (source, map) => { | ||
if (map) { | ||
return SourceNode.fromStringWithSourceMap( | ||
source, | ||
new SourceMapConsumer(map) | ||
); | ||
} else { | ||
return new SourceNode(null, null, null, source); | ||
} | ||
}; | ||
const sourceAndMapToListMap = (source, map) => { | ||
if (map) { | ||
return fromStringWithSourceMap(source, map); | ||
} else { | ||
return new SourceListMap(source); | ||
} | ||
}; | ||
class CachedSource extends Source { | ||
@@ -52,2 +75,17 @@ constructor(source, cachedData) { | ||
getCachedData() { | ||
const bufferedMaps = new Map(); | ||
for (const pair of this._cachedMaps) { | ||
let cacheEntry = pair[1]; | ||
if (cacheEntry.bufferedMap === undefined) { | ||
cacheEntry.bufferedMap = mapToBufferedMap( | ||
this._getMapFromCacheEntry(cacheEntry) | ||
); | ||
} | ||
bufferedMaps.set(pair[0], { | ||
map: undefined, | ||
node: undefined, | ||
listMap: undefined, | ||
bufferedMap: cacheEntry.bufferedMap | ||
}); | ||
} | ||
// We don't want to cache strings | ||
@@ -60,12 +98,2 @@ // So if we have a caches sources | ||
} | ||
const bufferedMaps = new Map(); | ||
for (const pair of this._cachedMaps) { | ||
if (pair[1].bufferedMap === undefined) { | ||
pair[1].bufferedMap = mapToBufferedMap(pair[1].map); | ||
} | ||
bufferedMaps.set(pair[0], { | ||
map: undefined, | ||
bufferedMap: pair[1].bufferedMap | ||
}); | ||
} | ||
return { | ||
@@ -97,2 +125,28 @@ buffer: this._cachedBuffer, | ||
source() { | ||
const source = this._getCachedSource(); | ||
if (source !== undefined) return source; | ||
return (this._cachedSource = this.original().source()); | ||
} | ||
_getMapFromCacheEntry(cacheEntry) { | ||
if (cacheEntry.map !== undefined) { | ||
return cacheEntry.map; | ||
} else if (cacheEntry.bufferedMap !== undefined) { | ||
return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap)); | ||
} else if (cacheEntry.node !== undefined) { | ||
const result = cacheEntry.node.toStringWithSourceMap({ | ||
file: "x" | ||
}); | ||
if (this._cachedSource === undefined) this._cachedSource = result.code; | ||
return (cacheEntry.map = result.map.toJSON()); | ||
} else if (cacheEntry.listMap !== undefined) { | ||
const result = cacheEntry.listMap.toStringWithSourceMap({ | ||
file: "x" | ||
}); | ||
if (this._cachedSource === undefined) this._cachedSource = result.source; | ||
return (cacheEntry.map = result.map); | ||
} | ||
} | ||
_getCachedSource() { | ||
if (this._cachedSource !== undefined) return this._cachedSource; | ||
@@ -103,10 +157,16 @@ if (this._cachedBuffer && this._cachedSourceType !== undefined) { | ||
: this._cachedBuffer); | ||
} else { | ||
return (this._cachedSource = this.original().source()); | ||
} | ||
for (const cacheEntry of this._cachedMaps.values()) { | ||
if (cacheEntry.node !== undefined) { | ||
return (this._cachedSource = cacheEntry.node.toString()); | ||
} | ||
if (cacheEntry.listMap !== undefined) { | ||
return (this._cachedSource = cacheEntry.listMap.toString()); | ||
} | ||
} | ||
} | ||
buffer() { | ||
if (typeof this._cachedBuffer !== "undefined") return this._cachedBuffer; | ||
if (typeof this._cachedSource !== "undefined") { | ||
if (this._cachedBuffer !== undefined) return this._cachedBuffer; | ||
if (this._cachedSource !== undefined) { | ||
if (Buffer.isBuffer(this._cachedSource)) { | ||
@@ -128,9 +188,10 @@ return (this._cachedBuffer = this._cachedSource); | ||
size() { | ||
if (typeof this._cachedSize !== "undefined") return this._cachedSize; | ||
if (typeof this._cachedSource !== "undefined") { | ||
return (this._cachedSize = Buffer.byteLength(this._cachedSource)); | ||
} | ||
if (typeof this._cachedBuffer !== "undefined") { | ||
if (this._cachedSize !== undefined) return this._cachedSize; | ||
if (this._cachedBuffer !== undefined) { | ||
return (this._cachedSize = this._cachedBuffer.length); | ||
} | ||
const source = this._getCachedSource(); | ||
if (source !== undefined) { | ||
return (this._cachedSize = Buffer.byteLength(source)); | ||
} | ||
return (this._cachedSize = this.original().size()); | ||
@@ -141,31 +202,104 @@ } | ||
const key = options ? JSON.stringify(options) : "{}"; | ||
const cacheEntry = this._cachedMaps.get(key); | ||
// Look for a cached map | ||
if (cacheEntry !== undefined) { | ||
// We have a cached map in some representation | ||
const map = this._getMapFromCacheEntry(cacheEntry); | ||
// Either get the cached source or compute it | ||
return { source: this.source(), map }; | ||
} | ||
// Look for a cached source | ||
let source = this._getCachedSource(); | ||
// Compute the map | ||
let map; | ||
if (source !== undefined) { | ||
map = this.original().map(options); | ||
} else { | ||
// Compute the source and map together. | ||
const sourceAndMap = this.original().sourceAndMap(options); | ||
source = sourceAndMap.source; | ||
map = sourceAndMap.map; | ||
this._cachedSource = source; | ||
} | ||
this._cachedMaps.set(key, { | ||
map, | ||
node: undefined, | ||
listMap: undefined, | ||
bufferedMap: undefined | ||
}); | ||
return { source, map }; | ||
} | ||
node(options) { | ||
const key = options ? JSON.stringify(options) : "{}"; | ||
let cacheEntry = this._cachedMaps.get(key); | ||
if (cacheEntry && cacheEntry.map === undefined) { | ||
cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap); | ||
// Check cache | ||
if (cacheEntry !== undefined) { | ||
// Directly cached | ||
if (cacheEntry.node) return cacheEntry.node; | ||
// Construct from cached map | ||
const map = this._getMapFromCacheEntry(cacheEntry); | ||
const source = this.source(); | ||
const node = sourceAndMapToNode(source, map); | ||
cacheEntry.node = node; | ||
return node; | ||
} | ||
if (typeof this._cachedSource !== "undefined") { | ||
if (cacheEntry === undefined) { | ||
const map = this.original().map(options); | ||
this._cachedMaps.set(key, { map, bufferedMap: undefined }); | ||
return { | ||
source: this._cachedSource, | ||
map | ||
}; | ||
} else { | ||
return { | ||
source: this._cachedSource, | ||
map: cacheEntry.map | ||
}; | ||
let node; | ||
const original = this.original(); | ||
if (typeof original.node === "function") { | ||
node = original.node(options); | ||
this._cachedMaps.set(key, { | ||
map: undefined, | ||
node, | ||
listMap: undefined, | ||
bufferedMap: undefined | ||
}); | ||
} else { | ||
const sourceAndMap = this.sourceAndMap(options); | ||
node = sourceAndMapToNode(sourceAndMap.source, sourceAndMap.map); | ||
this._cachedMaps.get(key).node = node; | ||
} | ||
return node; | ||
} | ||
listMap(options) { | ||
let key; | ||
// Enforce options must include columns: false | ||
if (!options) { | ||
key = '{"columns":false}'; | ||
options = LIST_MAP_OPTIONS; | ||
} else { | ||
if (options.columns !== false) { | ||
options = Object.assign({}, options, LIST_MAP_OPTIONS); | ||
} | ||
} else if (cacheEntry !== undefined) { | ||
return { | ||
source: (this._cachedSource = this.original().source()), | ||
map: cacheEntry.map | ||
}; | ||
key = JSON.stringify(options); | ||
} | ||
// Check cache | ||
let cacheEntry = this._cachedMaps.get(key); | ||
if (cacheEntry !== undefined) { | ||
// Directly cached | ||
if (cacheEntry.listMap) return cacheEntry.listMap; | ||
// Construct from cached map | ||
const map = this._getMapFromCacheEntry(cacheEntry); | ||
const source = this.source(); | ||
const listMap = sourceAndMapToListMap(source, map); | ||
cacheEntry.listMap = listMap; | ||
return listMap; | ||
} | ||
let listMap; | ||
const original = this.original(); | ||
if (typeof original.listMap === "function") { | ||
listMap = original.listMap(options); | ||
this._cachedMaps.set(key, { | ||
map: undefined, | ||
node: undefined, | ||
listMap, | ||
bufferedMap: undefined | ||
}); | ||
} else { | ||
const result = this.original().sourceAndMap(options); | ||
this._cachedSource = result.source; | ||
this._cachedMaps.set(key, { map: result.map, bufferedMap: undefined }); | ||
return result; | ||
const sourceAndMap = this.sourceAndMap(options); | ||
listMap = sourceAndMapToListMap(sourceAndMap.source, sourceAndMap.map); | ||
this._cachedMaps.get(key).listMap = listMap; | ||
} | ||
return listMap; | ||
} | ||
@@ -175,11 +309,13 @@ | ||
const key = options ? JSON.stringify(options) : "{}"; | ||
let cacheEntry = this._cachedMaps.get(key); | ||
const cacheEntry = this._cachedMaps.get(key); | ||
if (cacheEntry !== undefined) { | ||
if (cacheEntry.map === undefined) { | ||
cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap); | ||
} | ||
return cacheEntry.map; | ||
return this._getMapFromCacheEntry(cacheEntry); | ||
} | ||
const map = this.original().map(options); | ||
this._cachedMaps.set(key, { map, bufferedMap: undefined }); | ||
this._cachedMaps.set(key, { | ||
map, | ||
node: undefined, | ||
listMap: undefined, | ||
bufferedMap: undefined | ||
}); | ||
return map; | ||
@@ -186,0 +322,0 @@ } |
{ | ||
"name": "webpack-sources", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Source code handling classes for webpack", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
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
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
58858
1781