Skip to content

Commit ed17b9a

Browse files
userquinsxzz
andauthored
feat!: include dual cjs/esm in package exports (#99)
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
1 parent 39fb320 commit ed17b9a

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

package.json

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@
2020
],
2121
"exports": {
2222
".": {
23-
"types": "./dist/index.d.ts",
24-
"import": "./dist/index.mjs",
25-
"require": "./dist/index.cjs"
23+
"import": {
24+
"types": "./dist/index.d.ts",
25+
"default": "./dist/index.mjs"
26+
},
27+
"require": {
28+
"types": "./dist/index.d.cts",
29+
"default": "./dist/index.cjs"
30+
}
2631
},
2732
"./nuxt": {
28-
"types": "./dist/nuxt.d.ts",
29-
"import": "./dist/nuxt.mjs",
30-
"require": "./dist/nuxt.cjs"
33+
"import": {
34+
"types": "./dist/nuxt.d.ts",
35+
"default": "./dist/nuxt.mjs"
36+
},
37+
"require": {
38+
"types": "./dist/nuxt.d.cts",
39+
"default": "./dist/nuxt.cjs"
40+
}
3141
},
3242
"./*": "./*"
3343
},
@@ -50,7 +60,7 @@
5060
"node": ">=14"
5161
},
5262
"scripts": {
53-
"build": "rimraf dist && run-s build:*",
63+
"build": "rimraf dist && run-s build:* && esno ./scripts/postbuild.ts",
5464
"build:client": "vite build src/client",
5565
"build:js": "unbuild",
5666
"dev": "nr stub && cross-env INSPECT_DEV=true vite src/client",

scripts/postbuild.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
2+
import { resolve } from 'node:path'
3+
4+
function patchCjs(cjsModulePath: string, name: string) {
5+
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
6+
writeFileSync(
7+
cjsModulePath,
8+
cjsModule.replace(`module.exports = ${name};`, `exports.default = ${name};`),
9+
{ encoding: 'utf-8' },
10+
)
11+
}
12+
13+
rmSync(resolve('./dist/index.d.mts'))
14+
rmSync(resolve('./dist/nuxt.d.mts'))
15+
16+
readdirSync('./dist/shared')
17+
.filter(file => file.endsWith('.d.mts'))
18+
.forEach((file) => {
19+
rmSync(resolve('./dist/shared', file))
20+
})
21+
22+
patchCjs(resolve('./dist/index.cjs'), 'PluginInspect')
23+
patchCjs(resolve('./dist/nuxt.cjs'), 'nuxt')

0 commit comments

Comments
 (0)