Revision 0.9.1 (#4)

- Fix Esm Specifier Rewrite on Build
This commit is contained in:
sinclairzx81
2025-01-16 22:40:09 +09:00
committed by GitHub
parent 6c3f34f5fe
commit 053c890236
3 changed files with 14 additions and 5 deletions

View File

@@ -32,7 +32,15 @@ import * as Fs from 'node:fs'
// ------------------------------------------------------------------
// Specifier Rewrite
// ------------------------------------------------------------------
function shouldSkipSpecifier(captured: string) {
const specifier = captured.slice(1, captured.length - 1)
return (
specifier.includes('.mjs') || // mitigate duplicate rewrite
specifier.startsWith("@sinclair/typebox") ||
specifier.startsWith("valibot") ||
specifier.startsWith("zod")
)
}
// prettier-ignore
function replaceInlineImportSpecifiers(content: string): string {
const pattern = /import\((.*?)\)/g
@@ -40,7 +48,7 @@ function replaceInlineImportSpecifiers(content: string): string {
const match = pattern.exec(content)
if (match === null) return content
const captured = match[1]
if(captured.includes('.mjs')) continue
if(shouldSkipSpecifier(captured)) continue
const specifier = captured.slice(1, captured.length - 1)
content = content.replace(captured, `"${specifier}.mjs"`)
}
@@ -52,6 +60,7 @@ function replaceExportSpecifiers(content: string): string {
const match = pattern.exec(content)
if(match === null) return content
const captured = match[3]
if(shouldSkipSpecifier(captured)) continue
const specifier = captured.slice(1, captured.length - 1)
content = content.replace(captured, `'${specifier}.mjs'`)
}