Skip to content

Commit 2d330aa

Browse files
fix: plugin types are wrong in CJS (#2)
- Fix types in package.json causing package to masquerade as ESM - Change the default export to be a more helpful name to avoid import-x/no-rename-default. - Fix incorrect default export in CJS due to a known issue with unbuild.
1 parent f460633 commit 2d330aa

File tree

5 files changed

+316
-38
lines changed

5 files changed

+316
-38
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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
"sideEffects": false,
2424
"exports": {
2525
".": {
26-
"types": "./dist/index.d.ts",
27-
"import": "./dist/index.mjs",
28-
"require": "./dist/index.cjs"
26+
"import": {
27+
"types": "./dist/index.d.mts",
28+
"default": "./dist/index.mjs"
29+
},
30+
"require": {
31+
"types": "./dist/index.d.cts",
32+
"default": "./dist/index.cjs"
33+
}
2934
}
3035
},
3136
"main": "./dist/index.mjs",
@@ -36,7 +41,7 @@
3641
"docs"
3742
],
3843
"scripts": {
39-
"build": "unbuild",
44+
"build": "unbuild && tsx scripts/postbuild.ts",
4045
"lint": "eslint",
4146
"release": "bumpp && yarn run build",
4247
"test": "vitest",
@@ -71,6 +76,7 @@
7176
"eslint-plugin-import-x": "^4.4.0",
7277
"eslint-plugin-n": "^17.12.0",
7378
"rxjs": "^7.0.0",
79+
"tsx": "^4.19.2",
7480
"typescript": "~5.6.3",
7581
"typescript-eslint": "^8.12.2",
7682
"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')

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ const plugin = {
9191
},
9292
} satisfies TSESLint.FlatConfig.Plugin;
9393

94-
const configs = {
95-
recommended: createRecommendedConfig(plugin),
96-
} satisfies TSESLint.FlatConfig.SharedConfigs;
97-
98-
export default {
94+
const rxjsX = {
9995
...plugin,
100-
configs,
101-
};
96+
configs: {
97+
recommended: createRecommendedConfig(plugin),
98+
},
99+
} satisfies TSESLint.FlatConfig.Plugin;
100+
101+
export default rxjsX;

0 commit comments

Comments
 (0)