@playcraft/devkit 1.0.31-beta.1 → 1.0.31-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +262 -0
- package/cli/bin/playable-scripts.js +53 -6
- package/cli/commands/build.js +241 -32
- package/cli/commands/builds.js +13 -2
- package/cli/commands/dev.js +22 -1
- package/cli/commands/pack.js +323 -41
- package/cli/commands/vite-dev.js +7 -1
- package/core/artifacts/emit.js +98 -1
- package/core/artifacts/fflate.js +31 -1
- package/core/artifacts/zip.js +39 -1
- package/core/batch-build.js +1261 -149
- package/core/constants.js +20 -2
- package/core/cos-uploader.js +289 -23
- package/core/engines/cocos/clean-cache.js +188 -0
- package/core/engines/cocos/clean-cache.test.js +75 -0
- package/core/engines/cocos/cli-build.js +116 -2
- package/core/engines/cocos/cli.js +38 -1
- package/core/engines/cocos/constants.js +21 -1
- package/core/engines/cocos/creator-cli.js +108 -1
- package/core/engines/cocos/dev-server.js +412 -2
- package/core/engines/cocos/fs.js +105 -1
- package/core/engines/cocos/injects/3x/init.js +438 -19
- package/core/engines/cocos/injects/3x/main.js +52 -2
- package/core/engines/cocos/injects/libs/pako.js +2 -1
- package/core/engines/cocos/mcp-refresh.js +76 -2
- package/core/engines/cocos/merge.js +147 -2
- package/core/engines/cocos/pack.js +187 -1
- package/core/engines/cocos/preview-cache.js +51 -0
- package/core/engines/cocos/preview-cache.test.js +32 -0
- package/core/engines/cocos/preview-concat.js +192 -0
- package/core/engines/cocos/preview-concat.test.js +73 -0
- package/core/engines/cocos/preview-json-hook.js +200 -0
- package/core/engines/cocos/preview-json-pack.js +379 -0
- package/core/engines/cocos/preview-json-pack.test.js +226 -0
- package/core/engines/cocos/preview-proxy.js +132 -0
- package/core/engines/cocos/rebundle-preview.js +56 -0
- package/core/engines/cocos/strip-social-share.js +115 -3
- package/core/engines/cocos/theme-assets.js +161 -2
- package/core/engines/cocos/writable-cli.js +250 -0
- package/core/engines/cocos/writable-cli.test.js +112 -0
- package/core/engines/web/index.js +20 -1
- package/core/engines/web/vite.dev.js +322 -17
- package/core/engines/web/webpack.build.js +522 -65
- package/core/engines/web/webpack.common.js +177 -11
- package/core/engines/web/webpack.dev.js +88 -6
- package/core/index.d.ts +3 -0
- package/core/index.js +101 -7
- package/core/injectors/apply.js +94 -1
- package/core/injectors/check-channel-matrix.js +130 -2
- package/core/injectors/spec.js +217 -9
- package/core/loaders/gltf-loader.js +74 -28
- package/core/options.js +353 -16
- package/core/plugins/AdikteevInjectorPlugin.js +26 -4
- package/core/plugins/BigoAdsInjectorPlugin.js +21 -4
- package/core/plugins/DAPIInjectorPlugin.js +25 -2
- package/core/plugins/DebuggerInjectionPlugin.js +31 -3
- package/core/plugins/ExitAPIInjectorPlugin.js +56 -3
- package/core/plugins/FflateCompressionPlugin.js +231 -40
- package/core/plugins/FontInjectionWebpackPlugin.js +46 -4
- package/core/plugins/LiftoffInjectorPlugin.js +26 -4
- package/core/plugins/MRAIDInjectorPlugin.js +24 -2
- package/core/plugins/MintegralInjectorPlugin.js +25 -2
- package/core/plugins/PangleInjectorPlugin.js +24 -2
- package/core/plugins/PlaycraftOrientationInjectorPlugin.js +29 -3
- package/core/plugins/SnapchatInjectorPlugin.js +26 -4
- package/core/plugins/SocialMediaInjectorPlugin.js +119 -7
- package/core/plugins/TikTokInjectorPlugin.js +24 -2
- package/core/plugins/UnityInjectorPlugin.js +37 -8
- package/core/plugins/ZipPlugin.js +154 -20
- package/core/plugins/socialMediaVitePlugin.js +108 -2
- package/core/utils/buildDefines.js +30 -2
- package/core/utils/buildTemplateString.js +39 -5
- package/core/utils/date.js +16 -1
- package/core/utils/fontRegistry.js +81 -13
- package/core/utils/fontResolver.js +335 -37
- package/core/utils/generateAdikteevHtmlWebpackPluginConfig.js +42 -7
- package/core/utils/generateBigabidHtmlWebpackPluginConfig.js +30 -3
- package/core/utils/generateInMobiHtmlWebpackPluginConfig.js +43 -7
- package/core/utils/getPlaycraftOrientationSuffix.js +9 -2
- package/core/utils/injectSDKPlugins.js +68 -13
- package/core/utils/logOptions.js +46 -5
- package/core/utils/mergeOptions.js +21 -2
- package/core/utils/parseArgvOptions.js +125 -5
- package/core/utils/resolveChannelFold2Zip.js +15 -3
- package/core/utils/validateThemeData.js +227 -26
- package/core/validators/index.d.ts +4 -0
- package/core/validators/pre-build-checker.js +287 -25
- package/core/validators/tracking-validator.js +444 -43
- package/core/validators/tracking-validator.test.js +92 -0
- package/core/vite.dev.js +1 -1
- package/core/webpack.build.js +1 -1
- package/core/webpack.common.js +1 -1
- package/core/webpack.dev.js +1 -1
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# @tencent/playable-scripts
|
|
2
|
+
|
|
3
|
+
HTML5 Playable Ads 构建工具,支持多广告渠道打包。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 **一键构建** - 轻松为不同广告渠道生成构建包
|
|
8
|
+
- ⚡ **自动优化** - 包含压缩、Tree-shaking 和死代码消除
|
|
9
|
+
- 🗜️ **fflate 压缩** - 编译时压缩,运行时解压,体积减少 40-60%
|
|
10
|
+
- 🎯 **多渠道支持** - 支持 Google Ads、Meta、AppLovin、Unity、IronSource、BigoAds 等
|
|
11
|
+
- 🎨 **主题系统** - 支持多主题批量构建
|
|
12
|
+
- 🔄 **并发构建** - 智能并发,大幅提升构建效率
|
|
13
|
+
|
|
14
|
+
## 快速开始
|
|
15
|
+
|
|
16
|
+
### 安装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
tnpm install --save-dev @tencent/playable-scripts
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 配置 package.json
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"scripts": {
|
|
27
|
+
"dev": "playable-scripts dev",
|
|
28
|
+
"build": "playable-scripts build",
|
|
29
|
+
"builds": "playable-scripts builds"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 启动开发服务器
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm run dev # 默认预览模式
|
|
38
|
+
npm run dev bigoads # 指定渠道调试(简写)
|
|
39
|
+
npm run dev -- --network social_media # 指定渠道调试(完整参数)
|
|
40
|
+
npm run dev -- --port 8080 # 指定端口
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 构建指定渠道
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm run build google # Google Ads
|
|
47
|
+
npm run build facebook # Meta
|
|
48
|
+
npm run build unity # Unity Ads
|
|
49
|
+
npm run build bigoads # BigoAds
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 批量构建
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm run builds # 构建所有渠道
|
|
56
|
+
npm run builds -- -t "theme1,theme2" # 指定主题
|
|
57
|
+
npm run builds -- -c "google,facebook" # 指定渠道
|
|
58
|
+
npm run builds -- --parallel 4 # 指定并发数
|
|
59
|
+
|
|
60
|
+
# Windows PowerShell 兼容格式(推荐)
|
|
61
|
+
npm run builds -- "theme1" "google" # 位置参数:主题 渠道
|
|
62
|
+
npm run builds -- --themes="theme1,theme2" --channels="google,facebook"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 支持的广告渠道
|
|
66
|
+
|
|
67
|
+
| 渠道 | 命令 | 协议 |
|
|
68
|
+
|------|------|------|
|
|
69
|
+
| Preview (预览) | `preview` | none |
|
|
70
|
+
| Google Ads | `google` | none |
|
|
71
|
+
| Meta (Facebook) | `facebook` | none(ZIP,仅 index.html,上限 5MB) |
|
|
72
|
+
| AppLovin | `applovin` | mraid |
|
|
73
|
+
| Unity Ads | `unity` | mraid |
|
|
74
|
+
| IronSource | `ironsource` | mraid/dapi |
|
|
75
|
+
| TikTok / Pangle | `tiktok` | none |
|
|
76
|
+
| BigoAds | `bigoads` | bigo-mraid |
|
|
77
|
+
| Adikteev | `adikteev` | mraid |
|
|
78
|
+
| Moloco | `moloco` | none |
|
|
79
|
+
| Liftoff | `liftoff` | mraid (ZIP) |
|
|
80
|
+
| InMobi | `inmobi` | mraid |
|
|
81
|
+
| Snapchat | `snapchat` | snapchat |
|
|
82
|
+
| Remerge | `remerge` | mraid (ZIP) |
|
|
83
|
+
| Mintegral | `mintegral` | none |
|
|
84
|
+
|
|
85
|
+
## CLI 参数
|
|
86
|
+
|
|
87
|
+
| 参数 | 描述 | 默认值 |
|
|
88
|
+
|------|----------------|-----------|
|
|
89
|
+
| **广告渠道** | 目标广告渠道 | `preview` |
|
|
90
|
+
| `--out-dir` | 输出目录 | `dist` |
|
|
91
|
+
| `--obfuscate-level` | 代码混淆等级 (1-4) | `4` |
|
|
92
|
+
| `--fflate-compression` | 启用 fflate 压缩 | `true` |
|
|
93
|
+
| `--protocol` | 广告协议 | 渠道自动决定 |
|
|
94
|
+
| `--zip` | ZIP 输出结果 | `false` |
|
|
95
|
+
| `--port` | 开发服务器端口 | `3000` |
|
|
96
|
+
| `--open` | 自动打开浏览器 | `false` |
|
|
97
|
+
| `--language` | 语言代码 | `en` |
|
|
98
|
+
| `--is-channel-fold2zip ` | 所有渠道输出为 ZIP 结果 | `false` |
|
|
99
|
+
|
|
100
|
+
## 配置文件
|
|
101
|
+
|
|
102
|
+
支持两种配置文件(推荐使用 `builds.config.js`):
|
|
103
|
+
|
|
104
|
+
### builds.config.js(推荐)
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
module.exports = {
|
|
108
|
+
// 项目基本信息
|
|
109
|
+
projectName: 'My-Game', // 可选,默认 'Phaser Game'
|
|
110
|
+
googlePlayUrl: 'https://play.google.com/...', // 可选
|
|
111
|
+
appStoreUrl: 'https://apps.apple.com/...', // 可选
|
|
112
|
+
|
|
113
|
+
// 渠道配置
|
|
114
|
+
channels: ['google', 'facebook', 'unity'], // 可选,有默认值
|
|
115
|
+
|
|
116
|
+
// 主题配置(可选)
|
|
117
|
+
themes: {
|
|
118
|
+
enabled: true, // 默认 false
|
|
119
|
+
sourceDir: 'src/theme', // 默认 'src/theme'
|
|
120
|
+
entryFile: 'src/theme/index.ts', // 默认 'src/theme/index.ts'
|
|
121
|
+
whitelist: null, // 可选,过滤函数
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
// 文件命名配置(可选)
|
|
125
|
+
naming: {
|
|
126
|
+
language: 'EN', // 默认 'EN'
|
|
127
|
+
format: '无', // 默认 '无'
|
|
128
|
+
supplier: 'AIX', // 默认 'AIX'
|
|
129
|
+
follower: 'ZQL', // 默认 'ZQL'
|
|
130
|
+
generator: null, // 可选,自定义生成函数
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
// 构建配置(可选)
|
|
134
|
+
build: {
|
|
135
|
+
outputDir: 'dist-all', // 默认 'dist-all'
|
|
136
|
+
tempDirPrefix: 'dist-temp', // 默认 'dist-temp'
|
|
137
|
+
continueOnError: true, // 默认 true
|
|
138
|
+
cleanTemp: true, // 默认 true
|
|
139
|
+
parallel: undefined, // 默认自动计算(CPU核心数-1,最小2最大8)
|
|
140
|
+
},
|
|
141
|
+
cosConfig: {
|
|
142
|
+
enabled: true, // 是否启用 COS 上传
|
|
143
|
+
SecretId: 'YOUR_SECRET_ID', // 腾讯云 SecretId
|
|
144
|
+
SecretKey: 'YOUR_SECRET_KEY', // 腾讯云 SecretKey
|
|
145
|
+
Bucket: 'your-bucket-1234567890', // COS 存储桶名称
|
|
146
|
+
Region: 'ap-guangzhou', // COS 区域
|
|
147
|
+
CdnDomain: 'your-cdn-domain.com', // CDN 域名(用于生成访问链接)
|
|
148
|
+
PathPrefix: 'playable/preview', // COS 路径前缀(可选)
|
|
149
|
+
},
|
|
150
|
+
// 打包参数(可选)
|
|
151
|
+
obfuscateLevel: 4, // 默认 4
|
|
152
|
+
fflateCompression: false, // 默认 true
|
|
153
|
+
};
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### build.json(向后兼容)
|
|
157
|
+
|
|
158
|
+
```json
|
|
159
|
+
{
|
|
160
|
+
"app": "AppName",
|
|
161
|
+
"name": "ConceptName",
|
|
162
|
+
"google_play_url": "https://play.google.com/...",
|
|
163
|
+
"app_store_url": "https://apps.apple.com/..."
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 配置优先级
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
命令行参数 > build.json > builds.config.js > 默认值
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## 代码混淆等级
|
|
174
|
+
|
|
175
|
+
| 等级 | 名称 | 说明 | 体积影响 |
|
|
176
|
+
|------|------|------|----------|
|
|
177
|
+
| **1** | terser-only | 仅 Terser 压缩 | 基准 |
|
|
178
|
+
| **2** | basic | 控制流平坦化 + 字符串编码 | +5~10% |
|
|
179
|
+
| **3** | dead-code | 等级 2 + 死代码注入 | +15~25% |
|
|
180
|
+
| **4** | maximum | 全部拉满(默认) | +30~50% |
|
|
181
|
+
|
|
182
|
+
## fflate 压缩
|
|
183
|
+
|
|
184
|
+
| 指标 | 效果 |
|
|
185
|
+
|------|------|
|
|
186
|
+
| **压缩率** | 66-68% |
|
|
187
|
+
| **体积减少** | 40-60% |
|
|
188
|
+
| **解压速度** | 3-5ms |
|
|
189
|
+
| **兼容性** | 99%+ 浏览器 |
|
|
190
|
+
|
|
191
|
+
## 批量构建
|
|
192
|
+
|
|
193
|
+
### 特性
|
|
194
|
+
|
|
195
|
+
1. **智能并发** - 自动根据 CPU 核心数优化(默认:核心数-1,最小2最大8)
|
|
196
|
+
2. **独立临时目录** - 每个任务使用独立目录,避免冲突
|
|
197
|
+
3. **主题切换** - 自动切换主题入口文件
|
|
198
|
+
4. **错误容忍** - 单个失败不影响其他构建
|
|
199
|
+
|
|
200
|
+
### 命令行参数
|
|
201
|
+
|
|
202
|
+
| 参数 | 缩写 | 描述 | 示例 |
|
|
203
|
+
|------|------|------|------|
|
|
204
|
+
| `--themes` | `-t` | 指定主题列表(逗号分隔) | `-t "theme1,theme2"` |
|
|
205
|
+
| `--channels` | `-c` | 指定渠道列表(逗号分隔) | `-c "google,facebook"` |
|
|
206
|
+
| `--parallel` | `-p` | 指定并发数 | `-p 4` |
|
|
207
|
+
| 位置参数 | - | 第一个参数=主题,第二个=渠道 | `"theme1" "google"` |
|
|
208
|
+
| `=` 格式 | - | Windows PowerShell 兼容 | `--themes="theme1"` |
|
|
209
|
+
|
|
210
|
+
**注意**:
|
|
211
|
+
- **Mac/Linux**:所有格式均可使用
|
|
212
|
+
- **Windows PowerShell**:推荐使用位置参数或 `--themes=value` 格式
|
|
213
|
+
- **Windows CMD**:所有格式均可使用
|
|
214
|
+
|
|
215
|
+
### 输出结构
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
dist-all/
|
|
219
|
+
├── theme1/
|
|
220
|
+
│ ├── theme1-EN-无-3月-20260304-AIX-ZQL-google.zip
|
|
221
|
+
│ └── theme1-EN-无-3月-20260304-AIX-ZQL-facebook.zip
|
|
222
|
+
└── theme2/
|
|
223
|
+
└── ...
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
产物直接放在主题目录下,不再套一层 `主题名_渠道名` 文件夹。文件名本身已包含渠道名。
|
|
227
|
+
|
|
228
|
+
## 全局变量
|
|
229
|
+
|
|
230
|
+
构建时自动注入:
|
|
231
|
+
|
|
232
|
+
| 变量 | 描述 | 示例 |
|
|
233
|
+
|------|------|------|
|
|
234
|
+
| `__DEV__` | 开发模式标志 | `true` / `false` |
|
|
235
|
+
| `AD_NETWORK` | 当前广告渠道 | `"google"`, `"bigoads"` |
|
|
236
|
+
| `AD_PROTOCOL` | 当前广告协议 | `"none"`, `"mraid"` |
|
|
237
|
+
| `GOOGLE_PLAY_URL` | Google Play 链接 | `"https://..."` |
|
|
238
|
+
| `APP_STORE_URL` | App Store 链接 | `"https://..."` |
|
|
239
|
+
|
|
240
|
+
## 配套 SDK
|
|
241
|
+
|
|
242
|
+
配合 `@tencent/playable-sdk` 使用:
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
tnpm install @tencent/playable-sdk
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
import { sdk } from '@tencent/playable-sdk';
|
|
250
|
+
|
|
251
|
+
sdk.init((width, height) => {
|
|
252
|
+
new Game(width, height);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
sdk.start(); // 游戏开始
|
|
256
|
+
sdk.finish(); // 游戏结束
|
|
257
|
+
sdk.install(); // 跳转商店
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## 许可证
|
|
261
|
+
|
|
262
|
+
MIT License
|
|
@@ -1,7 +1,54 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
process.on('unhandledRejection', (err) => {
|
|
6
|
+
throw err;
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const spawn = require('cross-spawn');
|
|
10
|
+
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
|
|
13
|
+
const scriptIndex = args.findIndex((x) => x === 'build' || x === 'dev' || x === 'vite-dev' || x === 'builds' || x === 'pack');
|
|
14
|
+
|
|
15
|
+
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
|
|
16
|
+
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
|
|
17
|
+
|
|
18
|
+
if (['build', 'dev', 'vite-dev', 'builds', 'pack'].includes(script)) {
|
|
19
|
+
const result = spawn.sync(
|
|
20
|
+
process.execPath,
|
|
21
|
+
nodeArgs.concat(require.resolve('../commands/' + script)).concat(args.slice(scriptIndex + 1)),
|
|
22
|
+
{
|
|
23
|
+
stdio: 'inherit',
|
|
24
|
+
env: {
|
|
25
|
+
...process.env,
|
|
26
|
+
// 兜底:重度混淆(obfuscateLevel=4,controlFlowFlattening + deadCodeInjection
|
|
27
|
+
// + LimitChunkCountPlugin 单chunk)叠加较大规模源码时,单次 webpack 编译可能
|
|
28
|
+
// 占用数 GB 堆内存,默认的 V8 堆上限不足以支撑,会崩溃为
|
|
29
|
+
// "JavaScript heap out of memory"。此处提高上限做防御
|
|
30
|
+
// (不覆盖用户已自行设置的 NODE_OPTIONS)
|
|
31
|
+
NODE_OPTIONS: process.env.NODE_OPTIONS || '--max-old-space-size=8192'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
if (result.signal) {
|
|
36
|
+
if (result.signal === 'SIGKILL') {
|
|
37
|
+
console.log(
|
|
38
|
+
'The build failed because the process exited too early. ' +
|
|
39
|
+
'This probably means the system ran out of memory or someone called ' +
|
|
40
|
+
'`kill -9` on the process.'
|
|
41
|
+
);
|
|
42
|
+
} else if (result.signal === 'SIGTERM') {
|
|
43
|
+
console.log(
|
|
44
|
+
'The build failed because the process exited too early. ' +
|
|
45
|
+
'Someone might have called `kill` or `killall`, or the system could ' +
|
|
46
|
+
'be shutting down.'
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
process.exit(result.status);
|
|
52
|
+
} else {
|
|
53
|
+
console.log('Unknown script "' + script + '".');
|
|
54
|
+
}
|
package/cli/commands/build.js
CHANGED
|
@@ -1,13 +1,67 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
process.env.BABEL_ENV = 'production';
|
|
4
|
+
process.env.NODE_ENV = 'production';
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { runBuild } = require('../../core/webpack.build.js');
|
|
9
|
+
const { runThemeValidation, restoreSchema } = require('../../core/utils/validateThemeData');
|
|
10
|
+
const { runPreBuildChecks } = require('../../core/validators/pre-build-checker.js');
|
|
11
|
+
const { FontInjectionWebpackPlugin } = require('../../core/plugins/FontInjectionWebpackPlugin.js');
|
|
12
|
+
const { SocialMediaInjectorPlugin } = require('../../core/plugins/SocialMediaInjectorPlugin.js');
|
|
13
|
+
const { getPlaycraftOrientationSuffix } = require('../../core/utils/getPlaycraftOrientationSuffix.js');
|
|
14
|
+
const { options } = require('../../core/options.js');
|
|
15
|
+
const { isWebEngine } = require('../../core/utils/parseArgvOptions.js');
|
|
16
|
+
const { prepareCocosTemplate, emitCocosChannel } = require('../../core/engines/cocos/pack.js');
|
|
17
|
+
|
|
18
|
+
/**
|
|
2
19
|
* 从主题入口文件中检测当前主题名称。
|
|
3
20
|
* 支持以下模式:
|
|
4
21
|
* export { default } from './themeName';
|
|
5
22
|
* import Theme from './themeName';
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
23
|
+
*/
|
|
24
|
+
function detectCurrentTheme(projectRoot) {
|
|
25
|
+
// 尝试加载 builds.config.js 获取主题配置
|
|
26
|
+
const buildsConfigPath = path.join(projectRoot, 'builds.config.js');
|
|
27
|
+
let themeConfig = { sourceDir: 'src/theme', entryFile: 'src/theme/index.ts' };
|
|
28
|
+
|
|
29
|
+
if (fs.existsSync(buildsConfigPath)) {
|
|
30
|
+
try {
|
|
31
|
+
delete require.cache[buildsConfigPath];
|
|
32
|
+
const config = require(buildsConfigPath);
|
|
33
|
+
if (config.themes) {
|
|
34
|
+
themeConfig = { ...themeConfig, ...config.themes };
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
// 忽略错误
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const entryPath = path.join(projectRoot, themeConfig.entryFile);
|
|
42
|
+
if (!fs.existsSync(entryPath)) {
|
|
43
|
+
return { themeName: null, themeConfig };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const content = fs.readFileSync(entryPath, 'utf8');
|
|
47
|
+
|
|
48
|
+
// 匹配: export { default } from './themeName' 或 import ... from './themeName'
|
|
49
|
+
const match = content.match(/from\s+['"]\.\/([^'"]+)['"]/);
|
|
50
|
+
if (match) {
|
|
51
|
+
return { themeName: match[1], themeConfig };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { themeName: null, themeConfig };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// --- 主流程 ---
|
|
58
|
+
const projectRoot = process.cwd();
|
|
59
|
+
const { themeName, themeConfig } = detectCurrentTheme(projectRoot);
|
|
60
|
+
|
|
61
|
+
let schemaPathToRestore = null;
|
|
62
|
+
let isExiting = false;
|
|
63
|
+
|
|
64
|
+
// ============================================================
|
|
11
65
|
// 📌 中断信号处理(Ctrl+C / kill)
|
|
12
66
|
// ============================================================
|
|
13
67
|
// 单主题构建过程中,theme.schema.json5 可能已被 runThemeValidation()
|
|
@@ -15,35 +69,190 @@ const projectRoot=process.cwd();const{themeName,themeConfig}=detectCurrentTheme(
|
|
|
15
69
|
// 避免文件被强制中断而遗留在“已覆写”状态。
|
|
16
70
|
// 注意:SIGKILL(kill -9)无法被捕获,属于无法规避的极端情况,
|
|
17
71
|
// 此时依赖 validateThemeData.js 中的兜底检测(下次构建自动识别并降级处理)。
|
|
18
|
-
function handleInterrupt()
|
|
72
|
+
function handleInterrupt() {
|
|
73
|
+
if (isExiting) return;
|
|
74
|
+
isExiting = true;
|
|
75
|
+
|
|
76
|
+
console.log('\n\x1b[33m收到中断信号,正在清理...\x1b[0m');
|
|
77
|
+
|
|
78
|
+
if (schemaPathToRestore) {
|
|
79
|
+
try {
|
|
80
|
+
restoreSchema(schemaPathToRestore);
|
|
81
|
+
console.log('\x1b[36m已恢复 theme.schema.json5\x1b[0m');
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.error(`\x1b[31m恢复 theme.schema.json5 失败: ${e.message}\x1b[0m`);
|
|
84
|
+
}
|
|
85
|
+
schemaPathToRestore = null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
process.on('SIGINT', handleInterrupt);
|
|
92
|
+
process.on('SIGTERM', handleInterrupt);
|
|
93
|
+
|
|
94
|
+
// ============================================================
|
|
19
95
|
// 📌 编译前检查(埋点验证等)
|
|
20
96
|
// ============================================================
|
|
21
97
|
// 支持通过环境变量控制是否跳过检查
|
|
22
98
|
// - SKIP_PRE_BUILD_CHECKS=1: 强制跳过检查
|
|
23
99
|
// - PLAYABLE_BATCH_MODE=1: 批量构建模式,子进程自动跳过检查(主进程已执行)
|
|
24
|
-
const isBatchMode=process.env.PLAYABLE_BATCH_MODE
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
100
|
+
const isBatchMode = process.env.PLAYABLE_BATCH_MODE === '1';
|
|
101
|
+
const shouldSkipChecks = process.env.SKIP_PRE_BUILD_CHECKS === '1' || isBatchMode;
|
|
102
|
+
|
|
103
|
+
async function runPreChecks() {
|
|
104
|
+
if (shouldSkipChecks) {
|
|
105
|
+
console.log('\n⚠️ 跳过编译前检查(SKIP_PRE_BUILD_CHECKS=1)\n');
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const checkResult = await runPreBuildChecks(projectRoot, {
|
|
110
|
+
verbose: false,
|
|
111
|
+
showDetails: true,
|
|
112
|
+
failFast: false,
|
|
113
|
+
themes: themeName ? [themeName] : undefined,
|
|
114
|
+
channels: options['network'] ? [options['network']] : undefined,
|
|
115
|
+
engine: options.engine,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!checkResult.success) {
|
|
119
|
+
console.error('\n❌ 编译前检查失败,终止构建。');
|
|
120
|
+
console.error('提示:如需跳过检查,可设置环境变量: SKIP_PRE_BUILD_CHECKS=1\n');
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 执行编译前检查(异步)
|
|
126
|
+
runPreChecks().then(() => {
|
|
127
|
+
// 检查通过后继续原有流程
|
|
128
|
+
continueWithBuild();
|
|
129
|
+
}).catch((err) => {
|
|
130
|
+
console.error('\n❌ 编译前检查出错:', err);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
function continueWithBuild() {
|
|
135
|
+
// 当前广告渠道(用于决定是否启用 social_media 的 OG meta 注入)
|
|
136
|
+
const adNetwork = options['network'] || '';
|
|
137
|
+
|
|
138
|
+
// In batch mode (PLAYABLE_BATCH_MODE=1), the parent process has already
|
|
139
|
+
// executed runThemeValidation and overwritten theme.schema.json5 with merged data.
|
|
140
|
+
// Running it again in child processes would cause a race condition:
|
|
141
|
+
// 1. Child reads the already-overwritten schema (pure data, no "properties"/"default")
|
|
142
|
+
// 2. extractDefaultsFromSchema returns {} (empty)
|
|
143
|
+
// 3. Child overwrites schema with {} → file becomes empty object
|
|
144
|
+
const isBatchMode = process.env.PLAYABLE_BATCH_MODE === '1';
|
|
145
|
+
|
|
146
|
+
if (themeName && !isBatchMode) {
|
|
147
|
+
const buildLog = (msg, level) => {
|
|
148
|
+
const colors = { info: '\x1b[36m', success: '\x1b[32m', error: '\x1b[31m' };
|
|
149
|
+
const reset = '\x1b[0m';
|
|
150
|
+
const color = colors[level] || '';
|
|
151
|
+
console.log(`${color}[build] ${msg}${reset}`);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const { valid, schemaPath } = runThemeValidation(themeName, projectRoot, themeConfig, { log: buildLog });
|
|
155
|
+
schemaPathToRestore = schemaPath;
|
|
156
|
+
|
|
157
|
+
if (!valid) {
|
|
158
|
+
// 校验失败时恢复 schema(备份可能已创建)
|
|
159
|
+
if (schemaPathToRestore) restoreSchema(schemaPathToRestore);
|
|
160
|
+
process.exit(1);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ============================================================
|
|
165
|
+
// 📌 横竖屏方向多版本构建
|
|
166
|
+
// ============================================================
|
|
167
|
+
// playcraftOrientationChannel 中指定的渠道,需要按 playcraftOrientation
|
|
168
|
+
// 列出的每个方向版本分别打一个包(渠道_L / 渠道_P / 渠道,即 both 无后缀);
|
|
169
|
+
// 不在列表中的渠道始终只按 both(自适应)打一个包。
|
|
170
|
+
const orientationChannels = options['playcraftOrientationChannel'] || [];
|
|
171
|
+
const orientationVersions =
|
|
172
|
+
Array.isArray(options['playcraftOrientation']) && options['playcraftOrientation'].length > 0
|
|
173
|
+
? options['playcraftOrientation']
|
|
174
|
+
: ['both'];
|
|
175
|
+
const buildOrientations = orientationChannels.includes(adNetwork) ? orientationVersions : ['both'];
|
|
176
|
+
|
|
177
|
+
/**
|
|
41
178
|
* 构建单个方向版本
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
179
|
+
*/
|
|
180
|
+
async function buildOneOrientation(orientationValue) {
|
|
181
|
+
const suffix = getPlaycraftOrientationSuffix(orientationValue);
|
|
182
|
+
if (buildOrientations.length > 1) {
|
|
183
|
+
console.log(`\n📱 开始构建方向版本: ${orientationValue}${suffix ? ` (后缀: ${suffix})` : ''}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 注入字体插件(build 模式 base64 内联)
|
|
187
|
+
const fontPlugin = themeName
|
|
188
|
+
? new FontInjectionWebpackPlugin({ projectRoot, themeName, themeConfig })
|
|
189
|
+
: null;
|
|
190
|
+
|
|
191
|
+
// social_media 渠道:注入 OG / Twitter meta 标签,让 Facebook / LinkedIn
|
|
192
|
+
// 等抓取预览时拿到正确的标题/描述/图片。无 shareMeta 时空跑,不影响构建
|
|
193
|
+
const socialMediaPlugin = (themeName && adNetwork === 'social_media')
|
|
194
|
+
? new SocialMediaInjectorPlugin({ projectRoot, themeName, themeConfig })
|
|
195
|
+
: null;
|
|
196
|
+
|
|
197
|
+
if (!isWebEngine(options.engine)) {
|
|
198
|
+
await runCocosBuild({
|
|
199
|
+
playcraftOrientationValue: orientationValue,
|
|
200
|
+
filename: suffix ? `${options.filename}${suffix}` : options.filename,
|
|
201
|
+
themeName,
|
|
202
|
+
});
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
await runBuild(
|
|
207
|
+
undefined,
|
|
208
|
+
{
|
|
209
|
+
playcraftOrientationValue: orientationValue,
|
|
210
|
+
filename: suffix ? `${options.filename}${suffix}` : options.filename
|
|
211
|
+
},
|
|
212
|
+
undefined,
|
|
213
|
+
{
|
|
214
|
+
plugins: [
|
|
215
|
+
...(fontPlugin ? [fontPlugin] : []),
|
|
216
|
+
...(socialMediaPlugin ? [socialMediaPlugin] : []),
|
|
217
|
+
],
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
(async () => {
|
|
223
|
+
try {
|
|
224
|
+
if (!isWebEngine(options.engine)) {
|
|
225
|
+
const templateHtml = await prepareCocosTemplate({
|
|
226
|
+
playcraftOrientationValue: buildOrientations[0] || 'both',
|
|
227
|
+
});
|
|
228
|
+
for (const orientationValue of buildOrientations) {
|
|
229
|
+
const suffix = getPlaycraftOrientationSuffix(orientationValue);
|
|
230
|
+
if (buildOrientations.length > 1) {
|
|
231
|
+
console.log(`\n📱 开始构建方向版本: ${orientationValue}${suffix ? ` (后缀: ${suffix})` : ''}`);
|
|
232
|
+
}
|
|
233
|
+
await emitCocosChannel(templateHtml, {
|
|
234
|
+
playcraftOrientationValue: orientationValue,
|
|
235
|
+
filename: suffix ? `${options.filename}${suffix}` : options.filename,
|
|
236
|
+
themeName,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
for (const orientationValue of buildOrientations) {
|
|
241
|
+
await buildOneOrientation(orientationValue);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (schemaPathToRestore) restoreSchema(schemaPathToRestore);
|
|
246
|
+
|
|
247
|
+
// 主动退出进程,避免 TerserPlugin/webpack-obfuscator 等可能残留的
|
|
248
|
+
// worker 进程句柄(jest-worker 子进程 IPC 通道等)迟迟不释放,
|
|
249
|
+
// 导致本次 build 子进程(乃至上层 batch-build 的 spawn 等待)
|
|
250
|
+
// 迟迟无法结束。此时构建已全部完成,不再有需要等待的异步操作。
|
|
251
|
+
process.exit(0);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
if (schemaPathToRestore) restoreSchema(schemaPathToRestore);
|
|
254
|
+
console.error(err);
|
|
255
|
+
process.exit(1);
|
|
256
|
+
}
|
|
257
|
+
})();
|
|
258
|
+
}
|
package/cli/commands/builds.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/**
|
|
3
6
|
* Batch Builds Command for playable-scripts
|
|
4
7
|
*
|
|
5
8
|
* 支持:
|
|
@@ -13,4 +16,12 @@
|
|
|
13
16
|
* playable-scripts builds -c "google,facebook" # 指定渠道
|
|
14
17
|
* playable-scripts builds -t "theme1" -c "google" # 指定主题和渠道
|
|
15
18
|
* playable-scripts builds --parallel 3 # 设置并发数
|
|
16
|
-
*/
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
process.on('unhandledRejection', (err) => {
|
|
22
|
+
throw err;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const { buildBatch } = require('../../core/batch-build');
|
|
26
|
+
|
|
27
|
+
buildBatch();
|
package/cli/commands/dev.js
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
process.env.BABEL_ENV = 'development';
|
|
4
|
+
process.env.NODE_ENV = 'development';
|
|
5
|
+
|
|
6
|
+
const { options } = require('../../core/options.js');
|
|
7
|
+
const { isWebEngine } = require('../../core/utils/parseArgvOptions.js');
|
|
8
|
+
|
|
9
|
+
if (!isWebEngine(options.engine)) {
|
|
10
|
+
const { runCocosDev } = require('../../core/engines/cocos/dev-server.js');
|
|
11
|
+
runCocosDev({
|
|
12
|
+
projectRoot: process.cwd(),
|
|
13
|
+
port: process.env.PREVIEW_PORT || options.port || 3000,
|
|
14
|
+
cmd: process.argv[2] || 'start',
|
|
15
|
+
}).catch((err) => {
|
|
16
|
+
console.error(err);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
const { runDev } = require('../../core/webpack.dev.js');
|
|
21
|
+
runDev();
|
|
22
|
+
}
|