Skip to content

Commit ae9791c

Browse files
committed
fix: Export cjs/esm modules
1 parent 1ebdfe8 commit ae9791c

File tree

10 files changed

+230
-57
lines changed

10 files changed

+230
-57
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
with:
5050
name: build-artifacts
5151
retention-days: 1
52-
path: dist*/
52+
path: lib*/
5353

5454
test-package-rollup:
5555
name: "rollup v${{ matrix.rollup }} / node ${{ matrix.node }} / ${{ matrix.os }}"

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
*.log
22
.DS_Store
33
node_modules
4-
dist
4+
lib
55
.env

package-lock.json

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
"stats",
1212
"bundle-stats"
1313
],
14-
"main": "dist/index.cjs",
15-
"module": "dist/index.mjs",
16-
"typings": "dist/index.d.ts",
14+
"main": "lib/cjs/index.js",
15+
"module": "lib/esm/index.js",
16+
"typings": "lib/types/index.d.ts",
1717
"exports": {
1818
".": {
19-
"types": "./dist/index.d.ts",
20-
"import": "./dist/index.mjs",
21-
"require": "./dist/index.cjs"
19+
"types": "./lib/types/index.d.ts",
20+
"import": "./lib/esm/index.js",
21+
"require": "./lib/cjs/index.js"
2222
},
2323
"./transform": {
24-
"types": "./dist/transform.d.ts",
25-
"import": "./dist/transform.mjs",
26-
"require": "./dist/transform.cjs"
24+
"types": "./lib/types/transform.d.ts",
25+
"import": "./lib/esm/transform.js",
26+
"require": "./lib/cjs/transform.js"
2727
}
2828
},
2929
"author": {
@@ -40,14 +40,15 @@
4040
},
4141
"homepage": "https://github.com/relative-ci/rollup-plugin-webpack-stats/blob/master/#readme",
4242
"files": [
43-
"dist"
43+
"lib"
4444
],
4545
"engines": {
4646
"node": ">=18"
4747
},
4848
"scripts": {
49-
"prebuild": "rimraf ./dist",
50-
"build": "tsc && rollup -c rollup.config.mjs",
49+
"build": "npm run clean && rollup -c && npm run build-type",
50+
"build-type": "echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
51+
"clean": "rimraf ./lib",
5152
"lint": "exit 0",
5253
"test:unit": "vitest test/unit",
5354
"test:package": "npm run test:package:rollup && npm run test:package:vite",
@@ -69,6 +70,8 @@
6970
},
7071
"devDependencies": {
7172
"@release-it/conventional-changelog": "10.0.1",
73+
"@rollup/plugin-commonjs": "28.0.6",
74+
"@rollup/plugin-node-resolve": "16.0.1",
7275
"@rollup/plugin-typescript": "12.1.4",
7376
"@tsconfig/node18": "18.2.4",
7477
"@types/node": "24.3.0",
@@ -78,6 +81,7 @@
7881
"release-it": "19.0.4",
7982
"rimraf": "6.0.1",
8083
"rollup": "4.46.4",
84+
"rollup-plugin-node-externals": "8.1.0",
8185
"tslib": "2.8.1",
8286
"typescript": "5.9.2",
8387
"vitest": "3.2.4"
@@ -86,9 +90,9 @@
8690
"rollup-plugin-stats": "1.5.0"
8791
},
8892
"peerDependencies": {
89-
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0",
93+
"rolldown": "^1.0.0-beta.0",
9094
"rollup": "^3.0.0 || ^4.0.0",
91-
"rolldown": "^1.0.0-beta.0"
95+
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
9296
},
9397
"peerDependenciesMeta": {
9498
"vite": {

rollup.config.mjs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,65 @@
1+
import path from 'node:path';
12
import { defineConfig } from 'rollup';
2-
import typescript from '@rollup/plugin-typescript';
3+
import commonjsPlugin from '@rollup/plugin-commonjs';
4+
import nodeResolvePlugin from '@rollup/plugin-node-resolve';
5+
import typescriptPlugin from '@rollup/plugin-typescript';
6+
7+
const CONTEXT = path.join(import.meta.dirname, './src');
38

49
const INPUT = {
510
'index': './src/index.ts',
611
'transform': './src/transform.ts',
712
};
813

9-
const OUTPUT_DIR = 'dist';
14+
const OUTPUT_DIR = 'lib';
1015

1116
export default defineConfig([
1217
{
18+
context: CONTEXT,
1319
input: INPUT,
1420
output: {
1521
dir: OUTPUT_DIR,
16-
format: 'esm',
17-
entryFileNames: '[name].mjs',
18-
chunkFileNames: 'chunks/[name].mjs',
22+
format: 'cjs',
23+
entryFileNames: 'cjs/[name].js',
1924
sourcemap: true,
25+
preserveModules: true,
26+
preserveModulesRoot: CONTEXT,
27+
interop: 'auto',
2028
},
21-
plugins: [typescript({ tsconfig: './tsconfig.json' })],
22-
external: ['crypto', 'path'],
29+
external: /node_modules/,
30+
plugins: [
31+
nodeResolvePlugin({
32+
extensions: ['.js', '.cjs', '.json'],
33+
}),
34+
commonjsPlugin({
35+
defaultIsModuleExports: 'auto',
36+
}),
37+
typescriptPlugin({
38+
tsconfig: './tsconfig.cjs.json',
39+
}),
40+
],
2341
},
2442
{
25-
input: INPUT,
43+
context: CONTEXT,
44+
input: './src/index.ts',
2645
output: {
2746
dir: OUTPUT_DIR,
28-
format: 'commonjs',
29-
entryFileNames: '[name].cjs',
30-
chunkFileNames: 'chunks/[name].cjs',
47+
format: 'esm',
48+
entryFileNames: 'esm/[name].js',
3149
sourcemap: true,
50+
preserveModules: true,
51+
preserveModulesRoot: CONTEXT,
52+
interop: 'auto',
3253
},
33-
plugins: [typescript({ tsconfig: './tsconfig.json' })],
34-
external: ['crypto', 'path'],
54+
external: /node_modules/,
55+
plugins: [
56+
nodeResolvePlugin({
57+
extensions: ['.js', '.mjs', '.cjs', '.json'],
58+
}),
59+
commonjsPlugin(),
60+
typescriptPlugin({
61+
tsconfig: './tsconfig.esm.json',
62+
}),
63+
],
3564
},
3665
]);

test/package/rollup/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@rollup/plugin-typescript": "^12.1.4",
1616
"cross-env": "^7.0.0",
1717
"rollup": "^4.9.1",
18-
"rollup-plugin-webpack-stats": "*",
18+
"rollup-plugin-webpack-stats": "file://../../../",
1919
"typescript": "^5.9.2"
2020
}
2121
}

tsconfig.base.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"extends": "@tsconfig/node18/tsconfig.json",
3+
"compilerOptions": {
4+
/* dirs */
5+
"rootDir": "./src",
6+
"declarationDir": "./lib/types",
7+
"types": ["node"],
8+
/* module config */
9+
"target": "es2020",
10+
"lib": ["es2020", "es2022.error"],
11+
"resolveJsonModule": true,
12+
"esModuleInterop": true,
13+
/* behaviour options */
14+
"strict": true,
15+
"allowJs": true,
16+
"allowSyntheticDefaultImports": true,
17+
"noFallthroughCasesInSwitch": true,
18+
"declaration": true,
19+
"sourceMap": true,
20+
"noImplicitReturns": true,
21+
"noImplicitOverride": true,
22+
"noUnusedLocals": true,
23+
"noUnusedParameters": true,
24+
"skipLibCheck": true,
25+
"forceConsistentCasingInFileNames": true,
26+
"noEmit": true
27+
},
28+
"include": ["src"],
29+
"exclude": ["node_modules", "lib"]
30+
}

tsconfig.cjs.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./lib/cjs"
5+
}
6+
}

tsconfig.esm.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./lib/esm"
5+
}
6+
}

0 commit comments

Comments
 (0)