2026-08-12-miniprogram-npm-size-reduction.md 14.7 KB

miniprogram_npm Size Reduction Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.

Goal: Reduce the generated miniprogram_npm and uploaded mini-program package by pruning non-runtime artifacts and removing the unused glsx-applet-component dependency.

Architecture: Keep WeChat DevTools manual npm packaging as the source of runtime artifacts, then run one narrowly scoped upload-pruning function before the existing dependency-closure guard. Package manifests determine which plugins are built; upload configuration and the pruning function remove source maps, nested duplicate npm output, and development-only files without touching runtime assets.

Tech Stack: WeChat Mini Program project configuration, Node.js 24 CommonJS/ESM, node:test, npm lockfile v3, Yarn lockfile v1, PowerShell measurement commands.

Global Constraints

  • Preserve all unrelated working-tree changes, especially config.js; never stage or edit it for this task.
  • Preserve the runtime dependency glsk-applet-component and its app.json registration glsk-applet-component/GlsxPrivacy.
  • Remove only the unused dependency glsx-applet-component; do not upgrade any package versions.
  • The pruning boundary is limited to miniprogram_npm/miniprogram_npm and miniprogram_npm/**/*.map.
  • Do not edit or commit generated miniprogram_npm contents; generated deletion is local and recoverable by rebuilding npm in WeChat DevTools.
  • Run migration manifest phase pre before dependency/config implementation and phase post after verification.
  • Baseline is 712 files / 1,648,929 bytes; 49 source maps / 540,083 bytes; glsx-applet-component is 118 files / 423,591 bytes.

Task 1: Migration evidence and dependency RED test

Files:

  • Create: docs/migrations/miniprogram-npm-size-reduction/manifest.json
  • Create: docs/migrations/miniprogram-npm-size-reduction/api-inventory.md
  • Create: docs/migrations/miniprogram-npm-size-reduction/test-acceptance.md
  • Create: tests/work-order-migration/npm-dependency-prune.test.mjs

Interfaces:

  • Consumes: repository package manifests and the confirmed design in docs/superpowers/specs/2026-08-12-miniprogram-npm-size-reduction-design.md.
  • Produces: a migration manifest accepted by check:migration, plus a failing test proving the unused plugin still exists before implementation.

  • Step 1: Write the dependency test

Create tests/work-order-migration/npm-dependency-prune.test.mjs:

import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, it } from 'node:test'

const projectRoot = path.resolve(
  path.dirname(fileURLToPath(import.meta.url)),
  '../..'
)

describe('mini-program npm dependency pruning', () => {
  it('removes the unused glsx component plugin from every dependency manifest', async () => {
    const packageJson = JSON.parse(
      await readFile(path.join(projectRoot, 'package.json'), 'utf8')
    )
    const packageLock = JSON.parse(
      await readFile(path.join(projectRoot, 'package-lock.json'), 'utf8')
    )
    const yarnLock = await readFile(path.join(projectRoot, 'yarn.lock'), 'utf8')

    assert.equal(packageJson.dependencies['glsx-applet-component'], undefined)
    assert.equal(
      packageLock.packages[''].dependencies['glsx-applet-component'],
      undefined
    )
    assert.equal(
      packageLock.packages['node_modules/glsx-applet-component'],
      undefined
    )
    assert.doesNotMatch(yarnLock, /^glsx-applet-component@/m)
  })

  it('keeps the privacy component dependency used by app.json', async () => {
    const packageJson = JSON.parse(
      await readFile(path.join(projectRoot, 'package.json'), 'utf8')
    )
    const appConfig = JSON.parse(
      await readFile(path.join(projectRoot, 'app.json'), 'utf8')
    )

    assert.equal(packageJson.dependencies['glsk-applet-component'], '^1.0.0')
    assert.equal(
      appConfig.usingComponents['glsx-privacy'],
      'glsk-applet-component/GlsxPrivacy/index'
    )
  })
})
  • Step 2: Run the test and verify RED

Run:

node scripts/run-node-tests.mjs tests/work-order-migration/npm-dependency-prune.test.mjs

Expected: FAIL because package.json, package-lock.json, and yarn.lock still contain glsx-applet-component; the privacy-component preservation test passes.

  • Step 3: Create the migration evidence files

Write api-inventory.md with these exact facts:

# miniprogram_npm 体积优化接口清单

## 业务接口

本次迁移不新增、删除或修改任何业务 API、请求参数、响应结构或页面路由。

## 构建接口

- `project.config.json.setting.packNpmManually = true`
- `project.config.json.setting.packNpmRelationList[0] = { "packageJsonPath": "./package.json", "miniprogramNpmDistDir": "./" }`
- `pruneGeneratedNpmArtifacts(projectRoot)` 返回 `{ removedNestedNpm: boolean, removedSourceMaps: number }`。
- `scripts/wx-upload-guard.js` 在静态依赖闭包检查前调用裁剪函数。

## 运行时依赖

- 保留:`glsk-applet-component/GlsxPrivacy/index`。
- 删除:仓库业务源码无引用的 `glsx-applet-component`。

Write test-acceptance.md with the baseline table from Global Constraints, the expected post-state (zero source maps, no generated glsx-applet-component, privacy component retained), and verification commands npm run test:workOrderMigration and npm run check:workOrderClosure.

Write manifest.json with:

{
  "id": "miniprogram-npm-size-reduction",
  "title": "miniprogram_npm 插件与上传产物体积优化",
  "sourceProject": "dj-car-boutique-work-wechat",
  "affectedPaths": [
    "package.json",
    "package-lock.json",
    "yarn.lock",
    "project.config.json",
    "project.private.config.json",
    "scripts/wx-upload-guard.js",
    "scripts/prune-miniprogram-npm.js",
    "tests/work-order-migration/upload-config.test.mjs",
    "tests/work-order-migration/npm-upload-prune.test.mjs",
    "tests/work-order-migration/npm-dependency-prune.test.mjs",
    "docs/migrations/miniprogram-npm-size-reduction"
  ],
  "interfaces": [
    "WeChat DevTools manual npm packaging configuration",
    "pruneGeneratedNpmArtifacts(projectRoot)"
  ],
  "tests": [
    {
      "id": "npm-dependency-prune",
      "file": "tests/work-order-migration/npm-dependency-prune.test.mjs",
      "level": "unit",
      "expectedBefore": "fail"
    },
    {
      "id": "npm-artifact-prune",
      "file": "tests/work-order-migration/npm-upload-prune.test.mjs",
      "level": "unit",
      "expectedBefore": "pass"
    },
    {
      "id": "upload-config-boundary",
      "file": "tests/work-order-migration/upload-config.test.mjs",
      "level": "integration",
      "expectedBefore": "pass"
    }
  ],
  "baseline": {
    "command": "git rev-parse --short 70c239a",
    "result": "70c239a"
  },
  "apiInventory": "docs/migrations/miniprogram-npm-size-reduction/api-inventory.md",
  "acceptance": "docs/migrations/miniprogram-npm-size-reduction/test-acceptance.md"
}
  • Step 4: Run the migration pre-gate

Run:

npm run check:migration -- --manifest docs/migrations/miniprogram-npm-size-reduction/manifest.json --phase pre

Expected: Migration readiness pre check passed.

  • Step 5: Commit migration preparation only
git add -- docs/migrations/miniprogram-npm-size-reduction tests/work-order-migration/npm-dependency-prune.test.mjs
git commit -m "test: gate miniprogram npm pruning"

Do not stage config.js or any implementation files in this commit.


Task 2: Complete and verify upload artifact pruning

Files:

  • Modify: tests/work-order-migration/npm-upload-prune.test.mjs
  • Modify: tests/work-order-migration/upload-config.test.mjs
  • Verify: scripts/prune-miniprogram-npm.js
  • Verify: scripts/wx-upload-guard.js
  • Verify: project.config.json
  • Verify: project.private.config.json

Interfaces:

  • Consumes: pruneGeneratedNpmArtifacts(projectRoot) and WeChat setting / packOptions configuration.
  • Produces: regression coverage for safe repeated pruning and exact manual npm packaging configuration.

  • Step 1: Add idempotence and manual-packaging assertions

In npm-upload-prune.test.mjs, call pruneGeneratedNpmArtifacts(fixtureRoot) a second time and assert:

assert.deepEqual(pruneGeneratedNpmArtifacts(fixtureRoot), {
  removedNestedNpm: false,
  removedSourceMaps: 0
})

In upload-config.test.mjs, add:

assert.equal(config.setting?.packNpmManually, true)
assert.deepEqual(config.setting?.packNpmRelationList, [
  {
    packageJsonPath: './package.json',
    miniprogramNpmDistDir: './'
  }
])
  • Step 2: Run focused pruning/config tests

Run:

node scripts/run-node-tests.mjs tests/work-order-migration/npm-upload-prune.test.mjs tests/work-order-migration/upload-config.test.mjs

Expected: PASS. These assertions validate the already-present user-authored implementation; if a test fails, make the smallest change inside the listed implementation files and rerun it.

  • Step 3: Review the destructive boundary

Confirm scripts/prune-miniprogram-npm.js resolves miniprogram_npm from projectRoot, deletes only miniprogram_npm/miniprogram_npm recursively and .map files discovered below miniprogram_npm, and returns counts. Confirm wx-upload-guard.js invokes it before check-work-order-migration.mjs.

  • Step 4: Commit the verified pruning boundary
git add -- project.config.json project.private.config.json scripts/prune-miniprogram-npm.js scripts/wx-upload-guard.js tests/work-order-migration/npm-upload-prune.test.mjs tests/work-order-migration/upload-config.test.mjs
git commit -m "build: prune mini-program npm upload artifacts"

Do not stage config.js, yarn.lock, or package manifests in this commit.


Task 3: Remove the unused component plugin

Files:

  • Modify: package.json
  • Modify: package-lock.json
  • Modify: yarn.lock
  • Test: tests/work-order-migration/npm-dependency-prune.test.mjs

Interfaces:

  • Consumes: package-manager dependency records.
  • Produces: manifests that omit glsx-applet-component while preserving glsk-applet-component@^1.0.0.

  • Step 1: Remove the dependency with surgical edits

Use apply_patch to remove the "glsx-applet-component": "^1.1.29" property from package.json, its root dependency and node_modules/glsx-applet-component package object from package-lock.json, and only its stanza from the already-modified yarn.lock. Preserve every unrelated deletion already present in yarn.lock.

  • Step 2: Run the dependency test and verify GREEN

Run:

node scripts/run-node-tests.mjs tests/work-order-migration/npm-dependency-prune.test.mjs

Expected: both tests PASS.

  • Step 3: Prove no runtime source refers to the removed plugin

Run:

rg -n --hidden -g '!node_modules/**' -g '!miniprogram_npm/**' -g '!docs/**' -g '!tests/**' -g '!package.json' -g '!package-lock.json' -g '!yarn.lock' 'glsx-applet-component' .

Expected: no matches and exit code 1. Then run:

rg -n 'glsk-applet-component/GlsxPrivacy/index' app.json

Expected: exactly one retained runtime registration.

  • Step 4: Commit dependency removal
git add -- package.json package-lock.json yarn.lock tests/work-order-migration/npm-dependency-prune.test.mjs
git commit -m "build: remove unused mini-program component plugin"

Task 4: Prune generated output and close the migration gate

Files:

  • Modify: docs/migrations/miniprogram-npm-size-reduction/manifest.json
  • Modify: docs/migrations/miniprogram-npm-size-reduction/test-acceptance.md
  • Locally regenerate/prune but do not commit: miniprogram_npm/**

Interfaces:

  • Consumes: final manifests, pruning function, and generated npm output.
  • Produces: measured post-state plus passing migration post-gate evidence.

  • Step 1: Verify and remove the exact generated targets

Resolve miniprogram_npm/glsx-applet-component and confirm its absolute path is below the repository's absolute miniprogram_npm directory before deleting it recursively. This is a generated, git-ignored directory and is recoverable through WeChat DevTools “工具 → 构建 npm”. Then run:

node -e "const { pruneGeneratedNpmArtifacts } = require('./scripts/prune-miniprogram-npm.js'); console.log(pruneGeneratedNpmArtifacts(process.cwd()))"

Expected on the current baseline: removedSourceMaps: 49; removedNestedNpm reflects whether DevTools created a nested duplicate directory.

  • Step 2: Measure the resulting generated output

Run:

$files = Get-ChildItem -LiteralPath miniprogram_npm -Recurse -File
$bytes = ($files | Measure-Object Length -Sum).Sum
$maps = Get-ChildItem -LiteralPath miniprogram_npm -Recurse -File -Filter *.map
[pscustomobject]@{ Files = $files.Count; Bytes = $bytes; MiB = [math]::Round($bytes / 1MB, 3); SourceMaps = $maps.Count; RemovedPluginExists = Test-Path 'miniprogram_npm/glsx-applet-component'; PrivacyPluginExists = Test-Path 'miniprogram_npm/glsk-applet-component' }

Expected: SourceMaps = 0, RemovedPluginExists = false, PrivacyPluginExists = true, and total bytes are at most 700,000 (about 0.65 MiB from the recorded baseline).

  • Step 3: Run complete verification

Run:

npm run test:workOrderMigration
npm run check:workOrderClosure
git diff --check

Expected: all tests pass, dependency closure reports no missing runtime files, and diff check returns no errors.

  • Step 4: Record evidence and prepare post manifest

Update test-acceptance.md with the exact post measurement and test counts. In manifest.json, change expectedBefore to pass for npm-dependency-prune, set every test entry's status to pass, and add:

"verification": {
  "commands": [
    {
      "command": "npm run test:workOrderMigration",
      "status": "pass"
    },
    {
      "command": "npm run check:workOrderClosure",
      "status": "pass"
    }
  ]
}
  • Step 5: Run the migration post-gate

Run:

npm run check:migration -- --manifest docs/migrations/miniprogram-npm-size-reduction/manifest.json --phase post

Expected: both verification commands rerun successfully and Migration readiness post check passed is printed.

  • Step 6: Commit final evidence
git add -- docs/migrations/miniprogram-npm-size-reduction
git commit -m "docs: record miniprogram npm reduction evidence"

Finally run git status --short and verify that only unrelated user changes such as config.js remain; never stage them.