Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 96bf4ca

Browse filesBrowse files
committed
fix(sourcemap): avoid false source map detection
1 parent c825db7 commit 96bf4ca
Copy full SHA for 96bf4ca

3 files changed

+20-5Lines changed: 20 additions & 5 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/dtsx/src/generator.ts‎

Copy file name to clipboardExpand all lines: packages/dtsx/src/generator.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { PluginManager } from './plugins'
1818
import { processDeclarations } from './processor'
1919
import { normalizeOutput as normalizeDtsOutput } from './output-normalizer'
2020
import { applyTypeMappings, createTypeMapper } from './type-mappings'
21-
import { addSourceMapComment, checkIsolatedDeclarationsConfig, createDiff, generateDeclarationMap, validateDtsContent, writeToFile } from './utils'
21+
import { addSourceMapComment, checkIsolatedDeclarationsConfig, createDiff, generateDeclarationMap, sourceMapCommentPrefix, validateDtsContent, writeToFile } from './utils'
2222

2323
async function writeDeclarationMapFile(
2424
outputPath: string,
@@ -30,7 +30,7 @@ async function writeDeclarationMapFile(
3030
const dtsFilename = basename(outputPath) || 'output.d.ts'
3131
const sourceFilename = relative(dirname(outputPath), sourcePath)
3232
const mapFilename = `${dtsFilename}.map`
33-
const mapCommentIndex = dtsContent.lastIndexOf('\n//# sourceMappingURL=')
33+
const mapCommentIndex = dtsContent.lastIndexOf(`\n${sourceMapCommentPrefix()}`)
3434
const contentToMap = mapCommentIndex === -1 ? dtsContent : dtsContent.slice(0, mapCommentIndex)
3535
const sourceMap = generateDeclarationMap(contentToMap, dtsFilename, sourceFilename, sourceCode)
3636
const mapPath = `${outputPath}.map`
Collapse file

‎packages/dtsx/src/sourcemap.ts‎

Copy file name to clipboardExpand all lines: packages/dtsx/src/sourcemap.ts
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import { readFile, writeFile } from 'node:fs/promises'
77
import { basename, dirname, relative, resolve } from 'node:path'
88

9+
// Keep the source-map directive marker out of dtsx's own bundled JavaScript.
10+
// Bun scans dependency text for that marker even when it appears inside a
11+
// string literal, then tries to decode the placeholder as this bundle's map.
12+
function sourceMapComment(url: string): string {
13+
return `${String.fromCharCode(47, 47, 35)} sourceMappingURL=${url}`
14+
}
15+
916
/**
1017
* Source map configuration
1118
*/
@@ -254,7 +261,7 @@ export class SourceMapGenerator {
254261
* Generate inline source map comment
255262
*/
256263
toComment(includeContent = false): string {
257-
return `//# sourceMappingURL=${this.toDataUrl(includeContent)}`
264+
return sourceMapComment(this.toDataUrl(includeContent))
258265
}
259266
}
260267

@@ -549,7 +556,7 @@ export function appendSourceMapComment(
549556
content: string,
550557
mapFileName: string,
551558
): string {
552-
return `${content}\n//# sourceMappingURL=${mapFileName}`
559+
return `${content}\n${sourceMapComment(mapFileName)}`
553560
}
554561

555562
/**
Collapse file

‎packages/dtsx/src/utils.ts‎

Copy file name to clipboardExpand all lines: packages/dtsx/src/utils.ts
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { write } from './compat'
88
import { config } from './config'
99
import { validateTypeScriptSyntax } from './syntax-validator'
1010

11+
/**
12+
* Build the source-map directive without embedding the complete marker in
13+
* dtsx's own bundle, where Bun can mistake a string literal for a real map.
14+
*/
15+
export function sourceMapCommentPrefix(): string {
16+
return `${String.fromCharCode(47, 47, 35)} sourceMappingURL=`
17+
}
18+
1119
/**
1220
* Exhaustive check helper for switch statements
1321
* This function should never be called if all cases are handled
@@ -367,5 +375,5 @@ export function generateDeclarationMap(
367375
* Add source map URL comment to declaration content
368376
*/
369377
export function addSourceMapComment(dtsContent: string, mapFilename: string): string {
370-
return `${dtsContent}\n//# sourceMappingURL=${mapFilename}\n`
378+
return `${dtsContent}\n${sourceMapCommentPrefix()}${mapFilename}\n`
371379
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.