Skip to content

Commit 0a685bd

Browse files
authored
build: Reuse .d.t files for both esm and cjs dist files (#150)
For reduce package size
1 parent 5c7b425 commit 0a685bd

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,43 @@
108108
"exports": {
109109
".": {
110110
"import": "./dist/esm/mod.js",
111-
"require": "./dist/cjs/mod.js"
111+
"require": "./dist/cjs/mod.js",
112+
"types": "./dist/types/mod.d.ts"
112113
},
113114
"./dom": {
114115
"import": "./dist/esm/dom/mod.js",
115-
"require": "./dist/cjs/dom/mod.js"
116+
"require": "./dist/cjs/dom/mod.js",
117+
"types": "./dist/types/dom/mod.d.ts"
116118
},
117119
"./math": {
118120
"import": "./dist/esm/math/mod.js",
119-
"require": "./dist/cjs/math/mod.js"
121+
"require": "./dist/cjs/math/mod.js",
122+
"types": "./dist/types/math/mod.d.ts"
120123
},
121124
"./misc": {
122125
"import": "./dist/esm/misc/mod.js",
123-
"require": "./dist/cjs/misc/mod.js"
126+
"require": "./dist/cjs/misc/mod.js",
127+
"types": "./dist/types/misc/mod.d.ts"
124128
},
125129
"./react": {
126130
"import": "./dist/esm/react/mod.js",
127-
"require": "./dist/cjs/react/mod.js"
131+
"require": "./dist/cjs/react/mod.js",
132+
"types": "./dist/types/react/mod.d.ts"
128133
},
129134
"./rspack": {
130135
"import": "./dist/esm/rspack/mod.js",
131-
"require": "./dist/cjs/rspack/mod.js"
136+
"require": "./dist/cjs/rspack/mod.js",
137+
"types": "./dist/types/rspack/mod.d.ts"
132138
},
133139
"./testing": {
134140
"import": "./dist/esm/testing/mod.js",
135-
"require": "./dist/cjs/testing/mod.js"
141+
"require": "./dist/cjs/testing/mod.js",
142+
"types": "./dist/types/testing/mod.d.ts"
136143
},
137144
"./types": {
138145
"import": "./dist/esm/types/mod.js",
139-
"require": "./dist/cjs/types/mod.js"
146+
"require": "./dist/cjs/types/mod.js",
147+
"types": "./dist/types/types/mod.d.ts"
140148
},
141149
"./typings/css-modules": {
142150
"types": "./public/typings/css-modules.d.ts"

scripts/build.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ await execAsync('npx tsc -p tsconfig.build-esm.json');
1616
// build cjs files
1717
await execAsync('npx tsc -p tsconfig.build-cjs.json');
1818

19+
// build d.ts files
20+
await execAsync('npx tsc -p tsconfig.build-types.json');
21+
1922
// format (for reduce package size by replacing indent from 4 to 2)
2023
await execAsync('npx prettier "dist/**/*.js" --write --ignore-path=./.nonexistent');
2124

scripts/sync-exports.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ function exportsEntryFromEntrypoint(pathname: string): ExportsEntry {
5353
const basename = path.basename(pathname, path.extname(pathname));
5454
const srcRelativePath = path.relative('./src', pathname);
5555
const distRelativePath = path.join(path.dirname(srcRelativePath), `${basename}.js`);
56+
const distRelativePathDts = path.join(path.dirname(srcRelativePath), `${basename}.d.ts`);
5657

5758
return [
5859
formatPathname(path.dirname(srcRelativePath)),
5960
{
6061
import: formatPathname(path.join('dist/esm', distRelativePath)),
6162
require: formatPathname(path.join('dist/cjs', distRelativePath)),
63+
64+
// According to https://mayank.co/blog/dual-packages
65+
types: formatPathname(path.join('dist/types', distRelativePathDts)),
6266
},
6367
];
6468
}

tsconfig.build-cjs.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"rootDir": "./src",
5+
"baseUrl": "./src",
46
"noEmit": false,
5-
"declaration": true,
7+
"declaration": false,
8+
"isolatedDeclarations": false,
69
"sourceMap": true,
710
"outDir": "dist/cjs",
811
"module": "CommonJS",

tsconfig.build-esm.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"rootDir": "./src",
5+
"baseUrl": "./src",
46
"noEmit": false,
5-
"declaration": true,
7+
"declaration": false,
8+
"isolatedDeclarations": false,
69
"sourceMap": true,
710
"outDir": "dist/esm"
811
},

tsconfig.build-types.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"baseUrl": "./src",
6+
"noEmit": false,
7+
"emitDeclarationOnly": true,
8+
"declaration": true,
9+
"sourceMap": false,
10+
"outDir": "dist/types"
11+
},
12+
"include": ["src"],
13+
"exclude": [
14+
"node_modules",
15+
"tests-pkg",
16+
"tests-e2e",
17+
"docs",
18+
"dist",
19+
"./src/**/*.test.ts",
20+
"./src/**/*.test.tsx"
21+
],
22+
"tsc-alias": {
23+
"resolveFullPaths": true,
24+
"verbose": false
25+
}
26+
}

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"compilerOptions": {
3+
"rootDir": "./",
4+
"baseUrl": "./",
35
"noEmit": true,
46
"target": "ESNext",
57
"module": "NodeNext",

0 commit comments

Comments
 (0)