Skip to content

Commit 7b5a96c

Browse files
refactor: update for v3 source maps, prevent double writes.
1 parent 20a8dd1 commit 7b5a96c

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

src/cli.ts

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,28 @@ const readStdin = async (stdin: typeof defaultStdin) => {
472472

473473
const normalizeSourceMapArgv = (argv: string[]) => {
474474
let sourceMapInline = false
475+
let invalidSourceMapValue: string | null = null
475476
const normalized: string[] = []
477+
const recordInvalid = (value: string) => {
478+
if (!invalidSourceMapValue) invalidSourceMapValue = value
479+
}
476480

477481
for (let i = 0; i < argv.length; i += 1) {
478482
const arg = argv[i]
479483

480-
if (arg === '--source-map' && argv[i + 1] === 'inline') {
481-
sourceMapInline = true
482-
normalized.push('--source-map')
483-
i += 1
484-
continue
484+
if (arg === '--source-map') {
485+
const next = argv[i + 1]
486+
if (next === 'inline') {
487+
sourceMapInline = true
488+
normalized.push('--source-map')
489+
i += 1
490+
continue
491+
}
492+
if (next === 'true' || next === 'false') {
493+
normalized.push(`--source-map=${next}`)
494+
i += 1
495+
continue
496+
}
485497
}
486498

487499
if (arg.startsWith('--source-map=')) {
@@ -491,12 +503,23 @@ const normalizeSourceMapArgv = (argv: string[]) => {
491503
normalized.push('--source-map')
492504
continue
493505
}
506+
if (value === 'true' || value === 'false') {
507+
normalized.push(arg)
508+
continue
509+
}
510+
recordInvalid(value)
511+
continue
512+
}
513+
514+
if (arg === '--source-map' && argv[i + 1] && argv[i + 1].startsWith('--')) {
515+
normalized.push('--source-map')
516+
continue
494517
}
495518

496519
normalized.push(arg)
497520
}
498521

499-
return { argv: normalized, sourceMapInline }
522+
return { argv: normalized, sourceMapInline, invalidSourceMapValue }
500523
}
501524

502525
const expandFiles = async (patterns: string[], cwd: string, ignore?: string[]) => {
@@ -642,8 +665,11 @@ const runFiles = async (
642665
hazardScope === 'project' ? 'off' : moduleOpts.detectDualPackageHazard,
643666
}
644667

668+
const allowWrites = !flags.dryRun && !flags.list
669+
const writeInPlace = allowWrites && flags.inPlace
645670
let writeTarget: string | undefined
646-
if (!flags.dryRun && !flags.list) {
671+
672+
if (allowWrites) {
647673
if (flags.inPlace) {
648674
perFileOpts.inPlace = true
649675
} else if (outPath) {
@@ -656,6 +682,11 @@ const runFiles = async (
656682
}
657683
}
658684

685+
if (moduleOpts.sourceMap && (writeTarget || writeInPlace)) {
686+
perFileOpts.out = undefined
687+
perFileOpts.inPlace = false
688+
}
689+
659690
const transformed = await transform(file, perFileOpts)
660691
const output = typeof transformed === 'string' ? transformed : transformed.code
661692
const map = typeof transformed === 'string' ? null : transformed.map
@@ -671,25 +702,26 @@ const runFiles = async (
671702
logger.info(file)
672703
}
673704

674-
if (map && flags.sourceMapInline && !writeTarget && !perFileOpts.inPlace) {
705+
if (map && flags.sourceMapInline && !writeTarget && !writeInPlace) {
675706
const mapUri = Buffer.from(JSON.stringify(map)).toString('base64')
676707
finalOutput = `${output.replace(/\/\/# sourceMappingURL=.*/g, '').trimEnd()}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mapUri}\n`
677-
} else if (map && (writeTarget || perFileOpts.inPlace)) {
708+
} else if (map && (writeTarget || writeInPlace)) {
678709
const target = writeTarget ?? file
679710
const mapPath = `${target}.map`
680-
map.file = basename(mapPath)
711+
const mapFile = basename(mapPath)
712+
map.file = basename(target)
681713

682-
const updated = `${output.replace(/\/\/# sourceMappingURL=.*/g, '').trimEnd()}\n//# sourceMappingURL=${map.file}\n`
714+
const updated = `${output.replace(/\/\/# sourceMappingURL=.*/g, '').trimEnd()}\n//# sourceMappingURL=${mapFile}\n`
683715
await writeFile(mapPath, JSON.stringify(map))
684716

685717
if (writeTarget) {
686718
await writeFile(writeTarget, updated)
687-
} else if (perFileOpts.inPlace) {
719+
} else if (writeInPlace) {
688720
await writeFile(file, updated)
689721
}
690722
}
691723

692-
if (!flags.dryRun && !flags.list && !writeTarget && !perFileOpts.inPlace) {
724+
if (!flags.dryRun && !flags.list && !writeTarget && !writeInPlace) {
693725
io.stdout.write(finalOutput)
694726
}
695727

@@ -723,7 +755,17 @@ const runCli = async ({
723755
stdout = defaultStdout,
724756
stderr = defaultStderr,
725757
}: CliOptions = {}) => {
726-
const { argv: normalizedArgv, sourceMapInline } = normalizeSourceMapArgv(argv)
758+
const logger = makeLogger(stdout, stderr)
759+
const {
760+
argv: normalizedArgv,
761+
sourceMapInline,
762+
invalidSourceMapValue,
763+
} = normalizeSourceMapArgv(argv)
764+
765+
if (invalidSourceMapValue) {
766+
logger.error(`Invalid --source-map value: ${invalidSourceMapValue}`)
767+
return 2
768+
}
727769

728770
const { values, positionals } = parseArgs({
729771
args: normalizedArgv,
@@ -739,8 +781,6 @@ const runCli = async ({
739781
),
740782
})
741783

742-
const logger = makeLogger(stdout, stderr)
743-
744784
if (values.help) {
745785
stdout.write(buildHelp(stdout.isTTY ?? false))
746786
return 0

test/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ test('--source-map writes map files with out-dir', async t => {
736736
assert.match(written, /sourceMappingURL=entry.cjs.map/)
737737

738738
const map = JSON.parse(await readFile(mapFile, 'utf8'))
739-
assert.equal(map.file, 'entry.cjs.map')
739+
assert.equal(map.file, 'entry.cjs')
740740
assert.ok((map.sources ?? []).some((s: string) => s.endsWith('entry.cjs')))
741741
assert.ok(String(map.mappings || '').length > 0)
742742
})

0 commit comments

Comments
 (0)