From fba3cfef792bfcaf84029d0b4759d23521eefd96 Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Thu, 26 Oct 2023 02:02:32 +0300 Subject: [PATCH] $mol_sourcemap fixes --- sourcemap/builder.ts | 16 +++++++++------- sourcemap/dataurl.ts | 22 ++++++++++++++-------- sourcemap/sourcemap.ts | 2 +- tree2/text/to/sourcemap/sourcemap.ts | 1 - 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/sourcemap/builder.ts b/sourcemap/builder.ts index 23e4a77781c..fa7525081a3 100644 --- a/sourcemap/builder.ts +++ b/sourcemap/builder.ts @@ -126,14 +126,16 @@ namespace $ { if (segment.length >= 5) { const nameIndex = segment[4]! - const name = raw.names[nameIndex] - let mergedNameIndex = name_indexes.get(name) - if (mergedNameIndex === undefined) { - mergedNameIndex = names.length - name_indexes.set(name, mergedNameIndex) - names.push(name) + const name = raw.names?.[nameIndex] + if (name !== undefined) { + let mergedNameIndex = name_indexes.get(name) + if (mergedNameIndex === undefined) { + mergedNameIndex = names.length + name_indexes.set(name, mergedNameIndex) + names.push(name) + } + mergedSegment.push(mergedNameIndex) } - mergedSegment.push(mergedNameIndex) } mergedLine.push(mergedSegment) diff --git a/sourcemap/dataurl.ts b/sourcemap/dataurl.ts index 1d0218524f4..99bc1fac094 100644 --- a/sourcemap/dataurl.ts +++ b/sourcemap/dataurl.ts @@ -8,20 +8,26 @@ namespace $ { if (index === -1) return undefined - data = data.substring(index) + data = data.substring(index + prefix.length) - if (data.endsWith(end_comment)) data = data.substring(0, end_comment.length) + if (data.endsWith(end_comment)) data = data.substring(0, data.length - end_comment.length) const decoded = this.decodeURIComponent(data) - const map = JSON.parse(decoded) as $mol_sourcemap_raw | null - if (! map) return undefined + try { + const map = JSON.parse(decoded) as $mol_sourcemap_raw | null - if (typeof map.mappings === 'string' && map.mappings.startsWith(';;')) { - map.mappings = map.mappings.substring(2) - } + if (! map) return undefined + + if (typeof map.mappings === 'string' && map.mappings.startsWith(';;')) { + map.mappings = map.mappings.substring(2) + } - return map + return map + } catch (e) { + if (e instanceof Error) e.message += ', origin=' + decoded + $mol_fail_hidden(e) + } } export function $mol_sourcemap_dataurl_encode(this: $, map: $mol_sourcemap_raw, type = 'js' as 'js' | 'css') { diff --git a/sourcemap/sourcemap.ts b/sourcemap/sourcemap.ts index c67f36f29a4..131d60565de 100644 --- a/sourcemap/sourcemap.ts +++ b/sourcemap/sourcemap.ts @@ -7,7 +7,7 @@ namespace $ { export interface $mol_sourcemap_raw { version: number sources: string[] - names: string[] + names?: string[] sourceRoot?: string sourcesContent?: (string | null)[] mappings: string | $mol_sourcemap_line[] diff --git a/tree2/text/to/sourcemap/sourcemap.ts b/tree2/text/to/sourcemap/sourcemap.ts index 0d45a249f69..ceea8933562 100644 --- a/tree2/text/to/sourcemap/sourcemap.ts +++ b/tree2/text/to/sourcemap/sourcemap.ts @@ -90,7 +90,6 @@ namespace $ { const map: $mol_sourcemap_raw = { version: 3, - names: [], sources: [ ... file_sources.keys() ], sourcesContent: [ ... file_sources.values() ], mappings,