Commit 7b9cdeed by 张立顺

build: prune mini-program npm upload artifacts

1 parent 004711a5
...@@ -21,6 +21,26 @@ ...@@ -21,6 +21,26 @@
"type": "folder" "type": "folder"
}, },
{ {
"value": ".superpowers",
"type": "folder"
},
{
"value": "outputs",
"type": "folder"
},
{
"value": "typings",
"type": "folder"
},
{
"value": "test",
"type": "folder"
},
{
"value": "miniprogram_npm/miniprogram_npm",
"type": "folder"
},
{
"value": "docs", "value": "docs",
"type": "folder" "type": "folder"
}, },
...@@ -43,6 +63,38 @@ ...@@ -43,6 +63,38 @@
{ {
"value": "package-lock.json", "value": "package-lock.json",
"type": "file" "type": "file"
},
{
"value": "project.private.config.json",
"type": "file"
},
{
"value": "jsconfig.json",
"type": "file"
},
{
"value": "yarn.lock",
"type": "file"
},
{
"value": "prettier-plugin-wxml.mjs",
"type": "file"
},
{
"value": ".prettierrc.mjs",
"type": "file"
},
{
"value": ".editorconfig",
"type": "file"
},
{
"value": "AGENTS.md",
"type": "file"
},
{
"value": "README.md",
"type": "file"
} }
], ],
"include": [] "include": []
...@@ -72,10 +124,10 @@ ...@@ -72,10 +124,10 @@
"sass" "sass"
], ],
"ignoreDevUnusedFiles": false, "ignoreDevUnusedFiles": false,
"ignoreUploadUnusedFiles": false, "ignoreUploadUnusedFiles": true,
"compileWorklet": false, "compileWorklet": false,
"uglifyFileName": false, "uglifyFileName": false,
"uploadWithSourceMap": true, "uploadWithSourceMap": false,
"minifyWXSS": true, "minifyWXSS": true,
"minifyWXML": true, "minifyWXML": true,
"localPlugins": false, "localPlugins": false,
......
...@@ -19,6 +19,6 @@ ...@@ -19,6 +19,6 @@
"showES6CompileOption": false, "showES6CompileOption": false,
"checkInvalidKey": true, "checkInvalidKey": true,
"ignoreDevUnusedFiles": false, "ignoreDevUnusedFiles": false,
"bigPackageSizeSupport": false "bigPackageSizeSupport": true
} }
} }
const fs = require('node:fs')
const path = require('node:path')
function collectSourceMaps(directory, result = []) {
if (!fs.existsSync(directory)) return result
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const entryPath = path.join(directory, entry.name)
if (entry.isDirectory()) {
collectSourceMaps(entryPath, result)
} else if (entry.isFile() && entry.name.endsWith('.map')) {
result.push(entryPath)
}
}
return result
}
function pruneGeneratedNpmArtifacts(projectRoot) {
const npmRoot = path.resolve(projectRoot, 'miniprogram_npm')
const nestedNpmRoot = path.join(npmRoot, 'miniprogram_npm')
const removedNestedNpm = fs.existsSync(nestedNpmRoot)
if (removedNestedNpm) {
fs.rmSync(nestedNpmRoot, { recursive: true, force: true })
}
const sourceMaps = collectSourceMaps(npmRoot)
for (const sourceMap of sourceMaps) {
fs.rmSync(sourceMap, { force: true })
}
return {
removedNestedNpm,
removedSourceMaps: sourceMaps.length
}
}
module.exports = { pruneGeneratedNpmArtifacts }
const path = require('node:path') const path = require('node:path')
const { spawnSync } = require('node:child_process') const { spawnSync } = require('node:child_process')
const { pruneGeneratedNpmArtifacts } = require('./prune-miniprogram-npm.js')
const projectRoot = path.resolve(__dirname, '..') const projectRoot = path.resolve(__dirname, '..')
const nodeMajor = Number(process.versions.node.split('.')[0]) const nodeMajor = Number(process.versions.node.split('.')[0])
...@@ -11,6 +12,11 @@ if (nodeMajor < 24) { ...@@ -11,6 +12,11 @@ if (nodeMajor < 24) {
process.exit(1) process.exit(1)
} }
const pruneResult = pruneGeneratedNpmArtifacts(projectRoot)
console.log(
`[wx-upload-guard] npm artifacts pruned; nested=${pruneResult.removedNestedNpm}; sourceMaps=${pruneResult.removedSourceMaps}`
)
const result = spawnSync( const result = spawnSync(
process.execPath, process.execPath,
[path.join(__dirname, 'check-work-order-migration.mjs')], [path.join(__dirname, 'check-work-order-migration.mjs')],
...@@ -28,4 +34,3 @@ if (result.error) { ...@@ -28,4 +34,3 @@ if (result.error) {
} }
process.exit(result.status ?? 1) process.exit(result.status ?? 1)
import assert from 'node:assert/strict'
import { existsSync } from 'node:fs'
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import { describe, it } from 'node:test'
const require = createRequire(import.meta.url)
const testDir = path.dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(testDir, '../..')
describe('npm upload artifact pruning', () => {
it('removes only nested npm output and source maps', async t => {
const fixtureRoot = await mkdtemp(path.join(os.tmpdir(), 'wx-npm-prune-'))
t.after(() => rm(fixtureRoot, { recursive: true, force: true }))
const npmRoot = path.join(fixtureRoot, 'miniprogram_npm')
const packageRoot = path.join(npmRoot, 'example')
const nestedRoot = path.join(npmRoot, 'miniprogram_npm')
await mkdir(packageRoot, { recursive: true })
await mkdir(nestedRoot, { recursive: true })
await writeFile(path.join(packageRoot, 'index.js'), 'module.exports = {}')
await writeFile(path.join(packageRoot, 'index.js.map'), '{}')
await writeFile(path.join(nestedRoot, 'duplicate.js'), 'duplicate')
await writeFile(path.join(fixtureRoot, 'keep.map'), 'outside npm output')
const { pruneGeneratedNpmArtifacts } = require(
'../../scripts/prune-miniprogram-npm.js'
)
const result = pruneGeneratedNpmArtifacts(fixtureRoot)
assert.equal(result.removedNestedNpm, true)
assert.equal(result.removedSourceMaps, 1)
assert.equal(existsSync(nestedRoot), false)
assert.equal(existsSync(path.join(packageRoot, 'index.js.map')), false)
assert.equal(
await readFile(path.join(packageRoot, 'index.js'), 'utf8'),
'module.exports = {}'
)
assert.equal(
await readFile(path.join(fixtureRoot, 'keep.map'), 'utf8'),
'outside npm output'
)
assert.deepEqual(pruneGeneratedNpmArtifacts(fixtureRoot), {
removedNestedNpm: false,
removedSourceMaps: 0
})
})
it('runs pruning from the existing upload guard', async () => {
const guardSource = await readFile(
path.join(projectRoot, 'scripts/wx-upload-guard.js'),
'utf8'
)
assert.match(guardSource, /pruneGeneratedNpmArtifacts\(projectRoot\)/)
})
})
...@@ -39,6 +39,38 @@ describe('小程序上传边界', () => { ...@@ -39,6 +39,38 @@ describe('小程序上传边界', () => {
} }
}) })
it('上传会过滤未使用文件、源映射和重复 npm 产物', async () => {
const config = JSON.parse(
await readFile(path.join(projectRoot, 'project.config.json'), 'utf8')
)
const ignored = new Set(
(config.packOptions?.ignore || []).map(item => item.value)
)
assert.equal(config.setting?.ignoreUploadUnusedFiles, true)
assert.equal(config.setting?.uploadWithSourceMap, false)
assert.equal(config.setting?.packNpmManually, true)
assert.deepEqual(config.setting?.packNpmRelationList, [
{
packageJsonPath: './package.json',
miniprogramNpmDistDir: './'
}
])
for (const entry of [
'miniprogram_npm/miniprogram_npm',
'outputs',
'typings',
'.superpowers',
'test',
'project.private.config.json',
'yarn.lock',
'prettier-plugin-wxml.mjs'
]) {
assert.ok(ignored.has(entry), `未忽略上传非运行时内容:${entry}`)
}
})
it('上传前守卫脚本存在并执行静态闭包检查', async () => { it('上传前守卫脚本存在并执行静态闭包检查', async () => {
const guardPath = path.join(projectRoot, 'scripts/wx-upload-guard.js') const guardPath = path.join(projectRoot, 'scripts/wx-upload-guard.js')
assert.ok((await stat(guardPath)).isFile()) assert.ok((await stat(guardPath)).isFile())
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!