Skip to content

Commit 7f3aa9a

Browse files
build: optimize package contents for npm publishing
• Add CHANGELOG.md and TESTING.md to package.json files array • Create cross-platform clean-maps.js script to remove source maps • Add build:prod script and prepublishOnly hook for automated cleanup • Enhance .npmignore with explicit source map exclusion patterns • Reduces package size by 27% (109.9 kB to 80.1 kB) • Ensures all required documentation is included in published package • Automates source map removal before publishing for production builds
1 parent eaa263b commit 7f3aa9a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ docs/
2424

2525
# Exclude build artifacts
2626
*.map
27+
**/*.map
28+
dist/**/*.map
2729
tsconfig.json
2830
.prettierrc*
2931

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"build": "npx tsc",
9+
"build:prod": "npx tsc && pnpm run clean:maps",
10+
"clean:maps": "node scripts/clean-maps.js",
911
"format": "pnpm run format:code",
1012
"format:ci": "pnpm run format:code",
1113
"format:code": "prettier -w \"**/*\" --ignore-unknown --cache",
1214
"version": "changeset version && pnpm install --no-frozen-lockfile && pnpm run format",
13-
"dev:cli": "node dist/index-dev.js"
15+
"dev:cli": "node dist/index-dev.js",
16+
"prepublishOnly": "pnpm run build:prod"
1417
},
1518
"type": "module",
1619
"bin": {
@@ -59,7 +62,10 @@
5962
"dist/cli/commands/revert",
6063
"dist/cli/commands/shared",
6164
"dist/cli/utils",
62-
"dist/lib"
65+
"dist/lib",
66+
"README.md",
67+
"CHANGELOG.md",
68+
"TESTING.md"
6369
],
6470
"publishConfig": {
6571
"access": "public"

scripts/clean-maps.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Remove all .map files from dist directory
5+
* Cross-platform solution for cleaning source maps before publishing
6+
*/
7+
8+
import { readdir, stat, unlink } from "fs/promises";
9+
import { join } from "path";
10+
11+
async function removeMaps(dir) {
12+
try {
13+
const entries = await readdir(dir, { withFileTypes: true });
14+
15+
for (const entry of entries) {
16+
const fullPath = join(dir, entry.name);
17+
18+
if (entry.isDirectory()) {
19+
await removeMaps(fullPath);
20+
} else if (entry.isFile() && entry.name.endsWith(".map")) {
21+
await unlink(fullPath);
22+
console.log(`Removed: ${fullPath}`);
23+
}
24+
}
25+
} catch (error) {
26+
if (error.code !== "ENOENT") {
27+
throw error;
28+
}
29+
}
30+
}
31+
32+
const distDir = join(process.cwd(), "dist");
33+
removeMaps(distDir).catch((error) => {
34+
console.error("Error removing source maps:", error);
35+
process.exit(1);
36+
});
37+

0 commit comments

Comments
 (0)