@aiot-toolkit/aiotpack 2.1.0-prender.3 → 2.1.0-prender.5
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.
|
@@ -214,7 +214,7 @@ function elementToJson(el, styleSheet, importMap, scopeVars, pageDir) {
|
|
|
214
214
|
if (name === 'is' && tagName === 'component') {
|
|
215
215
|
continue; // is attribute handled at runtime
|
|
216
216
|
} else if (name === 'class') {
|
|
217
|
-
className = value.trim();
|
|
217
|
+
className = value.replace(/\s+/g, ' ').trim();
|
|
218
218
|
} else if (name.startsWith('@') || name.startsWith('on') && name.length > 2) {
|
|
219
219
|
const rawEvtName = name.startsWith('@') ? name.slice(1) : name.replace(/^on/, '');
|
|
220
220
|
const evtName = rawEvtName.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
@@ -7,10 +7,112 @@ exports.default = void 0;
|
|
|
7
7
|
var _TemplateCompiler = require("./TemplateCompiler");
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
|
9
9
|
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
10
|
+
var acorn = _interopRequireWildcard(require("acorn"));
|
|
10
11
|
var _UxCompileUtil = _interopRequireDefault(require("./vela/utils/UxCompileUtil"));
|
|
11
12
|
var _UxFileUtils = _interopRequireDefault(require("../../utils/ux/UxFileUtils"));
|
|
12
13
|
var _UxLoaderUtils = _interopRequireDefault(require("../../utils/ux/UxLoaderUtils"));
|
|
14
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
15
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
13
16
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
|
+
const NS_PREFIX = /^@(system|service|android|hap)\./;
|
|
18
|
+
const APP_MODULE_PREFIX = '@app-module/';
|
|
19
|
+
function rewriteSystemImports(code) {
|
|
20
|
+
if (!/@(system|service|android|hap)\.|@app-module\//.test(code)) return code;
|
|
21
|
+
let ast;
|
|
22
|
+
try {
|
|
23
|
+
ast = acorn.parse(code, {
|
|
24
|
+
sourceType: 'module',
|
|
25
|
+
ecmaVersion: 'latest',
|
|
26
|
+
allowHashBang: true,
|
|
27
|
+
allowAwaitOutsideFunction: true,
|
|
28
|
+
allowReturnOutsideFunction: true,
|
|
29
|
+
allowImportExportEverywhere: true
|
|
30
|
+
});
|
|
31
|
+
} catch {
|
|
32
|
+
return code;
|
|
33
|
+
}
|
|
34
|
+
const edits = [];
|
|
35
|
+
let tempCounter = 0;
|
|
36
|
+
for (const node of ast.body) {
|
|
37
|
+
if (node.type !== 'ImportDeclaration') continue;
|
|
38
|
+
const src = node.source.value;
|
|
39
|
+
let moduleName = null;
|
|
40
|
+
if (NS_PREFIX.test(src)) {
|
|
41
|
+
moduleName = src.replace(NS_PREFIX, '$1.');
|
|
42
|
+
} else if (src.startsWith(APP_MODULE_PREFIX)) {
|
|
43
|
+
moduleName = src.slice(APP_MODULE_PREFIX.length);
|
|
44
|
+
}
|
|
45
|
+
if (moduleName === null) continue;
|
|
46
|
+
const target = `"${APP_MODULE_PREFIX}${moduleName}"`;
|
|
47
|
+
if (node.specifiers.length === 0) {
|
|
48
|
+
edits.push({
|
|
49
|
+
start: node.start,
|
|
50
|
+
end: node.end,
|
|
51
|
+
replacement: `$app_require$(${target});`
|
|
52
|
+
});
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (node.specifiers.length === 1) {
|
|
56
|
+
const spec = node.specifiers[0];
|
|
57
|
+
const local = spec.local.name;
|
|
58
|
+
if (spec.type === 'ImportDefaultSpecifier' || spec.type === 'ImportNamespaceSpecifier') {
|
|
59
|
+
edits.push({
|
|
60
|
+
start: node.start,
|
|
61
|
+
end: node.end,
|
|
62
|
+
replacement: `const ${local} = $app_require$(${target});`
|
|
63
|
+
});
|
|
64
|
+
} else if (spec.type === 'ImportSpecifier') {
|
|
65
|
+
const imported = spec.imported.name;
|
|
66
|
+
const dest = imported === local ? `{ ${local} }` : `{ ${imported}: ${local} }`;
|
|
67
|
+
edits.push({
|
|
68
|
+
start: node.start,
|
|
69
|
+
end: node.end,
|
|
70
|
+
replacement: `const ${dest} = $app_require$(${target});`
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const tempVar = `$_imp${tempCounter++}`;
|
|
76
|
+
const parts = [`const ${tempVar} = $app_require$(${target});`];
|
|
77
|
+
for (const spec of node.specifiers) {
|
|
78
|
+
const local = spec.local.name;
|
|
79
|
+
if (spec.type === 'ImportDefaultSpecifier' || spec.type === 'ImportNamespaceSpecifier') {
|
|
80
|
+
parts.push(`const ${local} = ${tempVar};`);
|
|
81
|
+
} else if (spec.type === 'ImportSpecifier') {
|
|
82
|
+
const imported = spec.imported.name;
|
|
83
|
+
const dest = imported === local ? `{ ${local} }` : `{ ${imported}: ${local} }`;
|
|
84
|
+
parts.push(`const ${dest} = ${tempVar};`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
edits.push({
|
|
88
|
+
start: node.start,
|
|
89
|
+
end: node.end,
|
|
90
|
+
replacement: parts.join(' ')
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (edits.length === 0) return code;
|
|
94
|
+
edits.sort((a, b) => b.start - a.start);
|
|
95
|
+
let out = code;
|
|
96
|
+
for (const e of edits) out = out.slice(0, e.start) + e.replacement + out.slice(e.end);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
function loadUserViteConfig(projectPath, mode) {
|
|
100
|
+
const configPath = _path.default.join(projectPath, 'quickapp.config.js');
|
|
101
|
+
if (!_fsExtra.default.existsSync(configPath)) return {};
|
|
102
|
+
delete require.cache[require.resolve(configPath)];
|
|
103
|
+
const userConfig = require(configPath);
|
|
104
|
+
if (userConfig?.postHook && !userConfig?.vite) {
|
|
105
|
+
console.warn(`[aiot-toolkit] quickapp.config.js uses legacy webpack-style \`postHook\` API; ` + `the toolkit now reads a vite-native \`vite\` section instead. ` + `Migrate to: module.exports = { vite: { define: { ... }, plugins: [ ... ] } }`);
|
|
106
|
+
}
|
|
107
|
+
const viteSection = userConfig?.vite;
|
|
108
|
+
if (!viteSection) return {};
|
|
109
|
+
if (typeof viteSection === 'function') {
|
|
110
|
+
return viteSection({
|
|
111
|
+
mode
|
|
112
|
+
}) ?? {};
|
|
113
|
+
}
|
|
114
|
+
return viteSection;
|
|
115
|
+
}
|
|
14
116
|
function extractAppStyleLiteral(code) {
|
|
15
117
|
const m = code.match(/var\s+\$app_style\$\s*=\s*\[/);
|
|
16
118
|
if (!m) return null;
|
|
@@ -56,6 +158,15 @@ class ViteCompiler {
|
|
|
56
158
|
const manifestPath = _UxFileUtils.default.getManifestFilePath(originalProjectPath, sourceRoot);
|
|
57
159
|
const manifest = _fsExtra.default.readJSONSync(manifestPath);
|
|
58
160
|
const entries = _UxCompileUtil.default.resolveEntries(manifest, srcPath, originalProjectPath);
|
|
161
|
+
let userVite = {};
|
|
162
|
+
try {
|
|
163
|
+
userVite = loadUserViteConfig(originalProjectPath, param.mode === 'production' ? 'production' : 'development');
|
|
164
|
+
} catch (e) {
|
|
165
|
+
this.onLog?.([{
|
|
166
|
+
level: 1,
|
|
167
|
+
message: [`Failed to load quickapp.config.js: ${e.message}`]
|
|
168
|
+
}]);
|
|
169
|
+
}
|
|
59
170
|
for (const [entryName, entryValue] of Object.entries(entries)) {
|
|
60
171
|
const entryPath = entryValue.split('?')[0];
|
|
61
172
|
const resolvedEntry = _path.default.resolve(originalProjectPath, entryPath);
|
|
@@ -73,8 +184,10 @@ class ViteCompiler {
|
|
|
73
184
|
const result = await build({
|
|
74
185
|
root: originalProjectPath,
|
|
75
186
|
logLevel: 'silent',
|
|
187
|
+
define: userVite.define ?? {},
|
|
76
188
|
resolve: {
|
|
77
|
-
extensions: ['.js', '.ts', '.ux', '.json']
|
|
189
|
+
extensions: ['.js', '.ts', '.ux', '.json'],
|
|
190
|
+
...(userVite.resolve ?? {})
|
|
78
191
|
},
|
|
79
192
|
build: {
|
|
80
193
|
write: false,
|
|
@@ -115,17 +228,9 @@ class ViteCompiler {
|
|
|
115
228
|
name: 'system-to-app-require',
|
|
116
229
|
enforce: 'pre',
|
|
117
230
|
transform(code, id) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
t = t.replace(new RegExp(`import\\s+(\\w+)\\s+from\\s+['"]@${NS}\\.([^'"]+)['"]\\s*;?`, 'g'), 'const $1 = $app_require$("@app-module/$2.$3");');
|
|
122
|
-
t = t.replace(new RegExp(`import\\s+\\{([^}]+)\\}\\s+from\\s+['"]@${NS}\\.([^'"]+)['"]\\s*;?`, 'g'), 'const {$1} = $app_require$("@app-module/$2.$3");');
|
|
123
|
-
t = t.replace(/import\s+(\w+)\s+from\s+['"]@app-module\/([^'"]+)['"]\s*;?/g, 'const $1 = $app_require$("@app-module/$2");');
|
|
124
|
-
t = t.replace(/import\s+\{([^}]+)\}\s+from\s+['"]@app-module\/([^'"]+)['"]\s*;?/g, 'const {$1} = $app_require$("@app-module/$2");');
|
|
125
|
-
t = t.replace(/import\s+['"]@app-module\/([^'"]+)['"]\s*;?/g, '$app_require$("@app-module/$1");');
|
|
126
|
-
t = t.replace(new RegExp(`import\\s+['"]@${NS}\\.([^'"]+)['"]\\s*;?`, 'g'), '$app_require$("@app-module/$1.$2");');
|
|
127
|
-
return t !== code ? {
|
|
128
|
-
code: t,
|
|
231
|
+
const out = rewriteSystemImports(code);
|
|
232
|
+
return out !== code ? {
|
|
233
|
+
code: out,
|
|
129
234
|
map: null
|
|
130
235
|
} : null;
|
|
131
236
|
}
|
|
@@ -285,7 +390,7 @@ class ViteCompiler {
|
|
|
285
390
|
}, isApp, param, compilation);
|
|
286
391
|
return files[0]?.content || '';
|
|
287
392
|
}
|
|
288
|
-
}]
|
|
393
|
+
}, ...(userVite.plugins ?? [])]
|
|
289
394
|
});
|
|
290
395
|
// Extract the output code
|
|
291
396
|
if (result && !Array.isArray(result)) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Jsc
|
|
3
|
-
*/
|
|
4
1
|
declare class Jsc {
|
|
5
2
|
readonly projectPath: string;
|
|
6
3
|
readonly buildPath: string;
|
|
7
4
|
constructor(projectPath: string, buildPath: string);
|
|
8
5
|
jsc(deleteJs?: boolean): Promise<boolean>;
|
|
6
|
+
private logBeforeInvoke;
|
|
7
|
+
private logAfterInvoke;
|
|
8
|
+
private static listFiles;
|
|
9
9
|
}
|
|
10
10
|
export default Jsc;
|
|
@@ -4,10 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
+
var fs = _interopRequireWildcard(require("fs"));
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
7
9
|
var _sharedUtils = require("@aiot-toolkit/shared-utils");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
10
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
11
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
11
12
|
class Jsc {
|
|
12
13
|
constructor(projectPath, buildPath) {
|
|
13
14
|
this.projectPath = projectPath;
|
|
@@ -20,21 +21,93 @@ class Jsc {
|
|
|
20
21
|
buildPath
|
|
21
22
|
} = this;
|
|
22
23
|
return _sharedUtils.CommonUtil.requireNodeModule(projectPath, `@aiot-toolkit/jsc`).then(async module => {
|
|
24
|
+
this.logBeforeInvoke();
|
|
25
|
+
const start = Date.now();
|
|
23
26
|
const msg = await module.default(buildPath);
|
|
27
|
+
const elapsed = Date.now() - start;
|
|
24
28
|
if (msg) {
|
|
25
29
|
_sharedUtils.ColorConsole.success({
|
|
26
30
|
word: 'jsc command'
|
|
27
31
|
}, msg);
|
|
28
32
|
}
|
|
33
|
+
this.logAfterInvoke(elapsed);
|
|
29
34
|
}).then(() => {
|
|
30
35
|
if (deleteJs) {
|
|
31
36
|
return _sharedUtils.FileUtil.del(`${buildPath}/**/*.js`);
|
|
32
37
|
}
|
|
33
38
|
return true;
|
|
34
39
|
}).catch(error => {
|
|
35
|
-
|
|
40
|
+
const fields = [];
|
|
41
|
+
if (error.code !== undefined) fields.push(`code=${error.code}`);
|
|
42
|
+
if (error.status !== undefined) fields.push(`status=${error.status}`);
|
|
43
|
+
if (error.signal) fields.push(`signal=${error.signal}`);
|
|
44
|
+
if (error.killed !== undefined) fields.push(`killed=${error.killed}`);
|
|
45
|
+
if (error.cmd) fields.push(`cmd=${error.cmd}`);
|
|
46
|
+
const stderr = error.stderr ? Buffer.from(error.stderr).toString() : '';
|
|
47
|
+
const stdout = error.stdout ? Buffer.from(error.stdout).toString() : '';
|
|
48
|
+
const parts = [fields.length ? `[jsc] error fields: ${fields.join(' ')}` : '', stderr ? `[jsc] stderr:\n${stderr}` : '', stdout ? `[jsc] stdout:\n${stdout}` : ''];
|
|
49
|
+
const detail = parts.filter(Boolean).join('\n').trim();
|
|
50
|
+
_sharedUtils.ColorConsole.throw(detail ? `${error.message}\n${detail}` : error.message);
|
|
36
51
|
return Promise.reject(error);
|
|
37
52
|
});
|
|
38
53
|
}
|
|
54
|
+
logBeforeInvoke() {
|
|
55
|
+
const {
|
|
56
|
+
projectPath,
|
|
57
|
+
buildPath
|
|
58
|
+
} = this;
|
|
59
|
+
const jscName = '@aiot-toolkit/jsc';
|
|
60
|
+
try {
|
|
61
|
+
const pkgJsonPath = require.resolve(`${jscName}/package.json`, {
|
|
62
|
+
paths: [projectPath]
|
|
63
|
+
});
|
|
64
|
+
const pkg = require(pkgJsonPath);
|
|
65
|
+
_sharedUtils.ColorConsole.info(`[jsc] using ${jscName}@${pkg.version} resolved at ${pkgJsonPath}`);
|
|
66
|
+
} catch {
|
|
67
|
+
_sharedUtils.ColorConsole.info(`[jsc] cannot resolve ${jscName}/package.json from ${projectPath}`);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const files = Jsc.listFiles(buildPath, '.js');
|
|
71
|
+
const total = files.reduce((n, f) => n + f.size, 0);
|
|
72
|
+
_sharedUtils.ColorConsole.info(`[jsc] buildPath=${buildPath} files=${files.length} totalBytes=${total}`);
|
|
73
|
+
if (files.length) {
|
|
74
|
+
const sample = files.slice(0, 12).map(f => `${f.rel}:${f.size}`).join(', ');
|
|
75
|
+
const more = files.length > 12 ? ` ...and ${files.length - 12} more` : '';
|
|
76
|
+
_sharedUtils.ColorConsole.info(`[jsc] inputs: ${sample}${more}`);
|
|
77
|
+
}
|
|
78
|
+
} catch (e) {
|
|
79
|
+
_sharedUtils.ColorConsole.info(`[jsc] failed to enumerate inputs in ${buildPath}: ${e?.message ?? e}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
logAfterInvoke(elapsedMs) {
|
|
83
|
+
const {
|
|
84
|
+
buildPath
|
|
85
|
+
} = this;
|
|
86
|
+
try {
|
|
87
|
+
const jscFiles = Jsc.listFiles(buildPath, '.jsc');
|
|
88
|
+
const total = jscFiles.reduce((n, f) => n + f.size, 0);
|
|
89
|
+
_sharedUtils.ColorConsole.info(`[jsc] produced ${jscFiles.length} .jsc files (${total} bytes) in ${elapsedMs}ms`);
|
|
90
|
+
} catch {
|
|
91
|
+
/* ignore */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
static listFiles(dir, ext) {
|
|
95
|
+
const out = [];
|
|
96
|
+
if (!fs.existsSync(dir)) return out;
|
|
97
|
+
const walk = d => {
|
|
98
|
+
for (const name of fs.readdirSync(d)) {
|
|
99
|
+
const abs = path.join(d, name);
|
|
100
|
+
const stat = fs.statSync(abs);
|
|
101
|
+
if (stat.isDirectory()) walk(abs);else if (name.endsWith(ext)) {
|
|
102
|
+
out.push({
|
|
103
|
+
rel: path.relative(dir, abs),
|
|
104
|
+
size: stat.size
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
walk(dir);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
39
112
|
}
|
|
40
113
|
var _default = exports.default = Jsc;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/aiotpack",
|
|
3
|
-
"version": "2.1.0-prender.
|
|
3
|
+
"version": "2.1.0-prender.5",
|
|
4
4
|
"description": "The process tool for packaging aiot projects.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"aiotpack"
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"test": "node ./__tests__/aiotpack.test.js"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@aiot-toolkit/generator": "2.1.0-prender.
|
|
23
|
-
"@aiot-toolkit/parser": "2.1.0-prender.
|
|
24
|
-
"@aiot-toolkit/shared-utils": "2.1.0-prender.
|
|
22
|
+
"@aiot-toolkit/generator": "2.1.0-prender.5",
|
|
23
|
+
"@aiot-toolkit/parser": "2.1.0-prender.5",
|
|
24
|
+
"@aiot-toolkit/shared-utils": "2.1.0-prender.5",
|
|
25
25
|
"@hap-toolkit/aaptjs": "^2.0.0",
|
|
26
26
|
"@rspack/core": "^1.3.9",
|
|
27
27
|
"acorn": "^8.16.0",
|
|
28
28
|
"aiot-parse5": "^1.0.2",
|
|
29
29
|
"astring": "^1.9.0",
|
|
30
30
|
"babel-loader": "^9.1.3",
|
|
31
|
-
"file-lane": "2.1.0-prender.
|
|
31
|
+
"file-lane": "2.1.0-prender.5",
|
|
32
32
|
"file-loader": "^6.2.0",
|
|
33
33
|
"fs-extra": "^11.2.0",
|
|
34
34
|
"jsrsasign": "^11.1.0",
|