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

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@sinclair/typebox-adapter",
"version": "0.9.0",
"version": "0.9.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@sinclair/typebox-adapter",
"version": "0.9.0",
"version": "0.9.1",
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",

View File

@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox-adapter",
"version": "0.9.0",
"version": "0.9.1",
"description": "Integrate Valibot and Zod with TypeBox",
"author": "sinclairzx81",
"license": "MIT",

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'`)
}