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