From 6e3b57568eb2726b5bc185d14e5f7128ecc718fb Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 17 Feb 2025 07:00:20 +0000 Subject: [PATCH] build: align cjs/esm types + add explicit cjs extensions --- package.json | 2 +- packages/vite-plugin-checker/package.json | 7 +++--- packages/vite-plugin-checker/tsup.config.ts | 2 +- scripts/patchCjs.mjs | 24 ++++++++++++++------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 2766e05e..a7916670 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "format": "biome format . --write", "postinstall": "simple-git-hooks install", "lint": "biome check . --diagnostic-level=warn", - "publint": "pnpm -r --filter='vite-plugin-checker' exec publint", + "publint": "pnpm build && pnpm -r --filter='vite-plugin-checker' exec publint", "sort-package-json": "npx sort-package-json \"packages/*/package.json\"", "test": "run-s test-unit test-serve test-build", "test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", diff --git a/packages/vite-plugin-checker/package.json b/packages/vite-plugin-checker/package.json index 1ad15b6d..a91c2e28 100644 --- a/packages/vite-plugin-checker/package.json +++ b/packages/vite-plugin-checker/package.json @@ -18,13 +18,12 @@ "type": "module", "exports": { ".": { - "types": "./dist/esm/main.d.ts", "import": "./dist/esm/main.js", - "require": "./dist/cjs/main.js" + "require": "./dist/cjs/main.cjs" } }, - "main": "./dist/cjs/main.js", - "types": "./dist/esm/main.d.ts", + "main": "./dist/cjs/main.cjs", + "types": "./dist/cjs/main.d.cts", "files": [ "dist", "!dist/checkers/vueTsc/typescript-vue-tsc" diff --git a/packages/vite-plugin-checker/tsup.config.ts b/packages/vite-plugin-checker/tsup.config.ts index 8ba93067..ad5d9a67 100644 --- a/packages/vite-plugin-checker/tsup.config.ts +++ b/packages/vite-plugin-checker/tsup.config.ts @@ -19,7 +19,7 @@ export default defineConfig([ shims: true, outExtension() { return { - js: '.js', + js: '.cjs', } }, async onSuccess() { diff --git a/scripts/patchCjs.mjs b/scripts/patchCjs.mjs index 3bd1b7f8..e5a768dc 100644 --- a/scripts/patchCjs.mjs +++ b/scripts/patchCjs.mjs @@ -1,18 +1,26 @@ -import fs from 'node:fs/promises' -import path, { dirname } from 'node:path' +import fs, { copyFile } from 'node:fs/promises' +import path from 'node:path' import { fileURLToPath } from 'node:url' +import { glob } from 'tinyglobby' -const __filename = fileURLToPath(import.meta.url) -const __dirname = dirname(__filename) +const cjsDist = fileURLToPath(new URL('../packages/vite-plugin-checker/dist/cjs', import.meta.url)) async function main() { await fs.writeFile( - path.resolve( - __dirname, - '../packages/vite-plugin-checker/dist/cjs/package.json', - ), + path.join(cjsDist, 'package.json'), JSON.stringify({ type: 'commonjs' }, null, 2), ) + + // correct declaration file extensions + const cjsDeclarations = await glob('**/*.d.ts', { + absolute: true, + cwd: cjsDist + }) + + for (const file of cjsDeclarations) { + await copyFile(file, file.replace('.d.ts', '.d.cts')) + await fs.rm(file) + } } main()