[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const Source = require("./Source");
|
---|
| 8 |
|
---|
| 9 | class CachedSource extends Source {
|
---|
| 10 | constructor(source) {
|
---|
| 11 | super();
|
---|
| 12 | this._source = source;
|
---|
| 13 | this._cachedSource = undefined;
|
---|
| 14 | this._cachedSize = undefined;
|
---|
| 15 | this._cachedMaps = {};
|
---|
| 16 |
|
---|
| 17 | if(source.node) this.node = function(options) {
|
---|
| 18 | return this._source.node(options);
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | if(source.listMap) this.listMap = function(options) {
|
---|
| 22 | return this._source.listMap(options);
|
---|
| 23 | };
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | source() {
|
---|
| 27 | if(typeof this._cachedSource !== "undefined") return this._cachedSource;
|
---|
| 28 | return this._cachedSource = this._source.source();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | size() {
|
---|
| 32 | if(typeof this._cachedSize !== "undefined") return this._cachedSize;
|
---|
| 33 | if(typeof this._cachedSource !== "undefined") {
|
---|
| 34 | if(Buffer.from.length === 1) return new Buffer(this._cachedSource).length;
|
---|
| 35 | return this._cachedSize = Buffer.byteLength(this._cachedSource);
|
---|
| 36 | }
|
---|
| 37 | return this._cachedSize = this._source.size();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | sourceAndMap(options) {
|
---|
| 41 | const key = JSON.stringify(options);
|
---|
| 42 | if(typeof this._cachedSource !== "undefined" && key in this._cachedMaps)
|
---|
| 43 | return {
|
---|
| 44 | source: this._cachedSource,
|
---|
| 45 | map: this._cachedMaps[key]
|
---|
| 46 | };
|
---|
| 47 | else if(typeof this._cachedSource !== "undefined") {
|
---|
| 48 | return {
|
---|
| 49 | source: this._cachedSource,
|
---|
| 50 | map: this._cachedMaps[key] = this._source.map(options)
|
---|
| 51 | };
|
---|
| 52 | } else if(key in this._cachedMaps) {
|
---|
| 53 | return {
|
---|
| 54 | source: this._cachedSource = this._source.source(),
|
---|
| 55 | map: this._cachedMaps[key]
|
---|
| 56 | };
|
---|
| 57 | }
|
---|
| 58 | const result = this._source.sourceAndMap(options);
|
---|
| 59 | this._cachedSource = result.source;
|
---|
| 60 | this._cachedMaps[key] = result.map;
|
---|
| 61 | return {
|
---|
| 62 | source: this._cachedSource,
|
---|
| 63 | map: this._cachedMaps[key]
|
---|
| 64 | };
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | map(options) {
|
---|
| 68 | if(!options) options = {};
|
---|
| 69 | const key = JSON.stringify(options);
|
---|
| 70 | if(key in this._cachedMaps)
|
---|
| 71 | return this._cachedMaps[key];
|
---|
| 72 | return this._cachedMaps[key] = this._source.map();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | updateHash(hash) {
|
---|
| 76 | this._source.updateHash(hash);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | module.exports = CachedSource;
|
---|