Skip to content

Commit 78c379d

Browse files
fix: incorrect default export in CJS output
1 parent 7005279 commit 78c379d

File tree

4 files changed

+301
-28
lines changed

4 files changed

+301
-28
lines changed

build.config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { defineBuildConfig } from 'unbuild';
22

33
export default defineBuildConfig({
4-
entries: [
5-
'src/index',
6-
],
7-
declaration: true,
8-
clean: true,
9-
rollup: {
10-
emitCJS: true,
11-
},
4+
entries: [
5+
'src/index',
6+
],
7+
declaration: true,
8+
clean: true,
9+
rollup: {
10+
emitCJS: true,
11+
},
1212
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"docs"
4242
],
4343
"scripts": {
44-
"build": "unbuild",
44+
"build": "unbuild && tsx scripts/postbuild.ts",
4545
"lint": "eslint",
4646
"release": "bumpp && yarn run build",
4747
"test": "vitest",
@@ -76,6 +76,7 @@
7676
"eslint-plugin-import-x": "^4.4.0",
7777
"eslint-plugin-n": "^17.12.0",
7878
"rxjs": "^7.0.0",
79+
"tsx": "^4.19.2",
7980
"typescript": "~5.6.3",
8081
"typescript-eslint": "^8.12.2",
8182
"unbuild": "^2.0.0",

scripts/postbuild.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* There is known issue with unbuild CJS bundling: https://github.com/unjs/unbuild/issues/374
3+
*
4+
* Script from https://github.com/antfu-collective/vite-plugin-inspect/blob/main/scripts/postbuild.ts
5+
*
6+
* Explanation of problem: https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseExportDefault.md
7+
*/
8+
import { readFileSync, writeFileSync } from 'node:fs'
9+
import { resolve } from 'node:path'
10+
11+
function patchCjs(cjsModulePath: string, name: string) {
12+
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
13+
writeFileSync(
14+
cjsModulePath,
15+
cjsModule
16+
.replace(`'use strict';`, `'use strict';Object.defineProperty(exports, '__esModule', {value: true});`)
17+
.replace(`module.exports = ${name};`, `exports.default = ${name};`),
18+
{ encoding: 'utf-8' },
19+
)
20+
}
21+
22+
patchCjs(resolve('./dist/index.cjs'), 'rxjsX')

0 commit comments

Comments
 (0)