Skip to content

Commit 6caa0fb

Browse files
authored
fix: drop dangling sourceMappingURL from dts chunks (#317)
1 parent ccfeccb commit 6caa0fb

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎src/fake-js/index.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import type * as t from 'yuku'
6565
// output:
6666
// export declare function x$1(xx: X$1): void
6767

68+
const RE_SOURCE_MAPPING_URL = /\n?\/\/# sourceMappingURL=[^\n]*\n?$/
69+
6870
export function createFakeJsPlugin({
6971
sourcemap,
7072
cjsDefault,
@@ -132,6 +134,21 @@ export function createFakeJsPlugin({
132134

133135
generateBundle(options, bundle) {
134136
for (const chunk of Object.values(bundle)) {
137+
// `sourcemap` is an output-wide option, so the JS sourcemap setting
138+
// also applies to dts chunks. When declaration maps are disabled,
139+
// drop the map and its `sourceMappingURL` comment from dts chunks so
140+
// they don't reference a file that is never emitted.
141+
if (
142+
!sourcemap &&
143+
chunk.type === 'chunk' &&
144+
RE_DTS.test(chunk.fileName)
145+
) {
146+
chunk.code = chunk.code.replace(RE_SOURCE_MAPPING_URL, '')
147+
chunk.map = null
148+
chunk.sourcemapFileName = null
149+
continue
150+
}
151+
135152
if (!RE_DTS_MAP.test(chunk.fileName)) continue
136153

137154
if (sourcemap) {

‎tests/source-map.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,26 @@ test('disable dts source map only', async () => {
181181
]
182182
`)
183183
})
184+
185+
// https://github.com/rolldown/tsdown/issues/1091
186+
test.each([true, 'inline'] as const)(
187+
'disable dts source map only does not leave sourceMappingURL (sourcemap: %s)',
188+
async (sourcemap) => {
189+
const { chunks } = await rolldownBuild(
190+
input,
191+
[dts({ sourcemap: false })],
192+
{},
193+
{ sourcemap },
194+
)
195+
196+
const dtsChunk = chunks.find((chunk) => chunk.fileName === 'index.d.ts')
197+
expect(dtsChunk?.type).toBe('chunk')
198+
if (dtsChunk?.type !== 'chunk') return
199+
expect(dtsChunk.code).not.toContain('sourceMappingURL')
200+
expect(dtsChunk.map).toBeNull()
201+
202+
const jsChunk = chunks.find((chunk) => chunk.fileName === 'index.js')
203+
if (jsChunk?.type !== 'chunk') throw new Error('missing index.js')
204+
expect(jsChunk.code).toContain('//# sourceMappingURL=')
205+
},
206+
)

0 commit comments

Comments
 (0)