Skip to content

Commit 20d355a

Browse files
authored
build: generate sourcemaps in import-meta-current-dirname plugin (#23904)
1 parent 8109efd commit 20d355a

1 file changed

Lines changed: 39 additions & 27 deletions

File tree

‎packages/vite/rolldown.config.ts‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,39 +344,51 @@ function buildTimeImportMetaUrlPlugin(): Plugin {
344344
s.overwrite(ss, se + 4, `${prefix}${index}`)
345345
}
346346
}
347-
return s.hasChanged() ? s.toString() : undefined
347+
return s.hasChanged()
348+
? {
349+
code: s.toString(),
350+
map: s.generateMap({ source: id, hires: 'boundary' }),
351+
}
352+
: undefined
348353
},
349354
},
350355
renderChunk(code, chunk, outputOptions) {
351356
if (!code.includes(prefix)) return
352357

353-
return code.replace(
354-
/__vite_buildTimeImportMetaUrl_(\d+)/g,
355-
(_, index) => {
356-
const originalFile = Object.keys(idMap).find(
357-
(key) => idMap[key] === +index,
358+
const s = new MagicString(code)
359+
const pattern = /__vite_buildTimeImportMetaUrl_(\d+)/g
360+
let match
361+
while ((match = pattern.exec(code))) {
362+
const originalFile = Object.keys(idMap).find(
363+
(key) => idMap[key] === +match![1],
364+
)
365+
if (!originalFile) {
366+
throw new Error(
367+
`Could not find original file for ${prefix}${match[1]} in ${chunk.fileName}`,
358368
)
359-
if (!originalFile) {
360-
throw new Error(
361-
`Could not find original file for ${prefix}${index} in ${chunk.fileName}`,
362-
)
363-
}
364-
const outputFile = path.resolve(outputOptions.dir!, chunk.fileName)
365-
const relativePath = path
366-
.relative(path.dirname(outputFile), originalFile)
367-
.replaceAll('\\', '/')
368-
369-
if (outputOptions.format === 'es') {
370-
return `new URL(${JSON.stringify(relativePath)}, import.meta.url)`
371-
} else if (outputOptions.format === 'cjs') {
372-
return `new URL(${JSON.stringify(
373-
relativePath,
374-
)}, require('node:url').pathToFileURL(__filename))`
375-
} else {
376-
throw new Error(`Unsupported output format ${outputOptions.format}`)
377-
}
378-
},
379-
)
369+
}
370+
const outputFile = path.resolve(outputOptions.dir!, chunk.fileName)
371+
const relativePath = path
372+
.relative(path.dirname(outputFile), originalFile)
373+
.replaceAll('\\', '/')
374+
375+
let replacement: string
376+
if (outputOptions.format === 'es') {
377+
replacement = `new URL(${JSON.stringify(relativePath)}, import.meta.url)`
378+
} else if (outputOptions.format === 'cjs') {
379+
replacement = `new URL(${JSON.stringify(
380+
relativePath,
381+
)}, require('node:url').pathToFileURL(__filename))`
382+
} else {
383+
throw new Error(`Unsupported output format ${outputOptions.format}`)
384+
}
385+
s.overwrite(match.index, match.index + match[0].length, replacement)
386+
}
387+
388+
if (!outputOptions.sourcemap) {
389+
return s.toString()
390+
}
391+
return { code: s.toString(), map: s.generateMap({ hires: 'boundary' }) }
380392
},
381393
}
382394
}

0 commit comments

Comments
 (0)