Skip to content

Commit 2a0feda

Browse files
matmenclaude
andcommitted
package without declarations of internal modules
The build removes the .d.ts files the public declarations don't import (codecs, render, util except mem): the exports field hides those modules, nothing can import their types. 32 kB less unpacked, 159 kB packed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJgRK5qpwJEbHPuFv6kgAQ
1 parent d641dbe commit 2a0feda

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎scripts/build.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,23 @@ const rewrite = dir => {
2727
};
2828
rewrite(path.join(root, 'dist'));
2929

30+
// only the declarations the public ones import are needed (the exports field hides the other modules)
31+
const needed = new Set();
32+
const follow = file => {
33+
if (needed.has(file)) return;
34+
needed.add(file);
35+
for (const [, specifier] of fs.readFileSync(file, 'utf8').matchAll(/(?:from |import\()'(\.{1,2}\/[^']*)\.js'/g))
36+
follow(path.join(path.dirname(file), specifier + '.d.ts'));
37+
};
38+
follow(path.join(root, 'dist/index.d.ts'));
39+
const prune = dir => {
40+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
41+
const file = path.join(dir, entry.name);
42+
if (entry.isDirectory()) prune(file);
43+
else if (file.endsWith('.d.ts') && !needed.has(file)) fs.rmSync(file);
44+
}
45+
};
46+
prune(path.join(root, 'dist'));
47+
3048
// the package itself has no "type", so mark the build as ES modules
3149
fs.writeFileSync(path.join(root, 'dist/package.json'), '{"type":"module"}\n');

0 commit comments

Comments
 (0)