@aiot-toolkit/aiotpack 2.1.0-prender.2 → 2.1.0-prender.4

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());
@@ -11,6 +11,50 @@ var _UxCompileUtil = _interopRequireDefault(require("./vela/utils/UxCompileUtil"
11
11
  var _UxFileUtils = _interopRequireDefault(require("../../utils/ux/UxFileUtils"));
12
12
  var _UxLoaderUtils = _interopRequireDefault(require("../../utils/ux/UxLoaderUtils"));
13
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
+ function loadUserViteConfig(projectPath, mode) {
15
+ const configPath = _path.default.join(projectPath, 'quickapp.config.js');
16
+ if (!_fsExtra.default.existsSync(configPath)) return {};
17
+ delete require.cache[require.resolve(configPath)];
18
+ const userConfig = require(configPath);
19
+ if (userConfig?.postHook && !userConfig?.vite) {
20
+ 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: [ ... ] } }`);
21
+ }
22
+ const viteSection = userConfig?.vite;
23
+ if (!viteSection) return {};
24
+ if (typeof viteSection === 'function') {
25
+ return viteSection({
26
+ mode
27
+ }) ?? {};
28
+ }
29
+ return viteSection;
30
+ }
31
+ function extractAppStyleLiteral(code) {
32
+ const m = code.match(/var\s+\$app_style\$\s*=\s*\[/);
33
+ if (!m) return null;
34
+ let i = m.index + m[0].length - 1;
35
+ let depth = 0;
36
+ let inStr = null;
37
+ for (; i < code.length; i++) {
38
+ const c = code[i];
39
+ if (inStr) {
40
+ if (c === '\\') {
41
+ i++;
42
+ continue;
43
+ }
44
+ if (c === inStr) inStr = null;
45
+ continue;
46
+ }
47
+ if (c === '"' || c === "'") {
48
+ inStr = c;
49
+ continue;
50
+ }
51
+ if (c === '[') depth++;else if (c === ']') {
52
+ depth--;
53
+ if (depth === 0) return code.slice(m.index + m[0].length - 1, i + 1);
54
+ }
55
+ }
56
+ return null;
57
+ }
14
58
  class ViteCompiler {
15
59
  constructor(context, onLog) {
16
60
  this.context = context;
@@ -29,6 +73,15 @@ class ViteCompiler {
29
73
  const manifestPath = _UxFileUtils.default.getManifestFilePath(originalProjectPath, sourceRoot);
30
74
  const manifest = _fsExtra.default.readJSONSync(manifestPath);
31
75
  const entries = _UxCompileUtil.default.resolveEntries(manifest, srcPath, originalProjectPath);
76
+ let userVite = {};
77
+ try {
78
+ userVite = loadUserViteConfig(originalProjectPath, param.mode === 'production' ? 'production' : 'development');
79
+ } catch (e) {
80
+ this.onLog?.([{
81
+ level: 1,
82
+ message: [`Failed to load quickapp.config.js: ${e.message}`]
83
+ }]);
84
+ }
32
85
  for (const [entryName, entryValue] of Object.entries(entries)) {
33
86
  const entryPath = entryValue.split('?')[0];
34
87
  const resolvedEntry = _path.default.resolve(originalProjectPath, entryPath);
@@ -46,8 +99,10 @@ class ViteCompiler {
46
99
  const result = await build({
47
100
  root: originalProjectPath,
48
101
  logLevel: 'silent',
102
+ define: userVite.define ?? {},
49
103
  resolve: {
50
- extensions: ['.js', '.ts', '.ux', '.json']
104
+ extensions: ['.js', '.ts', '.ux', '.json'],
105
+ ...(userVite.resolve ?? {})
51
106
  },
52
107
  build: {
53
108
  write: false,
@@ -148,11 +203,9 @@ class ViteCompiler {
148
203
  const compiledCode = compFiles[0]?.content || '';
149
204
  // Extract $app_style$ array from compiled code
150
205
  let styleSheet = {};
151
- const styleMatch = compiledCode.match(/var \$app_style\$ = (\[[\s\S]*?\]);?\s*(?:import|export|var |const |$)/);
152
- if (styleMatch) {
153
- if (styleMatch) {
154
- styleSheet = (0, _TemplateCompiler.parseStyleArray)(styleMatch[1]);
155
- }
206
+ const styleLiteral = extractAppStyleLiteral(compiledCode);
207
+ if (styleLiteral) {
208
+ styleSheet = (0, _TemplateCompiler.parseStyleArray)(styleLiteral);
156
209
  }
157
210
  // Write css.json
158
211
  const cssPath = _path.default.join(buildPath, `${isApp ? 'app' : pageName}.css.json`);
@@ -226,9 +279,9 @@ class ViteCompiler {
226
279
  }, false, param, compilation);
227
280
  let compStyleSheet = {};
228
281
  const compCode = compResult.files[0]?.content || '';
229
- const csm = compCode.match(/var \$app_style\$ = (\[[\s\S]*?\]);?\s*(?:import|export|var |const |$)/);
282
+ const csm = extractAppStyleLiteral(compCode);
230
283
  if (csm) {
231
- compStyleSheet = (0, _TemplateCompiler.parseStyleArray)(csm[1]);
284
+ compStyleSheet = (0, _TemplateCompiler.parseStyleArray)(csm);
232
285
  }
233
286
  const compSid = (0, _TemplateCompiler.getStyleObjectId)(compPath);
234
287
  _fsExtra.default.writeJSONSync(compTplPath, (0, _TemplateCompiler.uxToTemplateJson)(compTpl, compSid, compStyleSheet, compImportMap, _path.default.dirname(compPath)), param.mode === 'production' ? undefined : {
@@ -260,7 +313,7 @@ class ViteCompiler {
260
313
  }, isApp, param, compilation);
261
314
  return files[0]?.content || '';
262
315
  }
263
- }]
316
+ }, ...(userVite.plugins ?? [])]
264
317
  });
265
318
  // Extract the output code
266
319
  if (result && !Array.isArray(result)) {
@@ -337,9 +390,9 @@ class ViteCompiler {
337
390
  const compCode = files[0]?.content || '';
338
391
  // Extract style
339
392
  let styleSheet = {};
340
- const csm = compCode.match(/var \$app_style\$ = (\[[\s\S]*?\]);?\s*(?:import|export|var |const |$)/);
393
+ const csm = extractAppStyleLiteral(compCode);
341
394
  if (csm) {
342
- styleSheet = (0, _TemplateCompiler.parseStyleArray)(csm[1]);
395
+ styleSheet = (0, _TemplateCompiler.parseStyleArray)(csm);
343
396
  }
344
397
  // Generate template.json
345
398
  if (templateNode) {
@@ -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
- * Jsc
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
- _sharedUtils.ColorConsole.throw(error.message);
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.2",
3
+ "version": "2.1.0-prender.4",
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.2",
23
- "@aiot-toolkit/parser": "2.1.0-prender.2",
24
- "@aiot-toolkit/shared-utils": "2.1.0-prender.2",
22
+ "@aiot-toolkit/generator": "2.1.0-prender.4",
23
+ "@aiot-toolkit/parser": "2.1.0-prender.4",
24
+ "@aiot-toolkit/shared-utils": "2.1.0-prender.4",
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.2",
31
+ "file-lane": "2.1.0-prender.4",
32
32
  "file-loader": "^6.2.0",
33
33
  "fs-extra": "^11.2.0",
34
34
  "jsrsasign": "^11.1.0",