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