catchword 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +314 -2
- package/bin/catchword.js +8 -0
- package/bin/falcon.js +5 -0
- package/dist/cli/adb.d.ts +6 -0
- package/dist/cli/adb.js +52 -0
- package/dist/cli/audit.d.ts +4 -0
- package/dist/cli/audit.js +91 -0
- package/dist/cli/build.d.ts +13 -0
- package/dist/cli/build.js +139 -0
- package/dist/cli/clean.d.ts +1 -0
- package/dist/cli/clean.js +15 -0
- package/dist/cli/create.d.ts +4 -0
- package/dist/cli/create.js +60 -0
- package/dist/cli/dev.d.ts +10 -0
- package/dist/cli/dev.js +23 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +243 -0
- package/dist/cli/native.d.ts +18 -0
- package/dist/cli/native.js +232 -0
- package/dist/cli/simulator.d.ts +8 -0
- package/dist/cli/simulator.js +77 -0
- package/dist/cli/upload.d.ts +9 -0
- package/dist/cli/upload.js +135 -0
- package/dist/cli/util.d.ts +8 -0
- package/dist/cli/util.js +43 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +90 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +51 -0
- package/dist/native/api.d.ts +46 -0
- package/dist/native/api.js +185 -0
- package/dist/native/build.d.ts +47 -0
- package/dist/native/build.js +407 -0
- package/dist/native/elf.d.ts +15 -0
- package/dist/native/elf.js +200 -0
- package/dist/native/types.d.ts +9 -0
- package/dist/native/types.js +91 -0
- package/dist/native/wsl.d.ts +7 -0
- package/dist/native/wsl.js +29 -0
- package/dist/plugin/app-entry.d.ts +6 -0
- package/dist/plugin/app-entry.js +115 -0
- package/dist/plugin/assets.d.ts +4 -0
- package/dist/plugin/assets.js +84 -0
- package/dist/plugin/config.d.ts +14 -0
- package/dist/plugin/config.js +80 -0
- package/dist/plugin/context.d.ts +58 -0
- package/dist/plugin/context.js +255 -0
- package/dist/plugin/fallback.d.ts +4 -0
- package/dist/plugin/fallback.js +81 -0
- package/dist/plugin/image-service.d.ts +17 -0
- package/dist/plugin/image-service.js +138 -0
- package/dist/plugin/jsapi.d.ts +6 -0
- package/dist/plugin/jsapi.js +82 -0
- package/dist/plugin/output.d.ts +4 -0
- package/dist/plugin/output.js +75 -0
- package/dist/plugin/preview.d.ts +34 -0
- package/dist/plugin/preview.js +459 -0
- package/dist/plugin/sanitize.d.ts +5 -0
- package/dist/plugin/sanitize.js +48 -0
- package/dist/plugin/security.d.ts +4 -0
- package/dist/plugin/security.js +18 -0
- package/dist/plugin/sfc.d.ts +6 -0
- package/dist/plugin/sfc.js +181 -0
- package/dist/plugin/shim.d.ts +4 -0
- package/dist/plugin/shim.js +68 -0
- package/dist/plugin/state.d.ts +31 -0
- package/dist/plugin/state.js +61 -0
- package/dist/plugin/string-table.d.ts +7 -0
- package/dist/plugin/string-table.js +324 -0
- package/dist/plugin/style.d.ts +15 -0
- package/dist/plugin/style.js +114 -0
- package/dist/plugin/template.d.ts +7 -0
- package/dist/plugin/template.js +39 -0
- package/dist/steps/manifest.d.ts +9 -0
- package/dist/steps/manifest.js +60 -0
- package/dist/steps/pack.d.ts +3 -0
- package/dist/steps/pack.js +46 -0
- package/dist/steps/qjsc.d.ts +9 -0
- package/dist/steps/qjsc.js +186 -0
- package/dist/steps/report.d.ts +2 -0
- package/dist/steps/report.js +50 -0
- package/dist/steps/security.d.ts +17 -0
- package/dist/steps/security.js +310 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.js +1 -0
- package/dist/util/class-tokenize.d.ts +2 -0
- package/dist/util/class-tokenize.js +56 -0
- package/dist/util/component-name.d.ts +6 -0
- package/dist/util/component-name.js +120 -0
- package/dist/util/fs.d.ts +14 -0
- package/dist/util/fs.js +70 -0
- package/dist/util/imports.d.ts +2 -0
- package/dist/util/imports.js +173 -0
- package/dist/util/log.d.ts +8 -0
- package/dist/util/log.js +24 -0
- package/package.json +61 -4
- package/template/README.md +38 -0
- package/template/catchword.config.ts +18 -0
- package/template/native/CMakeLists.txt +33 -0
- package/template/native/README.md +34 -0
- package/template/native/api.override.json +7 -0
- package/template/native/include/jsapi_demo.h +10 -0
- package/template/native/lib/README.md +10 -0
- package/template/native/sdk/README.md +6 -0
- package/template/native/src/bridge.cpp +9 -0
- package/template/native/src/jsapi_demo.cpp +11 -0
- package/template/package.json +38 -0
- package/template/src/app.js +45 -0
- package/template/src/app.json +11 -0
- package/template/src/base-page.js +150 -0
- package/template/src/components/HelloWorld.vue +20 -0
- package/template/src/mocks/$jsapi.test.js +5 -0
- package/template/src/mocks/test-module.js +5 -0
- package/template/src/pages/index/index.vue +41 -0
- package/template/src/pages/page/page.vue +63 -0
- package/template/src/shims.d.ts +21 -0
- package/template/src/styles/base.less +23 -0
- package/template/src/styles/mixin.less +32 -0
- package/template/src/styles/var.less +15 -0
- package/template/tsconfig.json +14 -0
- package/vendor/README.md +16 -0
- package/vendor/falcon-styler/.jscsrc +31 -0
- package/vendor/falcon-styler/LICENSE +21 -0
- package/vendor/falcon-styler/README.md +124 -0
- package/vendor/falcon-styler/index.js +273 -0
- package/vendor/falcon-styler/lib/shorthand-parser.js +78 -0
- package/vendor/falcon-styler/lib/util.js +30 -0
- package/vendor/falcon-styler/lib/validator.js +846 -0
- package/vendor/falcon-styler/package.json +32 -0
- package/vendor/licenses/falcon-styler.LICENSE +21 -0
- package/vendor/licenses/weex-template-compiler.README.md +3 -0
- package/vendor/package.json +5 -0
- package/vendor/qjsc/SHA256SUMS +11 -0
- package/vendor/qjsc/bin/darwin/arm64/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200412 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200906 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200412 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200906 +0 -0
- package/vendor/qjsc/bin/linux/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/win32/libgcc_s_dw2-1.dll +0 -0
- package/vendor/qjsc/bin/win32/pthreadGC-3.dll +0 -0
- package/vendor/qjsc/bin/win32/qjsc20200705.exe +0 -0
- package/vendor/web/falcon-js-web.js +863 -0
- package/vendor/web/falcon-jsapi-web.js +479 -0
- package/vendor/web/falcon-vue-render.js +11323 -0
- package/vendor/web/falcon-web-boot.js +4 -0
- package/vendor/web/falcon-web-hud.js +119 -0
- package/vendor/web/falcon-web-style.js +235 -0
- package/vendor/web/vue.runtime.common.dev.js +8467 -0
- package/vendor/web/vue.runtime.common.js +5 -0
- package/vendor/weex-template-compiler/README.md +3 -0
- package/vendor/weex-template-compiler/build.js +4824 -0
- package/vendor/weex-template-compiler/index.js +17 -0
- package/vendor/weex-template-compiler/package.json +25 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { log } from '../util/log.js';
|
|
5
|
+
import { readJson, toUnix } from '../util/fs.js';
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const hashSum = require('hash-sum');
|
|
8
|
+
export const BUILD_DIR = '.falcon_';
|
|
9
|
+
export const TMP_DIR = '.falcon_tmp';
|
|
10
|
+
export const SOURCE_DIR = 'src';
|
|
11
|
+
export const APP_JSON = 'app.json';
|
|
12
|
+
export const AMR_EXCLUDE_DOT_FILES = true;
|
|
13
|
+
const APP_ENTRY_CANDIDATES = ['app.js', 'app.ts'];
|
|
14
|
+
export class AppContext {
|
|
15
|
+
options;
|
|
16
|
+
config;
|
|
17
|
+
root;
|
|
18
|
+
srcDir;
|
|
19
|
+
pkg;
|
|
20
|
+
appJson;
|
|
21
|
+
buildDir;
|
|
22
|
+
tmpDir;
|
|
23
|
+
appEntry;
|
|
24
|
+
singleBundle;
|
|
25
|
+
pageFiles;
|
|
26
|
+
hasDynamicClassBindings;
|
|
27
|
+
isDev = false;
|
|
28
|
+
cliBuild = false;
|
|
29
|
+
pendingAssets = new Map();
|
|
30
|
+
_inputs = null;
|
|
31
|
+
_scopeIdCache = new Map();
|
|
32
|
+
constructor(root, options) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
this.config = options.config ?? {};
|
|
35
|
+
if (options.runtime && options.runtime !== 'vue2') {
|
|
36
|
+
throw new Error(`[catchword] 目前仅支持 runtime: 'vue2'。` +
|
|
37
|
+
`Vue 3.5 需要自带运行时与 Weex 渲染器移植,不属于构建工具范围。`);
|
|
38
|
+
}
|
|
39
|
+
this.root = path.resolve(root);
|
|
40
|
+
this.srcDir = path.join(this.root, SOURCE_DIR);
|
|
41
|
+
this.pkg = readJson(path.join(this.root, 'package.json')) ?? {};
|
|
42
|
+
this.appJson =
|
|
43
|
+
readJson(path.join(this.srcDir, APP_JSON)) ?? { pages: {} };
|
|
44
|
+
if (!this.pkg.name && !this.pkg.appid) {
|
|
45
|
+
throw new Error(`[catchword] ${this.root} 不是有效的小程序工程:package.json 缺少 name/appid`);
|
|
46
|
+
}
|
|
47
|
+
if (!this.appJson.pages || typeof this.appJson.pages !== 'object') {
|
|
48
|
+
throw new Error(`[catchword] 缺少或非法的 src/app.json: pages 字段`);
|
|
49
|
+
}
|
|
50
|
+
const entry = APP_ENTRY_CANDIDATES.map((name) => path.join(this.srcDir, name)).find((file) => fs.existsSync(file));
|
|
51
|
+
if (!entry) {
|
|
52
|
+
throw new Error(`[catchword] 找不到入口文件 src/app.js 或 src/app.ts`);
|
|
53
|
+
}
|
|
54
|
+
this.appEntry = entry;
|
|
55
|
+
this.buildDir = path.join(this.root, BUILD_DIR);
|
|
56
|
+
this.tmpDir = path.join(this.root, TMP_DIR);
|
|
57
|
+
const pageCount = Object.keys(this.appJson.pages).length;
|
|
58
|
+
this.singleBundle = this.pkg['single-js-bundle'] === true || pageCount === 1;
|
|
59
|
+
this.pageFiles = new Set(Object.values(this.appJson.pages).map((rel) => path.resolve(this.srcDir, rel)));
|
|
60
|
+
this.hasDynamicClassBindings = this.detectDynamicClassBindings();
|
|
61
|
+
}
|
|
62
|
+
get styleOptions() {
|
|
63
|
+
return this.appJson.options?.style ?? {};
|
|
64
|
+
}
|
|
65
|
+
get runtime() {
|
|
66
|
+
return this.options.runtime ?? 'vue2';
|
|
67
|
+
}
|
|
68
|
+
get appid() {
|
|
69
|
+
return this.pkg.appid ?? '';
|
|
70
|
+
}
|
|
71
|
+
get toolOptions() {
|
|
72
|
+
return this.pkg.catchword ?? this.pkg.falcon ?? {};
|
|
73
|
+
}
|
|
74
|
+
get appName() {
|
|
75
|
+
return this.pkg.appName ?? this.pkg.name ?? 'app';
|
|
76
|
+
}
|
|
77
|
+
get amrFileName() {
|
|
78
|
+
const base = this.pkg.appid || this.pkg.name || 'dist';
|
|
79
|
+
const version = (this.pkg.version || '0.0.0').replace(/\./g, '_');
|
|
80
|
+
return `${base}.${version}.amr`;
|
|
81
|
+
}
|
|
82
|
+
get amrPath() {
|
|
83
|
+
return path.join(this.root, this.amrFileName);
|
|
84
|
+
}
|
|
85
|
+
get quickjsOptions() {
|
|
86
|
+
const fromPkg = this.pkg.quickjs ?? {};
|
|
87
|
+
const fromOptions = this.options.qjscConfig ?? {};
|
|
88
|
+
return {
|
|
89
|
+
version: fromOptions.version ?? fromPkg.version ?? '20200705',
|
|
90
|
+
bigNum: fromOptions.bigNum ?? fromPkg.bigNum ?? false,
|
|
91
|
+
concurrency: fromOptions.concurrency ?? 4
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
get inputs() {
|
|
95
|
+
if (this._inputs)
|
|
96
|
+
return this._inputs;
|
|
97
|
+
const input = { app: 'virtual:falcon-app' };
|
|
98
|
+
if (!this.singleBundle) {
|
|
99
|
+
for (const [page, rel] of Object.entries(this.appJson.pages)) {
|
|
100
|
+
if (input[page])
|
|
101
|
+
throw new Error(`[catchword] 页面重名: ${page}`);
|
|
102
|
+
input[page] = this.resolveSource(rel);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
for (const [name, rel] of Object.entries(this.appJson.services ?? {})) {
|
|
106
|
+
input[`services/${name}`] = this.resolveSource(rel);
|
|
107
|
+
}
|
|
108
|
+
for (const [name, rel] of Object.entries(this.appJson.providers ?? {})) {
|
|
109
|
+
input[`providers/${name}`] = this.resolveSource(rel);
|
|
110
|
+
}
|
|
111
|
+
this._inputs = input;
|
|
112
|
+
return input;
|
|
113
|
+
}
|
|
114
|
+
resolveSource(rel) {
|
|
115
|
+
return path.resolve(this.srcDir, rel);
|
|
116
|
+
}
|
|
117
|
+
detectDynamicClassBindings() {
|
|
118
|
+
const bindRegex = /(?:^|[\s<])(?::class|v-bind:class)\s*=/i;
|
|
119
|
+
const mustacheRegex = /class\s*=\s*["'][^"']*\{\{/i;
|
|
120
|
+
const walk = (dir) => {
|
|
121
|
+
if (!fs.existsSync(dir))
|
|
122
|
+
return false;
|
|
123
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
124
|
+
if (entry.name === 'node_modules' || entry.name.startsWith('.'))
|
|
125
|
+
continue;
|
|
126
|
+
const full = path.join(dir, entry.name);
|
|
127
|
+
if (entry.isDirectory()) {
|
|
128
|
+
if (walk(full))
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
else if (entry.name.endsWith('.vue')) {
|
|
132
|
+
const source = fs.readFileSync(full, 'utf8');
|
|
133
|
+
if (bindRegex.test(source) || mustacheRegex.test(source))
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
};
|
|
139
|
+
return walk(this.srcDir);
|
|
140
|
+
}
|
|
141
|
+
pathToRootUnix(file) {
|
|
142
|
+
return toUnix(path.relative(this.root, file));
|
|
143
|
+
}
|
|
144
|
+
scopeIdFor(file) {
|
|
145
|
+
const rel = path.relative(process.cwd(), file);
|
|
146
|
+
let id = this._scopeIdCache.get(rel);
|
|
147
|
+
if (!id) {
|
|
148
|
+
id = hashSum(rel);
|
|
149
|
+
this._scopeIdCache.set(rel, id);
|
|
150
|
+
}
|
|
151
|
+
return id;
|
|
152
|
+
}
|
|
153
|
+
aliasEntries() {
|
|
154
|
+
const entries = [{ find: '@', replacement: this.srcDir }];
|
|
155
|
+
const custom = this.appJson.options?.alias ?? {};
|
|
156
|
+
for (const [find, rel] of Object.entries(custom)) {
|
|
157
|
+
entries.push({ find, replacement: path.resolve(this.root, rel) });
|
|
158
|
+
}
|
|
159
|
+
return entries;
|
|
160
|
+
}
|
|
161
|
+
getLessPaths() {
|
|
162
|
+
const style = this.styleOptions;
|
|
163
|
+
const paths = (style.lessPaths && style.lessPaths.length ? style.lessPaths : ['styles']).map((p) => path.resolve(this.srcDir, p));
|
|
164
|
+
paths.push(path.join(this.root, 'node_modules'));
|
|
165
|
+
return paths;
|
|
166
|
+
}
|
|
167
|
+
getLessModifyVars() {
|
|
168
|
+
const style = this.styleOptions;
|
|
169
|
+
return {
|
|
170
|
+
theme: style.theme || 'theme-default',
|
|
171
|
+
themeCustom: style.themeCustom || 'theme-custom'
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
getThemes() {
|
|
175
|
+
return ['_'].concat(this.styleOptions.themes ?? []);
|
|
176
|
+
}
|
|
177
|
+
getThemeSource() {
|
|
178
|
+
const style = this.styleOptions;
|
|
179
|
+
const theme = style.theme || 'theme-default';
|
|
180
|
+
const themeCustom = style.themeCustom || '';
|
|
181
|
+
const themePath = path.join(this.root, 'node_modules/falcon-ui', `src/styles/${theme}/theme.config.js`);
|
|
182
|
+
const customPath = themeCustom
|
|
183
|
+
? path.join(this.srcDir, 'styles', themeCustom, 'theme.config.js')
|
|
184
|
+
: null;
|
|
185
|
+
let FALCON_THEME = 'export default {}';
|
|
186
|
+
let FALCON_THEME_CUSTOM = 'export default {}';
|
|
187
|
+
const falconUiDir = path.join(this.root, 'node_modules/falcon-ui');
|
|
188
|
+
if (fs.existsSync(themePath)) {
|
|
189
|
+
FALCON_THEME = `export { default } from ${JSON.stringify(toUnix(themePath))};`;
|
|
190
|
+
}
|
|
191
|
+
else if (fs.existsSync(falconUiDir)) {
|
|
192
|
+
log.warn(`[catchword] 找不到 theme "${theme}":${themePath}`);
|
|
193
|
+
}
|
|
194
|
+
if (customPath && fs.existsSync(customPath)) {
|
|
195
|
+
FALCON_THEME_CUSTOM = `export { default } from ${JSON.stringify(toUnix(customPath))};`;
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
FALCON_THEME,
|
|
199
|
+
FALCON_THEME_CUSTOM,
|
|
200
|
+
themePath: fs.existsSync(themePath) ? themePath : null,
|
|
201
|
+
themeCustomPath: customPath && fs.existsSync(customPath) ? customPath : null
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
get mockDir() {
|
|
205
|
+
return path.resolve(this.root, this.config.preview?.mockDir ?? 'api-mock');
|
|
206
|
+
}
|
|
207
|
+
get mockDirLabel() {
|
|
208
|
+
return path.relative(this.root, this.mockDir).split(path.sep).join('/') || '.';
|
|
209
|
+
}
|
|
210
|
+
getMockApis() {
|
|
211
|
+
const result = {};
|
|
212
|
+
if (this.options.mock !== true && !this.isDev)
|
|
213
|
+
return result;
|
|
214
|
+
const dir = this.mockDir;
|
|
215
|
+
if (!fs.existsSync(dir))
|
|
216
|
+
return result;
|
|
217
|
+
for (const file of fs.readdirSync(dir)) {
|
|
218
|
+
if (!file.endsWith('.js'))
|
|
219
|
+
continue;
|
|
220
|
+
let name = path.basename(file, '.js');
|
|
221
|
+
if (name.startsWith('$jsapi.'))
|
|
222
|
+
name = name.replace('$jsapi.', '$jsapi/');
|
|
223
|
+
result[name] = fs.readFileSync(path.join(dir, file), 'utf8');
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
getDefines(isDev) {
|
|
228
|
+
const defines = {
|
|
229
|
+
__DEV__: isDev ? 'true' : 'false',
|
|
230
|
+
__FALCON_DEV__: isDev ? 'true' : 'false',
|
|
231
|
+
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production')
|
|
232
|
+
};
|
|
233
|
+
for (const [name, value] of Object.entries(this.config.build?.define ?? {})) {
|
|
234
|
+
defines[name] = JSON.stringify(value);
|
|
235
|
+
}
|
|
236
|
+
for (const item of this.options.env ?? []) {
|
|
237
|
+
const idx = item.indexOf('=');
|
|
238
|
+
if (idx === -1) {
|
|
239
|
+
defines[item.trim()] = 'true';
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
const key = item.slice(0, idx).trim();
|
|
243
|
+
const value = item.slice(idx + 1).trim();
|
|
244
|
+
if (key)
|
|
245
|
+
defines[key] = JSON.stringify(value);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return defines;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
export function createContext(options) {
|
|
252
|
+
const root = path.resolve(options.dir ?? process.cwd());
|
|
253
|
+
const ctx = new AppContext(root, options);
|
|
254
|
+
return ctx;
|
|
255
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { log } from '../util/log.js';
|
|
3
|
+
const MISSING_PREFIX = '\0falcon-missing:';
|
|
4
|
+
function missingModuleStub(name) {
|
|
5
|
+
const key = JSON.stringify(name);
|
|
6
|
+
const message = `[catchword-preview] 原生模块 ${key} 在浏览器预览中不可用(真机由 native 提供)`;
|
|
7
|
+
return `function unavailable() {
|
|
8
|
+
throw new Error(${JSON.stringify(message)})
|
|
9
|
+
}
|
|
10
|
+
const target = Object.create(null)
|
|
11
|
+
const stub = new Proxy(unavailable, {
|
|
12
|
+
get: function (_target, prop) {
|
|
13
|
+
if (prop === 'then' || prop === 'toJSON' || prop === 'constructor') return undefined
|
|
14
|
+
if (typeof prop === 'symbol') return undefined
|
|
15
|
+
unavailable()
|
|
16
|
+
},
|
|
17
|
+
apply: unavailable,
|
|
18
|
+
construct: unavailable
|
|
19
|
+
})
|
|
20
|
+
export default stub
|
|
21
|
+
export const available = false
|
|
22
|
+
void target
|
|
23
|
+
`;
|
|
24
|
+
}
|
|
25
|
+
export function fallbackPlugin(ctx, state) {
|
|
26
|
+
return {
|
|
27
|
+
name: 'falcon:resolve-fallback',
|
|
28
|
+
enforce: 'post',
|
|
29
|
+
async resolveId(id, importer) {
|
|
30
|
+
if (!id)
|
|
31
|
+
return null;
|
|
32
|
+
if (id.startsWith('\0') || id.startsWith('virtual:'))
|
|
33
|
+
return null;
|
|
34
|
+
if (id.startsWith('$jsapi/'))
|
|
35
|
+
return null;
|
|
36
|
+
if (id.startsWith('.'))
|
|
37
|
+
return null;
|
|
38
|
+
if (path.isAbsolute(id) || id.startsWith('/'))
|
|
39
|
+
return null;
|
|
40
|
+
if (!ctx.isDev && (id === 'vue' || id === 'falcon-vue-render')) {
|
|
41
|
+
return { id, external: true };
|
|
42
|
+
}
|
|
43
|
+
if (id.includes(':'))
|
|
44
|
+
return null;
|
|
45
|
+
let resolved = null;
|
|
46
|
+
try {
|
|
47
|
+
resolved = (await this.resolve(id, importer, { skipSelf: true }));
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
resolved = null;
|
|
51
|
+
}
|
|
52
|
+
if (resolved)
|
|
53
|
+
return null;
|
|
54
|
+
state.internalModules.add(id);
|
|
55
|
+
if (ctx.isDev)
|
|
56
|
+
return `${MISSING_PREFIX}${id}`;
|
|
57
|
+
return { id, external: true };
|
|
58
|
+
},
|
|
59
|
+
load(id) {
|
|
60
|
+
if (!id.startsWith(MISSING_PREFIX))
|
|
61
|
+
return null;
|
|
62
|
+
const name = id.slice(MISSING_PREFIX.length);
|
|
63
|
+
log.warn(`[catchword] 原生模块 ${name} 在预览中替换为占位(真机可用)`);
|
|
64
|
+
return missingModuleStub(name);
|
|
65
|
+
},
|
|
66
|
+
buildEnd(error) {
|
|
67
|
+
if (error)
|
|
68
|
+
return;
|
|
69
|
+
const deps = ctx.pkg.dependencies ?? {};
|
|
70
|
+
const internal = [...state.internalModules];
|
|
71
|
+
const required = internal.filter((name) => name in deps);
|
|
72
|
+
const optional = internal.filter((name) => !(name in deps));
|
|
73
|
+
if (required.length > 0) {
|
|
74
|
+
this.error(`[catchword] 以下模块必须安装:\n${required.join(',')}\n可运行 npm install 安装`);
|
|
75
|
+
}
|
|
76
|
+
if (optional.length > 0) {
|
|
77
|
+
log.warn(`[catchword] 未找到以下模块,已按运行时内置模块处理(原生模块可忽略):\n${optional.join(',')}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AppContext } from './context.js';
|
|
2
|
+
export interface ImageInfo {
|
|
3
|
+
id: string;
|
|
4
|
+
file: string;
|
|
5
|
+
pathToRoot: string;
|
|
6
|
+
format: 'file' | 'base64';
|
|
7
|
+
ext: string;
|
|
8
|
+
mime: string;
|
|
9
|
+
isBase64: boolean;
|
|
10
|
+
isDownload: boolean;
|
|
11
|
+
isHttp: boolean;
|
|
12
|
+
needDownload: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function isHttpUrl(url: string): boolean;
|
|
15
|
+
export declare function imageExtension(id: string): string;
|
|
16
|
+
export declare function getImageInfo(ctx: AppContext, id: string): ImageInfo | null;
|
|
17
|
+
export declare function processImage(ctx: AppContext, id: string): Promise<string | null>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { ensureDir, md5, md5File } from '../util/fs.js';
|
|
4
|
+
import { log } from '../util/log.js';
|
|
5
|
+
const MIME_TYPES = {
|
|
6
|
+
'.jpg': 'image/jpeg',
|
|
7
|
+
'.jpeg': 'image/jpeg',
|
|
8
|
+
'.png': 'image/png',
|
|
9
|
+
'.gif': 'image/gif',
|
|
10
|
+
'.bmp': 'image/bmp',
|
|
11
|
+
'.apng': 'image/apng',
|
|
12
|
+
'.svg': 'image/svg',
|
|
13
|
+
'.webp': 'image/webp'
|
|
14
|
+
};
|
|
15
|
+
const REG_BASE64 = /[?&]base64/;
|
|
16
|
+
const REG_DOWNLOAD = /[?&]download/;
|
|
17
|
+
const REG_HTTP = /^https?:\/\//;
|
|
18
|
+
const DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
19
|
+
const MAX_DOWNLOAD_BYTES = 32 * 1024 * 1024;
|
|
20
|
+
export function isHttpUrl(url) {
|
|
21
|
+
return REG_HTTP.test(url);
|
|
22
|
+
}
|
|
23
|
+
export function imageExtension(id) {
|
|
24
|
+
const clean = id.split('?')[0];
|
|
25
|
+
return path.extname(clean).toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
export function getImageInfo(ctx, id) {
|
|
28
|
+
const ext = imageExtension(id);
|
|
29
|
+
const mime = MIME_TYPES[ext];
|
|
30
|
+
if (!mime)
|
|
31
|
+
return null;
|
|
32
|
+
const isBase64 = REG_BASE64.test(id);
|
|
33
|
+
const isDownload = REG_DOWNLOAD.test(id);
|
|
34
|
+
const isHttp = isHttpUrl(id);
|
|
35
|
+
const needDownload = isHttp && (isBase64 || isDownload);
|
|
36
|
+
const file = id.replace(REG_BASE64, '').replace(REG_DOWNLOAD, '');
|
|
37
|
+
return {
|
|
38
|
+
id,
|
|
39
|
+
file,
|
|
40
|
+
pathToRoot: isHttp ? file : ctx.pathToRootUnix(path.resolve(file)),
|
|
41
|
+
format: isBase64 ? 'base64' : 'file',
|
|
42
|
+
ext,
|
|
43
|
+
mime,
|
|
44
|
+
isBase64,
|
|
45
|
+
isDownload,
|
|
46
|
+
isHttp,
|
|
47
|
+
needDownload
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function readResponseBody(res) {
|
|
51
|
+
const contentLength = Number(res.headers.get('content-length') ?? 0);
|
|
52
|
+
if (contentLength > MAX_DOWNLOAD_BYTES) {
|
|
53
|
+
if (res.body) {
|
|
54
|
+
try {
|
|
55
|
+
await res.body.cancel();
|
|
56
|
+
}
|
|
57
|
+
catch { }
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`响应超过 ${MAX_DOWNLOAD_BYTES / 1024 / 1024}MB 上限`);
|
|
60
|
+
}
|
|
61
|
+
if (!res.body)
|
|
62
|
+
return Buffer.from(await res.arrayBuffer());
|
|
63
|
+
const reader = res.body.getReader();
|
|
64
|
+
const chunks = [];
|
|
65
|
+
let total = 0;
|
|
66
|
+
try {
|
|
67
|
+
for (;;) {
|
|
68
|
+
const { done, value } = await reader.read();
|
|
69
|
+
if (done)
|
|
70
|
+
break;
|
|
71
|
+
if (!value)
|
|
72
|
+
continue;
|
|
73
|
+
total += value.length;
|
|
74
|
+
if (total > MAX_DOWNLOAD_BYTES) {
|
|
75
|
+
await reader.cancel().catch(() => { });
|
|
76
|
+
throw new Error(`响应超过 ${MAX_DOWNLOAD_BYTES / 1024 / 1024}MB 上限`);
|
|
77
|
+
}
|
|
78
|
+
chunks.push(Buffer.from(value));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
try {
|
|
83
|
+
reader.releaseLock();
|
|
84
|
+
}
|
|
85
|
+
catch { }
|
|
86
|
+
}
|
|
87
|
+
return Buffer.concat(chunks);
|
|
88
|
+
}
|
|
89
|
+
function download(ctx, url) {
|
|
90
|
+
const tmp = path.join(ctx.tmpDir, md5(url) + imageExtension(url.split('?')[0]));
|
|
91
|
+
if (fs.existsSync(tmp))
|
|
92
|
+
return Promise.resolve(tmp);
|
|
93
|
+
ensureDir(ctx.tmpDir);
|
|
94
|
+
return fetch(url, { signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS) })
|
|
95
|
+
.then(async (res) => {
|
|
96
|
+
if (!res.ok)
|
|
97
|
+
throw new Error(`HTTP ${res.status}`);
|
|
98
|
+
const buf = await readResponseBody(res);
|
|
99
|
+
ensureDir(path.dirname(tmp));
|
|
100
|
+
fs.writeFileSync(tmp, buf);
|
|
101
|
+
return tmp;
|
|
102
|
+
})
|
|
103
|
+
.catch((err) => {
|
|
104
|
+
log.warn(`[catchword] 图片下载失败: ${url} (${err.message})`);
|
|
105
|
+
return null;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
export async function processImage(ctx, id) {
|
|
109
|
+
const info = getImageInfo(ctx, id);
|
|
110
|
+
if (!info)
|
|
111
|
+
return null;
|
|
112
|
+
if (info.isHttp) {
|
|
113
|
+
if (!info.needDownload)
|
|
114
|
+
return id;
|
|
115
|
+
const downloaded = await download(ctx, info.file);
|
|
116
|
+
if (!downloaded)
|
|
117
|
+
return id;
|
|
118
|
+
info.file = downloaded;
|
|
119
|
+
info.pathToRoot = ctx.pathToRootUnix(path.resolve(downloaded));
|
|
120
|
+
}
|
|
121
|
+
if (info.format === 'base64') {
|
|
122
|
+
const source = fs.readFileSync(info.file, 'base64').replace(/[\r\n]+/gm, '');
|
|
123
|
+
if (!source) {
|
|
124
|
+
log.warn(`[catchword] 图片转 base64 失败,使用原始地址: ${info.id}`);
|
|
125
|
+
return info.id;
|
|
126
|
+
}
|
|
127
|
+
return `data:${info.mime};base64,${source}`;
|
|
128
|
+
}
|
|
129
|
+
const imagesDir = path.join(ctx.buildDir, 'images');
|
|
130
|
+
ensureDir(imagesDir);
|
|
131
|
+
const fileName = md5File(info.file) + info.ext;
|
|
132
|
+
const target = path.join(imagesDir, fileName);
|
|
133
|
+
if (!fs.existsSync(target)) {
|
|
134
|
+
fs.copyFileSync(info.file, target);
|
|
135
|
+
}
|
|
136
|
+
ctx.pendingAssets.set(target, info.file);
|
|
137
|
+
return `images/${fileName}`;
|
|
138
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import type { AppContext } from './context.js';
|
|
3
|
+
import type { BuildState } from './state.js';
|
|
4
|
+
export declare function accessorModule(name: string, mockDir?: string): string;
|
|
5
|
+
export declare function jsapiPlugin(ctx: AppContext, _state: BuildState): Plugin;
|
|
6
|
+
export declare function themePlugin(ctx: AppContext): Plugin;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const JSAPI_RE = /^\$jsapi\/(.*)$/;
|
|
2
|
+
const JSAPI_RESOLVED_PREFIX = '\0falcon-jsapi:';
|
|
3
|
+
const BRIDGE_RESOLVED = '\0falcon-bridge';
|
|
4
|
+
const BRIDGE_ID = 'bridge';
|
|
5
|
+
export function accessorModule(name, mockDir = 'api-mock') {
|
|
6
|
+
const key = JSON.stringify(name);
|
|
7
|
+
return `const falcon = typeof $falcon !== 'undefined' && $falcon ? $falcon : null
|
|
8
|
+
const jsapi = (falcon && falcon.jsapi) || globalThis.__FALCON_JSAPI__ || null
|
|
9
|
+
const mod = jsapi ? jsapi[${key}] : undefined
|
|
10
|
+
if (mod === undefined) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
'[catchword] $jsapi/${name} 在当前运行时不可用。' +
|
|
13
|
+
'浏览器预览请在 ${mockDir}/$jsapi.${name}.js 提供实现(dev 下自动加载)。'
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
export default mod
|
|
17
|
+
`;
|
|
18
|
+
}
|
|
19
|
+
export function jsapiPlugin(ctx, _state) {
|
|
20
|
+
return {
|
|
21
|
+
name: 'falcon:jsapi',
|
|
22
|
+
enforce: 'pre',
|
|
23
|
+
resolveId(id) {
|
|
24
|
+
if (id === BRIDGE_ID && ctx.isDev) {
|
|
25
|
+
const deps = { ...(ctx.pkg.dependencies ?? {}), ...(ctx.pkg.devDependencies ?? {}) };
|
|
26
|
+
if (!(BRIDGE_ID in deps))
|
|
27
|
+
return BRIDGE_RESOLVED;
|
|
28
|
+
}
|
|
29
|
+
const match = JSAPI_RE.exec(id);
|
|
30
|
+
if (!match)
|
|
31
|
+
return null;
|
|
32
|
+
const name = match[1];
|
|
33
|
+
if (!/^[0-9a-zA-Z_]+$/.test(name)) {
|
|
34
|
+
this.error(`[catchword] 非法的模块名: ${name}`);
|
|
35
|
+
}
|
|
36
|
+
return `${JSAPI_RESOLVED_PREFIX}${name}`;
|
|
37
|
+
},
|
|
38
|
+
load(id) {
|
|
39
|
+
if (id === BRIDGE_RESOLVED) {
|
|
40
|
+
return `const falcon = typeof $falcon !== 'undefined' && $falcon ? $falcon : null
|
|
41
|
+
const jsapi = (falcon && falcon.jsapi) || globalThis.__FALCON_JSAPI__ || null
|
|
42
|
+
if (!jsapi) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
'[catchword] bridge 模块在当前运行时不可用:浏览器预览需要 __FALCON_JSAPI__'
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
export default jsapi
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
if (!id.startsWith(JSAPI_RESOLVED_PREFIX))
|
|
51
|
+
return null;
|
|
52
|
+
const name = id.slice(JSAPI_RESOLVED_PREFIX.length);
|
|
53
|
+
const mocks = ctx.getMockApis();
|
|
54
|
+
const mock = mocks[`$jsapi/${name}`];
|
|
55
|
+
if (mock !== undefined)
|
|
56
|
+
return mock;
|
|
57
|
+
return accessorModule(name, ctx.mockDirLabel);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function themePlugin(ctx) {
|
|
62
|
+
const sources = ctx.getThemeSource();
|
|
63
|
+
const map = {
|
|
64
|
+
FALCON_THEME: sources.FALCON_THEME,
|
|
65
|
+
FALCON_THEME_CUSTOM: sources.FALCON_THEME_CUSTOM
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
name: 'falcon:theme',
|
|
69
|
+
enforce: 'pre',
|
|
70
|
+
resolveId(id) {
|
|
71
|
+
if (id === 'FALCON_THEME' || id === 'FALCON_THEME_CUSTOM') {
|
|
72
|
+
return `\0falcon-theme:${id}`;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
},
|
|
76
|
+
load(id) {
|
|
77
|
+
if (!id.startsWith('\0falcon-theme:'))
|
|
78
|
+
return null;
|
|
79
|
+
return map[id.slice('\0falcon-theme:'.length)];
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { log } from '../util/log.js';
|
|
4
|
+
function shouldCopy(source) {
|
|
5
|
+
const base = path.basename(source);
|
|
6
|
+
if (base.startsWith('.'))
|
|
7
|
+
return false;
|
|
8
|
+
if (base.endsWith('.map'))
|
|
9
|
+
return false;
|
|
10
|
+
if (base === 'Thumbs.db')
|
|
11
|
+
return false;
|
|
12
|
+
try {
|
|
13
|
+
if (fs.lstatSync(source).isSymbolicLink())
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
export function outputPlugin(ctx, state) {
|
|
22
|
+
return {
|
|
23
|
+
name: 'falcon:output',
|
|
24
|
+
apply: 'build',
|
|
25
|
+
closeBundle() {
|
|
26
|
+
for (const [target, source] of ctx.pendingAssets) {
|
|
27
|
+
if (!fs.existsSync(source))
|
|
28
|
+
continue;
|
|
29
|
+
if (!fs.existsSync(target)) {
|
|
30
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
31
|
+
fs.copyFileSync(source, target);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const appFile = path.join(ctx.buildDir, 'app.js');
|
|
35
|
+
if (state.keyframes.length > 0) {
|
|
36
|
+
if (fs.existsSync(appFile)) {
|
|
37
|
+
fs.appendFileSync(appFile, `\n$falcon.__KEYFRAMES = ${JSON.stringify(state.keyframes)};\n`);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
log.warn(`[catchword] 未找到 ${appFile},keyframes 未写入`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const dirName of ['libs', 'jsworkers', 'assets']) {
|
|
44
|
+
const src = path.join(ctx.root, dirName);
|
|
45
|
+
if (!fs.existsSync(src))
|
|
46
|
+
continue;
|
|
47
|
+
const dest = path.join(ctx.buildDir, dirName);
|
|
48
|
+
const skipped = [];
|
|
49
|
+
const shouldCopyEntry = (source) => {
|
|
50
|
+
if (shouldCopy(source))
|
|
51
|
+
return true;
|
|
52
|
+
skipped.push(path.relative(ctx.root, source).replace(/\\/g, '/'));
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
fs.cpSync(src, dest, {
|
|
57
|
+
recursive: true,
|
|
58
|
+
force: true,
|
|
59
|
+
filter: shouldCopyEntry
|
|
60
|
+
});
|
|
61
|
+
if (skipped.length > 0) {
|
|
62
|
+
const list = skipped.slice(0, 10).join(', ');
|
|
63
|
+
const more = skipped.length > 10 ? ` 等 ${skipped.length} 项` : '';
|
|
64
|
+
log.warn(`[catchword] ${dirName} 中已跳过隐藏/软链接/源码映射: ${list}${more}`);
|
|
65
|
+
}
|
|
66
|
+
log.success(`${dirName} 同步成功: ${dest}`);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
log.error(`${dirName} 同步失败: ${src} -> ${dest}`);
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|