Skip to content

Commit 9dc6700

Browse files
Vojtěch Václav Portešvojtechportes
authored andcommitted
feat: Reduced package size by removing map files and duplicate type declarations from published package
1 parent 2416dcf commit 9dc6700

4 files changed

Lines changed: 49 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.DS_Store
33
node_modules
44
.cache
5+
.npm-cache/
56
.vite
67
.rts2_cache_cjs
78
.rts2_cache_esm

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,22 @@
115115
"registry": "https://registry.npmjs.org"
116116
},
117117
"files": [
118-
"dist"
118+
"dist",
119+
"!dist/**/*.map",
120+
"!dist/**/*.d.ts",
121+
"dist/index.d.ts",
122+
"dist/parseQuery.d.ts",
123+
"dist/formatQuery.d.ts",
124+
"dist/bootstrap/v5/index.d.ts",
125+
"dist/mui/v7/index.d.ts",
126+
"dist/mui/v9/index.d.ts",
127+
"dist/antd/v5/index.d.ts",
128+
"dist/antd/v6/index.d.ts",
129+
"dist/fluentui/v8/index.d.ts",
130+
"dist/mantine/v8/index.d.ts",
131+
"dist/mantine/v9/index.d.ts",
132+
"dist/radix/v1/index.d.ts",
133+
"dist/monaco/index.d.ts"
119134
],
120135
"scripts": {
121136
"build": "tsdown && node scripts/copy-dts.mjs",

scripts/copy-dts.mjs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
/* global console */
22

3-
import { copyFile, readdir } from 'node:fs/promises';
3+
import { copyFile, readFile } from 'node:fs/promises';
44
import path from 'node:path';
55
import { fileURLToPath } from 'node:url';
66

77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
9+
const rootDir = path.resolve(__dirname, '..');
910
const distDir = path.resolve(__dirname, '..', 'dist');
10-
11-
const findDeclarationFiles = async (directory) => {
12-
const entries = await readdir(directory, { withFileTypes: true });
13-
const nestedFiles = await Promise.all(
14-
entries.map(async (entry) => {
15-
const resolvedPath = path.join(directory, entry.name);
16-
17-
if (entry.isDirectory()) {
18-
return findDeclarationFiles(resolvedPath);
19-
}
20-
21-
return entry.name.endsWith('.d.mts') ? [resolvedPath] : [];
22-
})
11+
const packageJsonPath = path.join(rootDir, 'package.json');
12+
13+
const getPublicDeclarationFiles = async () => {
14+
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'));
15+
const declarationFiles = new Set();
16+
17+
if (typeof packageJson.types === 'string') {
18+
declarationFiles.add(path.resolve(rootDir, packageJson.types));
19+
}
20+
21+
for (const exportDefinition of Object.values(packageJson.exports ?? {})) {
22+
if (
23+
exportDefinition &&
24+
typeof exportDefinition === 'object' &&
25+
typeof exportDefinition.types === 'string'
26+
) {
27+
declarationFiles.add(path.resolve(rootDir, exportDefinition.types));
28+
}
29+
}
30+
31+
return [...declarationFiles].filter(fileName =>
32+
fileName.startsWith(distDir) && fileName.endsWith('.d.ts')
2333
);
24-
25-
return nestedFiles.flat();
2634
};
2735

2836
try {
29-
const declarationFiles = await findDeclarationFiles(distDir);
37+
const declarationFiles = await getPublicDeclarationFiles();
3038

3139
await Promise.all(
3240
declarationFiles.map(fileName =>
3341
copyFile(
34-
fileName,
35-
fileName.replace(/\.d\.mts$/, '.d.ts')
42+
fileName.replace(/\.d\.ts$/, '.d.mts'),
43+
fileName
3644
)
3745
)
3846
);
3947
} catch (error) {
40-
console.error('Unable to create .d.ts files from .d.mts build artifacts');
48+
console.error('Unable to create public .d.ts files from .d.mts build artifacts');
4149
throw error;
4250
}

tsdown.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { defineConfig } from 'tsdown';
22

33
export default defineConfig({
44
clean: true,
5-
dts: true,
5+
dts: {
6+
cjsReexport: true,
7+
},
68
entry: {
79
index: 'src/index.tsx',
810
parseQuery: 'src/parseQuery/index.ts',
@@ -19,5 +21,5 @@ export default defineConfig({
1921
'monaco/index': 'src/monaco/index.ts',
2022
},
2123
format: ['esm', 'cjs'],
22-
sourcemap: true,
24+
sourcemap: false,
2325
});

0 commit comments

Comments
 (0)