[79a0317] | 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 | const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
|
---|
| 9 | const streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
|
---|
| 10 | const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
|
---|
| 11 |
|
---|
| 12 | class SourceMapSource extends Source {
|
---|
| 13 | constructor(
|
---|
| 14 | value,
|
---|
| 15 | name,
|
---|
| 16 | sourceMap,
|
---|
| 17 | originalSource,
|
---|
| 18 | innerSourceMap,
|
---|
| 19 | removeOriginalSource
|
---|
| 20 | ) {
|
---|
| 21 | super();
|
---|
| 22 | const valueIsBuffer = Buffer.isBuffer(value);
|
---|
| 23 | this._valueAsString = valueIsBuffer ? undefined : value;
|
---|
| 24 | this._valueAsBuffer = valueIsBuffer ? value : undefined;
|
---|
| 25 |
|
---|
| 26 | this._name = name;
|
---|
| 27 |
|
---|
| 28 | this._hasSourceMap = !!sourceMap;
|
---|
| 29 | const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
|
---|
| 30 | const sourceMapIsString = typeof sourceMap === "string";
|
---|
| 31 | this._sourceMapAsObject =
|
---|
| 32 | sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
|
---|
| 33 | this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
|
---|
| 34 | this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
|
---|
| 35 |
|
---|
| 36 | this._hasOriginalSource = !!originalSource;
|
---|
| 37 | const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
|
---|
| 38 | this._originalSourceAsString = originalSourceIsBuffer
|
---|
| 39 | ? undefined
|
---|
| 40 | : originalSource;
|
---|
| 41 | this._originalSourceAsBuffer = originalSourceIsBuffer
|
---|
| 42 | ? originalSource
|
---|
| 43 | : undefined;
|
---|
| 44 |
|
---|
| 45 | this._hasInnerSourceMap = !!innerSourceMap;
|
---|
| 46 | const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
|
---|
| 47 | const innerSourceMapIsString = typeof innerSourceMap === "string";
|
---|
| 48 | this._innerSourceMapAsObject =
|
---|
| 49 | innerSourceMapIsBuffer || innerSourceMapIsString
|
---|
| 50 | ? undefined
|
---|
| 51 | : innerSourceMap;
|
---|
| 52 | this._innerSourceMapAsString = innerSourceMapIsString
|
---|
| 53 | ? innerSourceMap
|
---|
| 54 | : undefined;
|
---|
| 55 | this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
|
---|
| 56 | ? innerSourceMap
|
---|
| 57 | : undefined;
|
---|
| 58 |
|
---|
| 59 | this._removeOriginalSource = removeOriginalSource;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | _ensureValueBuffer() {
|
---|
| 63 | if (this._valueAsBuffer === undefined) {
|
---|
| 64 | this._valueAsBuffer = Buffer.from(this._valueAsString, "utf-8");
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | _ensureValueString() {
|
---|
| 69 | if (this._valueAsString === undefined) {
|
---|
| 70 | this._valueAsString = this._valueAsBuffer.toString("utf-8");
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | _ensureOriginalSourceBuffer() {
|
---|
| 75 | if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
|
---|
| 76 | this._originalSourceAsBuffer = Buffer.from(
|
---|
| 77 | this._originalSourceAsString,
|
---|
| 78 | "utf-8"
|
---|
| 79 | );
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | _ensureOriginalSourceString() {
|
---|
| 84 | if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
|
---|
| 85 | this._originalSourceAsString = this._originalSourceAsBuffer.toString(
|
---|
| 86 | "utf-8"
|
---|
| 87 | );
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | _ensureInnerSourceMapObject() {
|
---|
| 92 | if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
|
---|
| 93 | this._ensureInnerSourceMapString();
|
---|
| 94 | this._innerSourceMapAsObject = JSON.parse(this._innerSourceMapAsString);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | _ensureInnerSourceMapBuffer() {
|
---|
| 99 | if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
|
---|
| 100 | this._ensureInnerSourceMapString();
|
---|
| 101 | this._innerSourceMapAsBuffer = Buffer.from(
|
---|
| 102 | this._innerSourceMapAsString,
|
---|
| 103 | "utf-8"
|
---|
| 104 | );
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | _ensureInnerSourceMapString() {
|
---|
| 109 | if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
|
---|
| 110 | if (this._innerSourceMapAsBuffer !== undefined) {
|
---|
| 111 | this._innerSourceMapAsString = this._innerSourceMapAsBuffer.toString(
|
---|
| 112 | "utf-8"
|
---|
| 113 | );
|
---|
| 114 | } else {
|
---|
| 115 | this._innerSourceMapAsString = JSON.stringify(
|
---|
| 116 | this._innerSourceMapAsObject
|
---|
| 117 | );
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | _ensureSourceMapObject() {
|
---|
| 123 | if (this._sourceMapAsObject === undefined) {
|
---|
| 124 | this._ensureSourceMapString();
|
---|
| 125 | this._sourceMapAsObject = JSON.parse(this._sourceMapAsString);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | _ensureSourceMapBuffer() {
|
---|
| 130 | if (this._sourceMapAsBuffer === undefined) {
|
---|
| 131 | this._ensureSourceMapString();
|
---|
| 132 | this._sourceMapAsBuffer = Buffer.from(this._sourceMapAsString, "utf-8");
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | _ensureSourceMapString() {
|
---|
| 137 | if (this._sourceMapAsString === undefined) {
|
---|
| 138 | if (this._sourceMapAsBuffer !== undefined) {
|
---|
| 139 | this._sourceMapAsString = this._sourceMapAsBuffer.toString("utf-8");
|
---|
| 140 | } else {
|
---|
| 141 | this._sourceMapAsString = JSON.stringify(this._sourceMapAsObject);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | getArgsAsBuffers() {
|
---|
| 147 | this._ensureValueBuffer();
|
---|
| 148 | this._ensureSourceMapBuffer();
|
---|
| 149 | this._ensureOriginalSourceBuffer();
|
---|
| 150 | this._ensureInnerSourceMapBuffer();
|
---|
| 151 | return [
|
---|
| 152 | this._valueAsBuffer,
|
---|
| 153 | this._name,
|
---|
| 154 | this._sourceMapAsBuffer,
|
---|
| 155 | this._originalSourceAsBuffer,
|
---|
| 156 | this._innerSourceMapAsBuffer,
|
---|
| 157 | this._removeOriginalSource
|
---|
| 158 | ];
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | buffer() {
|
---|
| 162 | this._ensureValueBuffer();
|
---|
| 163 | return this._valueAsBuffer;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | source() {
|
---|
| 167 | this._ensureValueString();
|
---|
| 168 | return this._valueAsString;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | map(options) {
|
---|
| 172 | if (!this._hasInnerSourceMap) {
|
---|
| 173 | this._ensureSourceMapObject();
|
---|
| 174 | return this._sourceMapAsObject;
|
---|
| 175 | }
|
---|
| 176 | return getMap(this, options);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | sourceAndMap(options) {
|
---|
| 180 | if (!this._hasInnerSourceMap) {
|
---|
| 181 | this._ensureValueString();
|
---|
| 182 | this._ensureSourceMapObject();
|
---|
| 183 | return {
|
---|
| 184 | source: this._valueAsString,
|
---|
| 185 | map: this._sourceMapAsObject
|
---|
| 186 | };
|
---|
| 187 | }
|
---|
| 188 | return getSourceAndMap(this, options);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | streamChunks(options, onChunk, onSource, onName) {
|
---|
| 192 | this._ensureValueString();
|
---|
| 193 | this._ensureSourceMapObject();
|
---|
| 194 | this._ensureOriginalSourceString();
|
---|
| 195 | if (this._hasInnerSourceMap) {
|
---|
| 196 | this._ensureInnerSourceMapObject();
|
---|
| 197 | return streamChunksOfCombinedSourceMap(
|
---|
| 198 | this._valueAsString,
|
---|
| 199 | this._sourceMapAsObject,
|
---|
| 200 | this._name,
|
---|
| 201 | this._originalSourceAsString,
|
---|
| 202 | this._innerSourceMapAsObject,
|
---|
| 203 | this._removeOriginalSource,
|
---|
| 204 | onChunk,
|
---|
| 205 | onSource,
|
---|
| 206 | onName,
|
---|
| 207 | !!(options && options.finalSource),
|
---|
| 208 | !!(options && options.columns !== false)
|
---|
| 209 | );
|
---|
| 210 | } else {
|
---|
| 211 | return streamChunksOfSourceMap(
|
---|
| 212 | this._valueAsString,
|
---|
| 213 | this._sourceMapAsObject,
|
---|
| 214 | onChunk,
|
---|
| 215 | onSource,
|
---|
| 216 | onName,
|
---|
| 217 | !!(options && options.finalSource),
|
---|
| 218 | !!(options && options.columns !== false)
|
---|
| 219 | );
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | updateHash(hash) {
|
---|
| 224 | this._ensureValueBuffer();
|
---|
| 225 | this._ensureSourceMapBuffer();
|
---|
| 226 | this._ensureOriginalSourceBuffer();
|
---|
| 227 | this._ensureInnerSourceMapBuffer();
|
---|
| 228 |
|
---|
| 229 | hash.update("SourceMapSource");
|
---|
| 230 |
|
---|
| 231 | hash.update(this._valueAsBuffer);
|
---|
| 232 |
|
---|
| 233 | hash.update(this._sourceMapAsBuffer);
|
---|
| 234 |
|
---|
| 235 | if (this._hasOriginalSource) {
|
---|
| 236 | hash.update(this._originalSourceAsBuffer);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | if (this._hasInnerSourceMap) {
|
---|
| 240 | hash.update(this._innerSourceMapAsBuffer);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | hash.update(this._removeOriginalSource ? "true" : "false");
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | module.exports = SourceMapSource;
|
---|