Skip to content

Commit 3a09ddd

Browse files
committed
feat: minify skill scripts and strip console output at build time
Inspired by https://justin.poehnelt.com/posts/rewrite-your-cli-for-ai-agents/ — skill scripts are now generated agent-first: no human-readable console output, no comments, minimal token footprint. - generate-skills.js now calls terser instead of copyFileSync - drop_console: true removes all console.* calls - mangle + no comments produces a single minified line per script - fallback to raw copy if minification fails (never breaks the build) - Each generated script gets a one-line header: // snippets/<Category>/<file>.js | sha256:<16-char> | <github-url> SHA-256 is computed from the original source, allowing verification that the minified script corresponds to a specific snippet version - terser added as devDependency
1 parent 0b55131 commit 3a09ddd

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"devDependencies": {
3535
"eslint": "^10.0.2",
36+
"terser": "^5.36.0",
3637
"vercel": "^32.4.1"
3738
},
3839
"overrides": {

scripts/generate-skills.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
const fs = require('fs')
1010
const path = require('path')
11+
const { createHash } = require('crypto')
12+
const { minify } = require('terser')
13+
14+
const GITHUB_BASE = 'https://github.com/nucliweb/webperf-snippets/blob/main'
1115

1216
const ROOT = path.join(__dirname, '..')
1317
const SNIPPETS_DIR = path.join(ROOT, 'snippets')
@@ -48,6 +52,28 @@ const CATEGORIES = {
4852
},
4953
}
5054

55+
async function buildScript(src, dst, relPath) {
56+
const source = fs.readFileSync(src, 'utf-8')
57+
const hash = createHash('sha256').update(source).digest('hex').slice(0, 16)
58+
const githubUrl = `${GITHUB_BASE}/${relPath}`
59+
const header = `// ${relPath} | sha256:${hash} | ${githubUrl}\n`
60+
61+
let code
62+
try {
63+
const result = await minify(source, {
64+
compress: { drop_console: true },
65+
mangle: true,
66+
format: { comments: false },
67+
})
68+
code = result.code
69+
} catch (err) {
70+
console.warn(` ⚠ minify failed for ${path.basename(src)}: ${err.message} — copying as-is`)
71+
code = source
72+
}
73+
74+
fs.writeFileSync(dst, header + code)
75+
}
76+
5177
function getSnippetFiles(category) {
5278
const dir = path.join(SNIPPETS_DIR, category)
5379
if (!fs.existsSync(dir)) return []
@@ -185,7 +211,7 @@ function buildSnippetMeta(category, snippetFile) {
185211
}
186212
}
187213

188-
function generateCategorySkill(category, catConfig) {
214+
async function generateCategorySkill(category, catConfig) {
189215
const snippetFiles = getSnippetFiles(category)
190216
if (snippetFiles.length === 0) return
191217

@@ -200,9 +226,9 @@ function generateCategorySkill(category, catConfig) {
200226
for (const snippetFile of snippetFiles) {
201227
const src = path.join(SNIPPETS_DIR, category, snippetFile)
202228
const dst = path.join(scriptsDir, snippetFile)
203-
fs.copyFileSync(src, dst)
229+
await buildScript(src, dst, `snippets/${category}/${snippetFile}`)
204230
}
205-
console.log(` copied ${snippetFiles.length} scripts to scripts/`)
231+
console.log(` built ${snippetFiles.length} scripts to scripts/`)
206232

207233
const lines = []
208234

@@ -357,7 +383,7 @@ function validateSkill(name, description) {
357383
return errors
358384
}
359385

360-
function main() {
386+
async function main() {
361387
console.log('Generating WebPerf skills...\n')
362388
fs.mkdirSync(SKILLS_DIR, { recursive: true })
363389

@@ -378,7 +404,7 @@ function main() {
378404
}
379405

380406
for (const [category, config] of Object.entries(CATEGORIES)) {
381-
generateCategorySkill(category, config)
407+
await generateCategorySkill(category, config)
382408
}
383409

384410
generateMetaSkill()
@@ -410,4 +436,4 @@ function main() {
410436
console.log('\nDone! Skills generated in /skills/ and .claude/skills/')
411437
}
412438

413-
main()
439+
main().catch(console.error)

0 commit comments

Comments
 (0)