[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 RawSource = require("./RawSource");
|
---|
| 9 | const streamChunks = require("./helpers/streamChunks");
|
---|
| 10 | const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
|
---|
| 11 |
|
---|
| 12 | const stringsAsRawSources = new WeakSet();
|
---|
| 13 |
|
---|
| 14 | class ConcatSource extends Source {
|
---|
| 15 | constructor() {
|
---|
| 16 | super();
|
---|
| 17 | this._children = [];
|
---|
| 18 | for (let i = 0; i < arguments.length; i++) {
|
---|
| 19 | const item = arguments[i];
|
---|
| 20 | if (item instanceof ConcatSource) {
|
---|
| 21 | for (const child of item._children) {
|
---|
| 22 | this._children.push(child);
|
---|
| 23 | }
|
---|
| 24 | } else {
|
---|
| 25 | this._children.push(item);
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | this._isOptimized = arguments.length === 0;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | getChildren() {
|
---|
| 32 | if (!this._isOptimized) this._optimize();
|
---|
| 33 | return this._children;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | add(item) {
|
---|
| 37 | if (item instanceof ConcatSource) {
|
---|
| 38 | for (const child of item._children) {
|
---|
| 39 | this._children.push(child);
|
---|
| 40 | }
|
---|
| 41 | } else {
|
---|
| 42 | this._children.push(item);
|
---|
| 43 | }
|
---|
| 44 | this._isOptimized = false;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | addAllSkipOptimizing(items) {
|
---|
| 48 | for (const item of items) {
|
---|
| 49 | this._children.push(item);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | buffer() {
|
---|
| 54 | if (!this._isOptimized) this._optimize();
|
---|
| 55 | const buffers = [];
|
---|
| 56 | for (const child of this._children) {
|
---|
| 57 | if (typeof child.buffer === "function") {
|
---|
| 58 | buffers.push(child.buffer());
|
---|
| 59 | } else {
|
---|
| 60 | const bufferOrString = child.source();
|
---|
| 61 | if (Buffer.isBuffer(bufferOrString)) {
|
---|
| 62 | buffers.push(bufferOrString);
|
---|
| 63 | } else {
|
---|
| 64 | // This will not happen
|
---|
| 65 | buffers.push(Buffer.from(bufferOrString, "utf-8"));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | return Buffer.concat(buffers);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | source() {
|
---|
| 73 | if (!this._isOptimized) this._optimize();
|
---|
| 74 | let source = "";
|
---|
| 75 | for (const child of this._children) {
|
---|
| 76 | source += child.source();
|
---|
| 77 | }
|
---|
| 78 | return source;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | size() {
|
---|
| 82 | if (!this._isOptimized) this._optimize();
|
---|
| 83 | let size = 0;
|
---|
| 84 | for (const child of this._children) {
|
---|
| 85 | size += child.size();
|
---|
| 86 | }
|
---|
| 87 | return size;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | map(options) {
|
---|
| 91 | return getMap(this, options);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | sourceAndMap(options) {
|
---|
| 95 | return getSourceAndMap(this, options);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | streamChunks(options, onChunk, onSource, onName) {
|
---|
| 99 | if (!this._isOptimized) this._optimize();
|
---|
| 100 | if (this._children.length === 1)
|
---|
| 101 | return this._children[0].streamChunks(options, onChunk, onSource, onName);
|
---|
| 102 | let currentLineOffset = 0;
|
---|
| 103 | let currentColumnOffset = 0;
|
---|
| 104 | let sourceMapping = new Map();
|
---|
| 105 | let nameMapping = new Map();
|
---|
| 106 | const finalSource = !!(options && options.finalSource);
|
---|
| 107 | let code = "";
|
---|
| 108 | let needToCloseMapping = false;
|
---|
| 109 | for (const item of this._children) {
|
---|
| 110 | const sourceIndexMapping = [];
|
---|
| 111 | const nameIndexMapping = [];
|
---|
| 112 | let lastMappingLine = 0;
|
---|
| 113 | const { generatedLine, generatedColumn, source } = streamChunks(
|
---|
| 114 | item,
|
---|
| 115 | options,
|
---|
| 116 | // eslint-disable-next-line no-loop-func
|
---|
| 117 | (
|
---|
| 118 | chunk,
|
---|
| 119 | generatedLine,
|
---|
| 120 | generatedColumn,
|
---|
| 121 | sourceIndex,
|
---|
| 122 | originalLine,
|
---|
| 123 | originalColumn,
|
---|
| 124 | nameIndex
|
---|
| 125 | ) => {
|
---|
| 126 | const line = generatedLine + currentLineOffset;
|
---|
| 127 | const column =
|
---|
| 128 | generatedLine === 1
|
---|
| 129 | ? generatedColumn + currentColumnOffset
|
---|
| 130 | : generatedColumn;
|
---|
| 131 | if (needToCloseMapping) {
|
---|
| 132 | if (generatedLine !== 1 || generatedColumn !== 0) {
|
---|
| 133 | onChunk(
|
---|
| 134 | undefined,
|
---|
| 135 | currentLineOffset + 1,
|
---|
| 136 | currentColumnOffset,
|
---|
| 137 | -1,
|
---|
| 138 | -1,
|
---|
| 139 | -1,
|
---|
| 140 | -1
|
---|
| 141 | );
|
---|
| 142 | }
|
---|
| 143 | needToCloseMapping = false;
|
---|
| 144 | }
|
---|
| 145 | const resultSourceIndex =
|
---|
| 146 | sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
|
---|
| 147 | ? -1
|
---|
| 148 | : sourceIndexMapping[sourceIndex];
|
---|
| 149 | const resultNameIndex =
|
---|
| 150 | nameIndex < 0 || nameIndex >= nameIndexMapping.length
|
---|
| 151 | ? -1
|
---|
| 152 | : nameIndexMapping[nameIndex];
|
---|
| 153 | lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine;
|
---|
| 154 | if (finalSource) {
|
---|
| 155 | if (chunk !== undefined) code += chunk;
|
---|
| 156 | if (resultSourceIndex >= 0) {
|
---|
| 157 | onChunk(
|
---|
| 158 | undefined,
|
---|
| 159 | line,
|
---|
| 160 | column,
|
---|
| 161 | resultSourceIndex,
|
---|
| 162 | originalLine,
|
---|
| 163 | originalColumn,
|
---|
| 164 | resultNameIndex
|
---|
| 165 | );
|
---|
| 166 | }
|
---|
| 167 | } else {
|
---|
| 168 | if (resultSourceIndex < 0) {
|
---|
| 169 | onChunk(chunk, line, column, -1, -1, -1, -1);
|
---|
| 170 | } else {
|
---|
| 171 | onChunk(
|
---|
| 172 | chunk,
|
---|
| 173 | line,
|
---|
| 174 | column,
|
---|
| 175 | resultSourceIndex,
|
---|
| 176 | originalLine,
|
---|
| 177 | originalColumn,
|
---|
| 178 | resultNameIndex
|
---|
| 179 | );
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | },
|
---|
| 183 | (i, source, sourceContent) => {
|
---|
| 184 | let globalIndex = sourceMapping.get(source);
|
---|
| 185 | if (globalIndex === undefined) {
|
---|
| 186 | sourceMapping.set(source, (globalIndex = sourceMapping.size));
|
---|
| 187 | onSource(globalIndex, source, sourceContent);
|
---|
| 188 | }
|
---|
| 189 | sourceIndexMapping[i] = globalIndex;
|
---|
| 190 | },
|
---|
| 191 | (i, name) => {
|
---|
| 192 | let globalIndex = nameMapping.get(name);
|
---|
| 193 | if (globalIndex === undefined) {
|
---|
| 194 | nameMapping.set(name, (globalIndex = nameMapping.size));
|
---|
| 195 | onName(globalIndex, name);
|
---|
| 196 | }
|
---|
| 197 | nameIndexMapping[i] = globalIndex;
|
---|
| 198 | }
|
---|
| 199 | );
|
---|
| 200 | if (source !== undefined) code += source;
|
---|
| 201 | if (needToCloseMapping) {
|
---|
| 202 | if (generatedLine !== 1 || generatedColumn !== 0) {
|
---|
| 203 | onChunk(
|
---|
| 204 | undefined,
|
---|
| 205 | currentLineOffset + 1,
|
---|
| 206 | currentColumnOffset,
|
---|
| 207 | -1,
|
---|
| 208 | -1,
|
---|
| 209 | -1,
|
---|
| 210 | -1
|
---|
| 211 | );
|
---|
| 212 | needToCloseMapping = false;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | if (generatedLine > 1) {
|
---|
| 216 | currentColumnOffset = generatedColumn;
|
---|
| 217 | } else {
|
---|
| 218 | currentColumnOffset += generatedColumn;
|
---|
| 219 | }
|
---|
| 220 | needToCloseMapping =
|
---|
| 221 | needToCloseMapping ||
|
---|
| 222 | (finalSource && lastMappingLine === generatedLine);
|
---|
| 223 | currentLineOffset += generatedLine - 1;
|
---|
| 224 | }
|
---|
| 225 | return {
|
---|
| 226 | generatedLine: currentLineOffset + 1,
|
---|
| 227 | generatedColumn: currentColumnOffset,
|
---|
| 228 | source: finalSource ? code : undefined
|
---|
| 229 | };
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | updateHash(hash) {
|
---|
| 233 | if (!this._isOptimized) this._optimize();
|
---|
| 234 | hash.update("ConcatSource");
|
---|
| 235 | for (const item of this._children) {
|
---|
| 236 | item.updateHash(hash);
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | _optimize() {
|
---|
| 241 | const newChildren = [];
|
---|
| 242 | let currentString = undefined;
|
---|
| 243 | let currentRawSources = undefined;
|
---|
| 244 | const addStringToRawSources = string => {
|
---|
| 245 | if (currentRawSources === undefined) {
|
---|
| 246 | currentRawSources = string;
|
---|
| 247 | } else if (Array.isArray(currentRawSources)) {
|
---|
| 248 | currentRawSources.push(string);
|
---|
| 249 | } else {
|
---|
| 250 | currentRawSources = [
|
---|
| 251 | typeof currentRawSources === "string"
|
---|
| 252 | ? currentRawSources
|
---|
| 253 | : currentRawSources.source(),
|
---|
| 254 | string
|
---|
| 255 | ];
|
---|
| 256 | }
|
---|
| 257 | };
|
---|
| 258 | const addSourceToRawSources = source => {
|
---|
| 259 | if (currentRawSources === undefined) {
|
---|
| 260 | currentRawSources = source;
|
---|
| 261 | } else if (Array.isArray(currentRawSources)) {
|
---|
| 262 | currentRawSources.push(source.source());
|
---|
| 263 | } else {
|
---|
| 264 | currentRawSources = [
|
---|
| 265 | typeof currentRawSources === "string"
|
---|
| 266 | ? currentRawSources
|
---|
| 267 | : currentRawSources.source(),
|
---|
| 268 | source.source()
|
---|
| 269 | ];
|
---|
| 270 | }
|
---|
| 271 | };
|
---|
| 272 | const mergeRawSources = () => {
|
---|
| 273 | if (Array.isArray(currentRawSources)) {
|
---|
| 274 | const rawSource = new RawSource(currentRawSources.join(""));
|
---|
| 275 | stringsAsRawSources.add(rawSource);
|
---|
| 276 | newChildren.push(rawSource);
|
---|
| 277 | } else if (typeof currentRawSources === "string") {
|
---|
| 278 | const rawSource = new RawSource(currentRawSources);
|
---|
| 279 | stringsAsRawSources.add(rawSource);
|
---|
| 280 | newChildren.push(rawSource);
|
---|
| 281 | } else {
|
---|
| 282 | newChildren.push(currentRawSources);
|
---|
| 283 | }
|
---|
| 284 | };
|
---|
| 285 | for (const child of this._children) {
|
---|
| 286 | if (typeof child === "string") {
|
---|
| 287 | if (currentString === undefined) {
|
---|
| 288 | currentString = child;
|
---|
| 289 | } else {
|
---|
| 290 | currentString += child;
|
---|
| 291 | }
|
---|
| 292 | } else {
|
---|
| 293 | if (currentString !== undefined) {
|
---|
| 294 | addStringToRawSources(currentString);
|
---|
| 295 | currentString = undefined;
|
---|
| 296 | }
|
---|
| 297 | if (stringsAsRawSources.has(child)) {
|
---|
| 298 | addSourceToRawSources(child);
|
---|
| 299 | } else {
|
---|
| 300 | if (currentRawSources !== undefined) {
|
---|
| 301 | mergeRawSources();
|
---|
| 302 | currentRawSources = undefined;
|
---|
| 303 | }
|
---|
| 304 | newChildren.push(child);
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | if (currentString !== undefined) {
|
---|
| 309 | addStringToRawSources(currentString);
|
---|
| 310 | }
|
---|
| 311 | if (currentRawSources !== undefined) {
|
---|
| 312 | mergeRawSources();
|
---|
| 313 | }
|
---|
| 314 | this._children = newChildren;
|
---|
| 315 | this._isOptimized = true;
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | module.exports = ConcatSource;
|
---|