| 84 | * @returns {RawSourceMap} a v3 RawSourceMap |
| 85 | */ |
| 86 | const buildExportsSourceMap = ( |
| 87 | generatedJs, |
| 88 | exportLocs, |
| 89 | cssContent, |
| 90 | sourceName |
| 91 | ) => { |
| 92 | const lines = generatedJs.split("\n"); |
| 93 | |
| 94 | // O(L) pass: match `\t"<name>": <value>` lines against export names. |
| 95 | const keyToName = new Map(); |
| 96 | for (const [exportName] of exportLocs) { |
| 97 | keyToName.set(JSON.stringify(exportName), exportName); |
| 98 | } |
| 99 | const lineByExport = new Map(); |
| 100 | for (let i = 0; i < lines.length && lineByExport.size < keyToName.size; i++) { |
| 101 | const line = lines[i]; |
| 102 | if (line.charCodeAt(0) !== 9 || line.charCodeAt(1) !== 34) continue; |
| 103 | let j = 2; |
| 104 | while (j < line.length) { |
| 105 | const c = line.charCodeAt(j); |
| 106 | if (c === 92) { |
| 107 | j += 2; |
| 108 | continue; |
| 109 | } |
| 110 | if (c === 34) break; |
| 111 | j++; |
| 112 | } |
| 113 | if (line.charCodeAt(j + 1) !== 58) continue; |
| 114 | const name = keyToName.get(line.slice(1, j + 1)); |
| 115 | if (name !== undefined && !lineByExport.has(name)) { |
| 116 | lineByExport.set(name, i); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** @type {(import("../util/createMappings").LineMappings)[]} */ |
| 121 | const perLine = lines.map(() => null); |
| 122 | for (const [exportName, genLine] of lineByExport) { |
| 123 | const pos = /** @type {SourcePosition} */ (exportLocs.get(exportName)); |
| 124 | // V3 source maps are 0-based; webpack `loc` lines are 1-based. |
| 125 | perLine[genLine] = { |
| 126 | generatedColumn: 0, |
| 127 | sourceIndex: 0, |
| 128 | originalLine: pos.line - 1, |
| 129 | originalColumn: pos.column |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | return { |
| 134 | version: 3, |
| 135 | file: "", |
| 136 | sources: [sourceName], |
| 137 | sourcesContent: [cssContent], |
| 138 | names: [], |
| 139 | mappings: encodeMappings(perLine) |
| 140 | }; |
| 141 | }; |
| 142 | |
| 143 | class CssGenerator extends Generator { |