Skip to content

Commit 4b9322f

Browse files
committed
source map: avoid null entry for 0-length parts
1 parent 199a0d3 commit 4b9322f

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

internal/linker/linker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5851,7 +5851,7 @@ func (c *linkerContext) generateChunkJS(chunkIndex int, chunkWaitGroup *sync.Wai
58515851
prevOffset.AdvanceBytes(compileResult.JS)
58525852

58535853
// Include a null entry in the source map
5854-
if c.options.SourceMap != config.SourceMapNone {
5854+
if len(compileResult.JS) > 0 && c.options.SourceMap != config.SourceMapNone {
58555855
if n := len(compileResultsForSourceMap); n > 0 && !compileResultsForSourceMap[n-1].isNullEntry {
58565856
compileResultsForSourceMap = append(compileResultsForSourceMap, compileResultForSourceMap{
58575857
sourceIndex: compileResult.sourceIndex,
@@ -6358,7 +6358,7 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
63586358
prevOffset.AdvanceBytes(compileResult.CSS)
63596359

63606360
// Include a null entry in the source map
6361-
if c.options.SourceMap != config.SourceMapNone && compileResult.sourceIndex.IsValid() {
6361+
if len(compileResult.CSS) > 0 && c.options.SourceMap != config.SourceMapNone && compileResult.sourceIndex.IsValid() {
63626362
if n := len(compileResultsForSourceMap); n > 0 && !compileResultsForSourceMap[n-1].isNullEntry {
63636363
compileResultsForSourceMap = append(compileResultsForSourceMap, compileResultForSourceMap{
63646364
sourceIndex: compileResult.sourceIndex.GetIndex(),

scripts/verify-source-map.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,27 @@ const toSearchCodeSplitting = {
192192
out: 'out.ts',
193193
}
194194

195+
const testCaseCodeSplittingEmptyFile = {
196+
'entry1.ts': `
197+
import './a.ts'
198+
import './empty.ts'
199+
import './b.ts'
200+
`,
201+
'entry2.ts': `
202+
import './a.ts'
203+
import './empty.ts'
204+
import './b.ts'
205+
`,
206+
'a.ts': `'foo'.print()`,
207+
'empty.ts': `//! @preserve`,
208+
'b.ts': `'bar'.print()`,
209+
}
210+
211+
const toSearchCodeSplittingEmptyFile = {
212+
foo: 'a.ts',
213+
bar: 'b.ts',
214+
}
215+
195216
const testCaseUnicode = {
196217
'entry.js': `
197218
import './a'
@@ -430,7 +451,7 @@ const toSearchNullSourcesContent = {
430451
bar: 'bar.ts',
431452
}
432453

433-
async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf, followUpFlags = [] }) {
454+
async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf, followUpFlags = [], checkChunk }) {
434455
let failed = 0
435456

436457
try {
@@ -470,6 +491,18 @@ async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf,
470491

471492
let outCode
472493
let outCodeMap
494+
let outPrefix = 'out'
495+
496+
// Optionally check the first chunk when splitting
497+
if (checkChunk && flags.includes('--splitting')) {
498+
const entries = await fs.readdir(tempDir)
499+
for (const entry of entries.sort()) {
500+
if (entry.startsWith('chunk-')) {
501+
outPrefix = entry.slice(0, entry.indexOf('.'))
502+
break
503+
}
504+
}
505+
}
473506

474507
if (isStdin) {
475508
outCode = stdout
@@ -478,9 +511,9 @@ async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf,
478511
}
479512

480513
else {
481-
outCode = await fs.readFile(path.join(tempDir, `out.${ext}`), 'utf8')
482-
recordCheck(outCode.includes(`# sourceMappingURL=out.${ext}.map`), `.${ext} file must link to .${ext}.map`)
483-
outCodeMap = await fs.readFile(path.join(tempDir, `out.${ext}.map`), 'utf8')
514+
outCode = await fs.readFile(path.join(tempDir, `${outPrefix}.${ext}`), 'utf8')
515+
recordCheck(outCode.includes(`# sourceMappingURL=${outPrefix}.${ext}.map`), `.${ext} file must link to .${ext}.map`)
516+
outCodeMap = await fs.readFile(path.join(tempDir, `${outPrefix}.${ext}.map`), 'utf8')
484517
}
485518

486519
// Check the mapping of various key locations back to the original source
@@ -558,7 +591,7 @@ async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf,
558591

559592
// Bundle again to test nested source map chaining
560593
for (let order of [0, 1, 2]) {
561-
const fileToTest = isStdin ? `stdout.${ext}` : `out.${ext}`
594+
const fileToTest = isStdin ? `stdout.${ext}` : `${outPrefix}.${ext}`
562595
const nestedEntry = path.join(tempDir, `nested-entry.${ext}`)
563596
if (isStdin) await fs.writeFile(path.join(tempDir, fileToTest), outCode)
564597
await fs.writeFile(path.join(tempDir, `extra.${ext}`), `console.log('extra')`)
@@ -572,6 +605,7 @@ async function check(kind, testCase, toSearch, { ext, flags, entryPoints, crlf,
572605
'--bundle',
573606
'--outfile=' + path.join(tempDir, `out2.${ext}`),
574607
'--sourcemap',
608+
'--format=esm',
575609
].concat(followUpFlags), { cwd: testDir })
576610

577611
const out2Code = await fs.readFile(path.join(tempDir, `out2.${ext}`), 'utf8')
@@ -892,6 +926,15 @@ async function main() {
892926
entryPoints: ['foo.js'],
893927
crlf,
894928
}),
929+
930+
// This checks for issues with files in a bundle that don't emit source maps
931+
check('splitting-empty' + suffix, testCaseCodeSplittingEmptyFile, toSearchCodeSplittingEmptyFile, {
932+
ext: 'js',
933+
flags: flags.concat('--outdir=.', '--bundle', '--splitting', '--format=esm'),
934+
entryPoints: ['entry1.ts', 'entry2.ts'],
935+
crlf,
936+
checkChunk: true,
937+
})
895938
)
896939
}
897940
}

0 commit comments

Comments
 (0)