@depup/webpack-sources 3.3.4-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/lib/CachedSource.js +419 -0
- package/lib/CompatSource.js +109 -0
- package/lib/ConcatSource.js +398 -0
- package/lib/OriginalSource.js +202 -0
- package/lib/PrefixSource.js +162 -0
- package/lib/RawSource.js +140 -0
- package/lib/ReplaceSource.js +557 -0
- package/lib/SizeOnlySource.js +71 -0
- package/lib/Source.js +89 -0
- package/lib/SourceMapSource.js +367 -0
- package/lib/helpers/createMappingsSerializer.js +225 -0
- package/lib/helpers/getFromStreamChunks.js +159 -0
- package/lib/helpers/getGeneratedSourceInfo.js +44 -0
- package/lib/helpers/getName.js +21 -0
- package/lib/helpers/getSource.js +24 -0
- package/lib/helpers/readMappings.js +120 -0
- package/lib/helpers/splitIntoLines.js +33 -0
- package/lib/helpers/splitIntoPotentialTokens.js +53 -0
- package/lib/helpers/streamAndGetSourceAndMap.js +123 -0
- package/lib/helpers/streamChunks.js +62 -0
- package/lib/helpers/streamChunksOfCombinedSourceMap.js +366 -0
- package/lib/helpers/streamChunksOfRawSource.js +54 -0
- package/lib/helpers/streamChunksOfSourceMap.js +499 -0
- package/lib/helpers/stringBufferUtils.js +117 -0
- package/lib/index.js +120 -0
- package/package.json +71 -0
- package/types.d.ts +461 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const createMappingsSerializer = require("./createMappingsSerializer");
|
|
9
|
+
|
|
10
|
+
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
|
|
11
|
+
/** @typedef {import("../Source").SourceAndMap} SourceAndMap */
|
|
12
|
+
/** @typedef {import("./streamChunks").Options} Options */
|
|
13
|
+
/** @typedef {import("./streamChunks").StreamChunksFunction} StreamChunksFunction */
|
|
14
|
+
|
|
15
|
+
/** @typedef {{ streamChunks: StreamChunksFunction }} SourceLikeWithStreamChunks */
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {SourceLikeWithStreamChunks} source source
|
|
19
|
+
* @param {Options=} options options
|
|
20
|
+
* @returns {RawSourceMap | null} map
|
|
21
|
+
*/
|
|
22
|
+
module.exports.getMap = (source, options) => {
|
|
23
|
+
let mappings = "";
|
|
24
|
+
/** @type {(string | null)[]} */
|
|
25
|
+
const potentialSources = [];
|
|
26
|
+
/** @type {(string | null)[]} */
|
|
27
|
+
const potentialSourcesContent = [];
|
|
28
|
+
/** @type {(string | null)[]} */
|
|
29
|
+
const potentialNames = [];
|
|
30
|
+
const addMapping = createMappingsSerializer(options);
|
|
31
|
+
source.streamChunks(
|
|
32
|
+
{ ...options, source: false, finalSource: true },
|
|
33
|
+
(
|
|
34
|
+
chunk,
|
|
35
|
+
generatedLine,
|
|
36
|
+
generatedColumn,
|
|
37
|
+
sourceIndex,
|
|
38
|
+
originalLine,
|
|
39
|
+
originalColumn,
|
|
40
|
+
nameIndex,
|
|
41
|
+
) => {
|
|
42
|
+
mappings += addMapping(
|
|
43
|
+
generatedLine,
|
|
44
|
+
generatedColumn,
|
|
45
|
+
sourceIndex,
|
|
46
|
+
originalLine,
|
|
47
|
+
originalColumn,
|
|
48
|
+
nameIndex,
|
|
49
|
+
);
|
|
50
|
+
},
|
|
51
|
+
(sourceIndex, source, sourceContent) => {
|
|
52
|
+
while (potentialSources.length < sourceIndex) {
|
|
53
|
+
potentialSources.push(null);
|
|
54
|
+
}
|
|
55
|
+
potentialSources[sourceIndex] = source;
|
|
56
|
+
if (sourceContent !== undefined) {
|
|
57
|
+
while (potentialSourcesContent.length < sourceIndex) {
|
|
58
|
+
potentialSourcesContent.push(null);
|
|
59
|
+
}
|
|
60
|
+
potentialSourcesContent[sourceIndex] = sourceContent;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
(nameIndex, name) => {
|
|
64
|
+
while (potentialNames.length < nameIndex) {
|
|
65
|
+
potentialNames.push(null);
|
|
66
|
+
}
|
|
67
|
+
potentialNames[nameIndex] = name;
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
return mappings.length > 0
|
|
71
|
+
? {
|
|
72
|
+
version: 3,
|
|
73
|
+
file: "x",
|
|
74
|
+
mappings,
|
|
75
|
+
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
|
|
76
|
+
sources: /** @type {string[]} */ (potentialSources),
|
|
77
|
+
sourcesContent:
|
|
78
|
+
potentialSourcesContent.length > 0
|
|
79
|
+
? /** @type {string[]} */ (potentialSourcesContent)
|
|
80
|
+
: undefined,
|
|
81
|
+
names: /** @type {string[]} */ (potentialNames),
|
|
82
|
+
}
|
|
83
|
+
: null;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {SourceLikeWithStreamChunks} inputSource input source
|
|
88
|
+
* @param {Options=} options options
|
|
89
|
+
* @returns {SourceAndMap} map
|
|
90
|
+
*/
|
|
91
|
+
module.exports.getSourceAndMap = (inputSource, options) => {
|
|
92
|
+
let code = "";
|
|
93
|
+
let mappings = "";
|
|
94
|
+
/** @type {(string | null)[]} */
|
|
95
|
+
const potentialSources = [];
|
|
96
|
+
/** @type {(string | null)[]} */
|
|
97
|
+
const potentialSourcesContent = [];
|
|
98
|
+
/** @type {(string | null)[]} */
|
|
99
|
+
const potentialNames = [];
|
|
100
|
+
const addMapping = createMappingsSerializer(options);
|
|
101
|
+
const { source } = inputSource.streamChunks(
|
|
102
|
+
{ ...options, finalSource: true },
|
|
103
|
+
(
|
|
104
|
+
chunk,
|
|
105
|
+
generatedLine,
|
|
106
|
+
generatedColumn,
|
|
107
|
+
sourceIndex,
|
|
108
|
+
originalLine,
|
|
109
|
+
originalColumn,
|
|
110
|
+
nameIndex,
|
|
111
|
+
) => {
|
|
112
|
+
if (chunk !== undefined) code += chunk;
|
|
113
|
+
mappings += addMapping(
|
|
114
|
+
generatedLine,
|
|
115
|
+
generatedColumn,
|
|
116
|
+
sourceIndex,
|
|
117
|
+
originalLine,
|
|
118
|
+
originalColumn,
|
|
119
|
+
nameIndex,
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
(sourceIndex, source, sourceContent) => {
|
|
123
|
+
while (potentialSources.length < sourceIndex) {
|
|
124
|
+
potentialSources.push(null);
|
|
125
|
+
}
|
|
126
|
+
potentialSources[sourceIndex] = source;
|
|
127
|
+
if (sourceContent !== undefined) {
|
|
128
|
+
while (potentialSourcesContent.length < sourceIndex) {
|
|
129
|
+
potentialSourcesContent.push(null);
|
|
130
|
+
}
|
|
131
|
+
potentialSourcesContent[sourceIndex] = sourceContent;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
(nameIndex, name) => {
|
|
135
|
+
while (potentialNames.length < nameIndex) {
|
|
136
|
+
potentialNames.push(null);
|
|
137
|
+
}
|
|
138
|
+
potentialNames[nameIndex] = name;
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
return {
|
|
142
|
+
source: source !== undefined ? source : code,
|
|
143
|
+
map:
|
|
144
|
+
mappings.length > 0
|
|
145
|
+
? {
|
|
146
|
+
version: 3,
|
|
147
|
+
file: "x",
|
|
148
|
+
mappings,
|
|
149
|
+
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
|
|
150
|
+
sources: /** @type {string[]} */ (potentialSources),
|
|
151
|
+
sourcesContent:
|
|
152
|
+
potentialSourcesContent.length > 0
|
|
153
|
+
? /** @type {string[]} */ (potentialSourcesContent)
|
|
154
|
+
: undefined,
|
|
155
|
+
names: /** @type {string[]} */ (potentialNames),
|
|
156
|
+
}
|
|
157
|
+
: null,
|
|
158
|
+
};
|
|
159
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const CHAR_CODE_NEW_LINE = "\n".charCodeAt(0);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} GeneratedSourceInfo
|
|
12
|
+
* @property {number=} generatedLine generated line
|
|
13
|
+
* @property {number=} generatedColumn generated column
|
|
14
|
+
* @property {string=} source source
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string | undefined} source source
|
|
19
|
+
* @returns {GeneratedSourceInfo} source info
|
|
20
|
+
*/
|
|
21
|
+
const getGeneratedSourceInfo = (source) => {
|
|
22
|
+
if (source === undefined) {
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
const lastLineStart = source.lastIndexOf("\n");
|
|
26
|
+
if (lastLineStart === -1) {
|
|
27
|
+
return {
|
|
28
|
+
generatedLine: 1,
|
|
29
|
+
generatedColumn: source.length,
|
|
30
|
+
source,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
let generatedLine = 2;
|
|
34
|
+
for (let i = 0; i < lastLineStart; i++) {
|
|
35
|
+
if (source.charCodeAt(i) === CHAR_CODE_NEW_LINE) generatedLine++;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
generatedLine,
|
|
39
|
+
generatedColumn: source.length - lastLineStart - 1,
|
|
40
|
+
source,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports = getGeneratedSourceInfo;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {RawSourceMap} sourceMap source map
|
|
12
|
+
* @param {number} index index
|
|
13
|
+
* @returns {string | undefined | null} name
|
|
14
|
+
*/
|
|
15
|
+
const getName = (sourceMap, index) => {
|
|
16
|
+
if (index < 0) return null;
|
|
17
|
+
const { names } = sourceMap;
|
|
18
|
+
return names[index];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports = getName;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {RawSourceMap} sourceMap source map
|
|
12
|
+
* @param {number} index index
|
|
13
|
+
* @returns {string | null} name
|
|
14
|
+
*/
|
|
15
|
+
const getSource = (sourceMap, index) => {
|
|
16
|
+
if (index < 0) return null;
|
|
17
|
+
const { sourceRoot, sources } = sourceMap;
|
|
18
|
+
const source = sources[index];
|
|
19
|
+
if (!sourceRoot) return source;
|
|
20
|
+
if (sourceRoot.endsWith("/")) return sourceRoot + source;
|
|
21
|
+
return `${sourceRoot}/${source}`;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = getSource;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const ALPHABET =
|
|
9
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
10
|
+
|
|
11
|
+
const CONTINUATION_BIT = 0x20;
|
|
12
|
+
const END_SEGMENT_BIT = 0x40;
|
|
13
|
+
const NEXT_LINE = END_SEGMENT_BIT | 0x01;
|
|
14
|
+
const INVALID = END_SEGMENT_BIT | 0x02;
|
|
15
|
+
const DATA_MASK = 0x1f;
|
|
16
|
+
|
|
17
|
+
const ccToValue = new Uint8Array("z".charCodeAt(0) + 1);
|
|
18
|
+
|
|
19
|
+
ccToValue.fill(INVALID);
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
22
|
+
ccToValue[ALPHABET.charCodeAt(i)] = i;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
|
|
26
|
+
ccToValue[";".charCodeAt(0)] = NEXT_LINE;
|
|
27
|
+
|
|
28
|
+
const ccMax = ccToValue.length - 1;
|
|
29
|
+
|
|
30
|
+
/** @typedef {(generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnMapping */
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} mappings the mappings string
|
|
34
|
+
* @param {OnMapping} onMapping called for each mapping
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
const readMappings = (mappings, onMapping) => {
|
|
38
|
+
// generatedColumn, [sourceIndex, originalLine, orignalColumn, [nameIndex]]
|
|
39
|
+
const currentData = new Uint32Array([0, 0, 1, 0, 0]);
|
|
40
|
+
let currentDataPos = 0;
|
|
41
|
+
// currentValue will include a sign bit at bit 0
|
|
42
|
+
let currentValue = 0;
|
|
43
|
+
let currentValuePos = 0;
|
|
44
|
+
let generatedLine = 1;
|
|
45
|
+
let generatedColumn = -1;
|
|
46
|
+
for (let i = 0; i < mappings.length; i++) {
|
|
47
|
+
const cc = mappings.charCodeAt(i);
|
|
48
|
+
if (cc > ccMax) continue;
|
|
49
|
+
const value = ccToValue[cc];
|
|
50
|
+
if ((value & END_SEGMENT_BIT) !== 0) {
|
|
51
|
+
// End current segment
|
|
52
|
+
if (currentData[0] > generatedColumn) {
|
|
53
|
+
if (currentDataPos === 1) {
|
|
54
|
+
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
|
|
55
|
+
} else if (currentDataPos === 4) {
|
|
56
|
+
onMapping(
|
|
57
|
+
generatedLine,
|
|
58
|
+
currentData[0],
|
|
59
|
+
currentData[1],
|
|
60
|
+
currentData[2],
|
|
61
|
+
currentData[3],
|
|
62
|
+
-1,
|
|
63
|
+
);
|
|
64
|
+
} else if (currentDataPos === 5) {
|
|
65
|
+
onMapping(
|
|
66
|
+
generatedLine,
|
|
67
|
+
currentData[0],
|
|
68
|
+
currentData[1],
|
|
69
|
+
currentData[2],
|
|
70
|
+
currentData[3],
|
|
71
|
+
currentData[4],
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
[generatedColumn] = currentData;
|
|
75
|
+
}
|
|
76
|
+
currentDataPos = 0;
|
|
77
|
+
if (value === NEXT_LINE) {
|
|
78
|
+
// Start new line
|
|
79
|
+
generatedLine++;
|
|
80
|
+
currentData[0] = 0;
|
|
81
|
+
generatedColumn = -1;
|
|
82
|
+
}
|
|
83
|
+
} else if ((value & CONTINUATION_BIT) === 0) {
|
|
84
|
+
// last sextet
|
|
85
|
+
currentValue |= value << currentValuePos;
|
|
86
|
+
const finalValue =
|
|
87
|
+
currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
|
|
88
|
+
currentData[currentDataPos++] += finalValue;
|
|
89
|
+
currentValuePos = 0;
|
|
90
|
+
currentValue = 0;
|
|
91
|
+
} else {
|
|
92
|
+
currentValue |= (value & DATA_MASK) << currentValuePos;
|
|
93
|
+
currentValuePos += 5;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// End current segment
|
|
97
|
+
if (currentDataPos === 1) {
|
|
98
|
+
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
|
|
99
|
+
} else if (currentDataPos === 4) {
|
|
100
|
+
onMapping(
|
|
101
|
+
generatedLine,
|
|
102
|
+
currentData[0],
|
|
103
|
+
currentData[1],
|
|
104
|
+
currentData[2],
|
|
105
|
+
currentData[3],
|
|
106
|
+
-1,
|
|
107
|
+
);
|
|
108
|
+
} else if (currentDataPos === 5) {
|
|
109
|
+
onMapping(
|
|
110
|
+
generatedLine,
|
|
111
|
+
currentData[0],
|
|
112
|
+
currentData[1],
|
|
113
|
+
currentData[2],
|
|
114
|
+
currentData[3],
|
|
115
|
+
currentData[4],
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
module.exports = readMappings;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} str string
|
|
10
|
+
* @returns {string[]} array of string separated by lines
|
|
11
|
+
*/
|
|
12
|
+
const splitIntoLines = (str) => {
|
|
13
|
+
const results = [];
|
|
14
|
+
const len = str.length;
|
|
15
|
+
let i = 0;
|
|
16
|
+
while (i < len) {
|
|
17
|
+
const cc = str.charCodeAt(i);
|
|
18
|
+
// 10 is "\n".charCodeAt(0)
|
|
19
|
+
if (cc === 10) {
|
|
20
|
+
results.push("\n");
|
|
21
|
+
i++;
|
|
22
|
+
} else {
|
|
23
|
+
let j = i + 1;
|
|
24
|
+
// 10 is "\n".charCodeAt(0)
|
|
25
|
+
while (j < len && str.charCodeAt(j) !== 10) j++;
|
|
26
|
+
results.push(str.slice(i, j + 1));
|
|
27
|
+
i = j + 1;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return results;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports = splitIntoLines;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
// \n = 10
|
|
9
|
+
// ; = 59
|
|
10
|
+
// { = 123
|
|
11
|
+
// } = 125
|
|
12
|
+
// <space> = 32
|
|
13
|
+
// \r = 13
|
|
14
|
+
// \t = 9
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} str string
|
|
18
|
+
* @returns {string[] | null} array of string separated by potential tokens
|
|
19
|
+
*/
|
|
20
|
+
const splitIntoPotentialTokens = (str) => {
|
|
21
|
+
const len = str.length;
|
|
22
|
+
if (len === 0) return null;
|
|
23
|
+
const results = [];
|
|
24
|
+
let i = 0;
|
|
25
|
+
while (i < len) {
|
|
26
|
+
const start = i;
|
|
27
|
+
block: {
|
|
28
|
+
let cc = str.charCodeAt(i);
|
|
29
|
+
while (cc !== 10 && cc !== 59 && cc !== 123 && cc !== 125) {
|
|
30
|
+
if (++i >= len) break block;
|
|
31
|
+
cc = str.charCodeAt(i);
|
|
32
|
+
}
|
|
33
|
+
while (
|
|
34
|
+
cc === 59 ||
|
|
35
|
+
cc === 32 ||
|
|
36
|
+
cc === 123 ||
|
|
37
|
+
cc === 125 ||
|
|
38
|
+
cc === 13 ||
|
|
39
|
+
cc === 9
|
|
40
|
+
) {
|
|
41
|
+
if (++i >= len) break block;
|
|
42
|
+
cc = str.charCodeAt(i);
|
|
43
|
+
}
|
|
44
|
+
if (cc === 10) {
|
|
45
|
+
i++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
results.push(str.slice(start, i));
|
|
49
|
+
}
|
|
50
|
+
return results;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
module.exports = splitIntoPotentialTokens;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const createMappingsSerializer = require("./createMappingsSerializer");
|
|
9
|
+
const streamChunks = require("./streamChunks");
|
|
10
|
+
|
|
11
|
+
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
|
|
12
|
+
/** @typedef {import("./streamChunks").GeneratedSourceInfo} GeneratedSourceInfo */
|
|
13
|
+
/** @typedef {import("./streamChunks").OnChunk} OnChunk */
|
|
14
|
+
/** @typedef {import("./streamChunks").OnName} OnName */
|
|
15
|
+
/** @typedef {import("./streamChunks").OnSource} OnSource */
|
|
16
|
+
/** @typedef {import("./streamChunks").Options} Options */
|
|
17
|
+
/** @typedef {import("./streamChunks").SourceMaybeWithStreamChunksFunction} SourceMaybeWithStreamChunksFunction */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {SourceMaybeWithStreamChunksFunction} inputSource input source
|
|
21
|
+
* @param {Options} options options
|
|
22
|
+
* @param {OnChunk} onChunk on chunk
|
|
23
|
+
* @param {OnSource} onSource on source
|
|
24
|
+
* @param {OnName} onName on name
|
|
25
|
+
* @returns {{ result: GeneratedSourceInfo, source: string, map: RawSourceMap | null }} result
|
|
26
|
+
*/
|
|
27
|
+
const streamAndGetSourceAndMap = (
|
|
28
|
+
inputSource,
|
|
29
|
+
options,
|
|
30
|
+
onChunk,
|
|
31
|
+
onSource,
|
|
32
|
+
onName,
|
|
33
|
+
) => {
|
|
34
|
+
let code = "";
|
|
35
|
+
let mappings = "";
|
|
36
|
+
/** @type {(string | null)[]} */
|
|
37
|
+
const potentialSources = [];
|
|
38
|
+
/** @type {(string | null)[]} */
|
|
39
|
+
const potentialSourcesContent = [];
|
|
40
|
+
/** @type {(string | null)[]} */
|
|
41
|
+
const potentialNames = [];
|
|
42
|
+
const addMapping = createMappingsSerializer({ ...options, columns: true });
|
|
43
|
+
const finalSource = Boolean(options && options.finalSource);
|
|
44
|
+
const { generatedLine, generatedColumn, source } = streamChunks(
|
|
45
|
+
inputSource,
|
|
46
|
+
options,
|
|
47
|
+
(
|
|
48
|
+
chunk,
|
|
49
|
+
generatedLine,
|
|
50
|
+
generatedColumn,
|
|
51
|
+
sourceIndex,
|
|
52
|
+
originalLine,
|
|
53
|
+
originalColumn,
|
|
54
|
+
nameIndex,
|
|
55
|
+
) => {
|
|
56
|
+
if (chunk !== undefined) code += chunk;
|
|
57
|
+
mappings += addMapping(
|
|
58
|
+
generatedLine,
|
|
59
|
+
generatedColumn,
|
|
60
|
+
sourceIndex,
|
|
61
|
+
originalLine,
|
|
62
|
+
originalColumn,
|
|
63
|
+
nameIndex,
|
|
64
|
+
);
|
|
65
|
+
return onChunk(
|
|
66
|
+
finalSource ? undefined : chunk,
|
|
67
|
+
generatedLine,
|
|
68
|
+
generatedColumn,
|
|
69
|
+
sourceIndex,
|
|
70
|
+
originalLine,
|
|
71
|
+
originalColumn,
|
|
72
|
+
nameIndex,
|
|
73
|
+
);
|
|
74
|
+
},
|
|
75
|
+
(sourceIndex, source, sourceContent) => {
|
|
76
|
+
while (potentialSources.length < sourceIndex) {
|
|
77
|
+
potentialSources.push(null);
|
|
78
|
+
}
|
|
79
|
+
potentialSources[sourceIndex] = source;
|
|
80
|
+
if (sourceContent !== undefined) {
|
|
81
|
+
while (potentialSourcesContent.length < sourceIndex) {
|
|
82
|
+
potentialSourcesContent.push(null);
|
|
83
|
+
}
|
|
84
|
+
potentialSourcesContent[sourceIndex] = sourceContent;
|
|
85
|
+
}
|
|
86
|
+
return onSource(sourceIndex, source, sourceContent);
|
|
87
|
+
},
|
|
88
|
+
(nameIndex, name) => {
|
|
89
|
+
while (potentialNames.length < nameIndex) {
|
|
90
|
+
potentialNames.push(null);
|
|
91
|
+
}
|
|
92
|
+
potentialNames[nameIndex] = name;
|
|
93
|
+
return onName(nameIndex, name);
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
const resultSource = source !== undefined ? source : code;
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
result: {
|
|
100
|
+
generatedLine,
|
|
101
|
+
generatedColumn,
|
|
102
|
+
source: finalSource ? resultSource : undefined,
|
|
103
|
+
},
|
|
104
|
+
source: resultSource,
|
|
105
|
+
map:
|
|
106
|
+
mappings.length > 0
|
|
107
|
+
? {
|
|
108
|
+
version: 3,
|
|
109
|
+
file: "x",
|
|
110
|
+
mappings,
|
|
111
|
+
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
|
|
112
|
+
sources: /** @type {string[]} */ (potentialSources),
|
|
113
|
+
sourcesContent:
|
|
114
|
+
potentialSourcesContent.length > 0
|
|
115
|
+
? /** @type {string[]} */ (potentialSourcesContent)
|
|
116
|
+
: undefined,
|
|
117
|
+
names: /** @type {string[]} */ (potentialNames),
|
|
118
|
+
}
|
|
119
|
+
: null,
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
module.exports = streamAndGetSourceAndMap;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const streamChunksOfRawSource = require("./streamChunksOfRawSource");
|
|
9
|
+
const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
|
|
10
|
+
|
|
11
|
+
/** @typedef {import("../Source")} Source */
|
|
12
|
+
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
|
|
13
|
+
/** @typedef {(chunk: string | undefined, generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnChunk */
|
|
14
|
+
/** @typedef {(sourceIndex: number, source: string | null, sourceContent: string | undefined) => void} OnSource */
|
|
15
|
+
/** @typedef {(nameIndex: number, name: string) => void} OnName */
|
|
16
|
+
|
|
17
|
+
/** @typedef {{ source?: boolean, finalSource?: boolean, columns?: boolean }} Options */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @callback StreamChunksFunction
|
|
21
|
+
* @param {Options} options options
|
|
22
|
+
* @param {OnChunk} onChunk on chunk
|
|
23
|
+
* @param {OnSource} onSource on source
|
|
24
|
+
* @param {OnName} onName on name
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** @typedef {Source & { streamChunks?: StreamChunksFunction }} SourceMaybeWithStreamChunksFunction */
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {SourceMaybeWithStreamChunksFunction} source source
|
|
31
|
+
* @param {Options} options options
|
|
32
|
+
* @param {OnChunk} onChunk on chunk
|
|
33
|
+
* @param {OnSource} onSource on source
|
|
34
|
+
* @param {OnName} onName on name
|
|
35
|
+
* @returns {GeneratedSourceInfo} generated source info
|
|
36
|
+
*/
|
|
37
|
+
module.exports = (source, options, onChunk, onSource, onName) => {
|
|
38
|
+
if (typeof source.streamChunks === "function") {
|
|
39
|
+
return source.streamChunks(options, onChunk, onSource, onName);
|
|
40
|
+
}
|
|
41
|
+
const sourceAndMap = source.sourceAndMap(options);
|
|
42
|
+
if (sourceAndMap.map) {
|
|
43
|
+
return streamChunksOfSourceMap(
|
|
44
|
+
/** @type {string} */
|
|
45
|
+
(sourceAndMap.source),
|
|
46
|
+
sourceAndMap.map,
|
|
47
|
+
onChunk,
|
|
48
|
+
onSource,
|
|
49
|
+
onName,
|
|
50
|
+
Boolean(options && options.finalSource),
|
|
51
|
+
Boolean(options && options.columns !== false),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return streamChunksOfRawSource(
|
|
55
|
+
/** @type {string} */
|
|
56
|
+
(sourceAndMap.source),
|
|
57
|
+
onChunk,
|
|
58
|
+
onSource,
|
|
59
|
+
onName,
|
|
60
|
+
Boolean(options && options.finalSource),
|
|
61
|
+
);
|
|
62
|
+
};
|