Skip to content

Commit 4cc6c31

Browse files
committed
feat(compiler): add --force option to recompile all mini programs
1 parent e8fe591 commit 4cc6c31

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

fe/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pnpm install
3232
# 编译 example/ 目录下的所有小程序
3333
pnpm compile
3434

35+
# 忽略本地编译缓存,强制重新编译 example/ 目录下的所有小程序
36+
pnpm compile --force
37+
3538
# 构建(开发环境,不压缩)
3639
pnpm build:dev
3740

@@ -61,6 +64,22 @@ pnpm generate:sdk
6164

6265
### 资源生成工具
6366

67+
#### pnpm compile
68+
69+
编译 `example/` 目录下的所有小程序,并将产物输出到 `packages/container/public`
70+
71+
默认情况下,命令会读取 `packages/container/public/compile-cache.json`,跳过未发生变化的小程序以提升编译速度。如需忽略本地缓存并重新编译全部示例小程序,可以执行:
72+
73+
```sh
74+
pnpm compile --force
75+
```
76+
77+
也支持简写参数:
78+
79+
```sh
80+
pnpm compile -f
81+
```
82+
6483
#### pnpm generate:app
6584

6685
将编译好的小程序打包并复制到共享目录 `shared/jsapp` 中。

fe/packages/compiler/src/bin/compile.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function saveCache(cache) {
2222
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2))
2323
}
2424

25+
function parseOptions(argv = process.argv.slice(2)) {
26+
return {
27+
force: argv.includes('--force') || argv.includes('-f') || argv.includes('force'),
28+
}
29+
}
30+
2531
function isModified(dirPath, lastCompileTime) {
2632
const files = fs.readdirSync(dirPath)
2733
for (const file of files) {
@@ -120,12 +126,13 @@ async function cleanUpOldApps(targetPath, appList) {
120126
}
121127
}
122128

123-
async function buildMiniApp() {
129+
async function buildMiniApp(options = {}) {
130+
const { force = false } = options
124131
const currentDirectory = `${process.cwd()}/example`
125132
const cache = loadCache()
126133

127134
// 检查编译器是否被修改
128-
const compilerModified = isCompilerModified(cache.compilerLastModified)
135+
const compilerModified = force || isCompilerModified(cache.compilerLastModified)
129136

130137
fs.readdir(currentDirectory, async (err, files) => {
131138
if (err) {
@@ -148,7 +155,7 @@ async function buildMiniApp() {
148155
const lastCompileTime = cache.apps[fileName]?.lastCompileTime || 0
149156

150157
// 检查是否需要重新编译
151-
if (compilerModified || isModified(workPath, lastCompileTime)) {
158+
if (force || compilerModified || isModified(workPath, lastCompileTime)) {
152159
const appInfo = await build(targetPath, workPath)
153160
if (appInfo) {
154161
appList.push(appInfo)
@@ -206,4 +213,4 @@ function getLastCompileTime(data, appId) {
206213
return 0 // 如果找不到对应的 appId
207214
}
208215

209-
buildMiniApp()
216+
buildMiniApp(parseOptions())

0 commit comments

Comments
 (0)