| 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;
|
|---|