@aiot-toolkit/aiotpack 2.0.5-beta.14 → 2.0.5-beta.16

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.
@@ -4,7 +4,7 @@ import IJavascriptCompileOption from '../../compiler/javascript/interface/IJavas
4
4
  * UxAfterCompile
5
5
  */
6
6
  declare class UxAfterCompile {
7
- static webpack: FollowWork<IJavascriptCompileOption>;
7
+ static compileJavascript: FollowWork<IJavascriptCompileOption>;
8
8
  static protobuf: FollowWork<IJavascriptCompileOption>;
9
9
  static jsc: FollowWork<IJavascriptCompileOption>;
10
10
  static toRpk: FollowWork<IJavascriptCompileOption>;
@@ -15,6 +15,13 @@ declare class UxAfterCompile {
15
15
  * @param compilerOption
16
16
  */
17
17
  static copyResource: FollowWork<IJavascriptCompileOption>;
18
+ /**
19
+ * 压缩资源文件
20
+ * 1. png8
21
+ * 2. bin
22
+ * @param params
23
+ */
24
+ static compressResource: FollowWork<IJavascriptCompileOption>;
18
25
  /**
19
26
  * 如果是更新触发,则对比临时项目build文件夹 和 项目build文件夹,生成diff.json
20
27
  * @param context
@@ -17,6 +17,7 @@ var _UxFileUtils = _interopRequireDefault(require("../../utils/ux/UxFileUtils"))
17
17
  var _FileLaneTriggerType = _interopRequireDefault(require("file-lane/lib/enum/FileLaneTriggerType"));
18
18
  var _ZipFileUtil = _interopRequireDefault(require("@aiot-toolkit/shared-utils/lib/utils/ZipFileUtil"));
19
19
  var _process = require("process");
20
+ var _ImageIcu = _interopRequireDefault(require("../../compiler/tools/icu/ImageIcu"));
20
21
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
22
  const BinaryPlugin = require('@aiot-toolkit/parser/lib/ux/translate/vela/protobuf/BinaryPlugin');
22
23
  const HapToolkit = require('hap-toolkit');
@@ -25,7 +26,7 @@ const HapToolkit = require('hap-toolkit');
25
26
  * UxAfterCompile
26
27
  */
27
28
  class UxAfterCompile {
28
- static webpack = async params => {
29
+ static compileJavascript = async params => {
29
30
  const {
30
31
  context,
31
32
  compilerOption
@@ -146,7 +147,7 @@ class UxAfterCompile {
146
147
  sourceRoot,
147
148
  outputPath
148
149
  } = compilerOption;
149
- const includeList = [/sitemap\.json$/, /i18n(.+)\.json/];
150
+ const includeList = [/sitemap\.json$/, /i18n(.+)\.json/, /web\/*/];
150
151
  const excludeExtList = ['js', 'jsx', 'coffee', 'ts', 'tsx', 'vue', 'css', 'less', 'sass', 'styl', 'html', 'md', 'ux', 'mix', 'DS_Store', 'map'];
151
152
  const excludeReg = `\\.(${excludeExtList.join('|')})$`;
152
153
  _sharedUtils.FileUtil.copyFiles(_path.default.join(projectPath, sourceRoot), _path.default.join(projectPath, outputPath), new RegExp(excludeReg));
@@ -253,6 +254,37 @@ class UxAfterCompile {
253
254
  await genMetaFiles(compilerOption);
254
255
  }
255
256
  };
257
+
258
+ /**
259
+ * 压缩资源文件
260
+ * 1. png8
261
+ * 2. bin
262
+ * @param params
263
+ */
264
+ static compressResource = async params => {
265
+ const {
266
+ compilerOption
267
+ } = params;
268
+ if (compilerOption) {
269
+ const {
270
+ enableImagePng8,
271
+ projectPath,
272
+ outputPath
273
+ } = compilerOption;
274
+ if (enableImagePng8) {
275
+ const format = 'png';
276
+ const files = _sharedUtils.FileUtil.readAlldirSync(_path.default.join(projectPath, outputPath));
277
+ const compressFiles = files.filter(filePath => _ImageIcu.default.canTranslate(filePath, format));
278
+ if (compressFiles.length) {
279
+ const icu = new _ImageIcu.default();
280
+ compressFiles.forEach(item => {
281
+ icu.translate(item, _path.default.dirname(item), format);
282
+ });
283
+ }
284
+ }
285
+ }
286
+ };
287
+
256
288
  /**
257
289
  * 如果是更新触发,则对比临时项目build文件夹 和 项目build文件夹,生成diff.json
258
290
  * @param context
@@ -288,8 +320,8 @@ class UxAfterCompile {
288
320
  const diffFileName = `.diff.rpk`;
289
321
  const diffPath = _path.default.join(distFold, diffFileName);
290
322
  await _ZipFileUtil.default.createZipFile(diffPath, diffList.map(item => ({
291
- filePath: _path.default.join(buildFold, item),
292
- zipFilePath: item
323
+ filePath: _path.default.join(buildFold, item.path),
324
+ zipFilePath: item.path
293
325
  })));
294
326
  compalition.info.diffList = diffList;
295
327
  compalition.info.diffFile = _path.default.join(context.projectPath, compilerOption.releasePath, diffFileName);
@@ -9,7 +9,7 @@ var _fileLane = require("file-lane");
9
9
  class UxAfterWorks {
10
10
  static async cleanOutput(context) {
11
11
  const outputPath = _fileLane.FileLaneUtil.getOutputPath(context);
12
- _sharedUtils.FileUtil.del(outputPath);
12
+ await _sharedUtils.FileUtil.del(outputPath);
13
13
  }
14
14
  }
15
15
  var _default = exports.default = UxAfterWorks;
@@ -4,6 +4,15 @@ import IJavascriptCompileOption from './interface/IJavascriptCompileOption';
4
4
  declare class JavascriptCompiler implements ICompiler {
5
5
  readonly QUICKAPP_CONFIG = "quickapp.config.js";
6
6
  compile(param: IJavascriptCompileOption): Promise<void>;
7
+ /**
8
+ * 压缩插件
9
+ *
10
+ * dev模式,只处理console,其它禁用
11
+ * prod模式,其它使用默认值
12
+ * @param mode
13
+ * @returns
14
+ */
15
+ private createMinimizerRspackPlugin;
7
16
  private createWebpackConfig;
8
17
  private getConfigurator;
9
18
  clean(param: ICompileParam & IJavascriptCompileOption): Promise<void>;
@@ -11,6 +11,7 @@ var _path = _interopRequireDefault(require("path"));
11
11
  var _AndroidWebpackConfigurator = _interopRequireDefault(require("./android/AndroidWebpackConfigurator"));
12
12
  var _VelaWebpackConfigurator = _interopRequireDefault(require("./vela/VelaWebpackConfigurator"));
13
13
  var _UxCompileUtil = _interopRequireDefault(require("./vela/utils/UxCompileUtil"));
14
+ var _CompileMode = _interopRequireDefault(require("../enum/CompileMode"));
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  class JavascriptCompiler {
16
17
  QUICKAPP_CONFIG = 'quickapp.config.js';
@@ -49,6 +50,65 @@ class JavascriptCompiler {
49
50
  }
50
51
  });
51
52
  }
53
+
54
+ /**
55
+ * 压缩插件
56
+ *
57
+ * dev模式,只处理console,其它禁用
58
+ * prod模式,其它使用默认值
59
+ * @param mode
60
+ * @returns
61
+ */
62
+ createMinimizerRspackPlugin(compileOption) {
63
+ const {
64
+ mode,
65
+ dropConsole
66
+ } = compileOption;
67
+ const translateDropConsole = () => {
68
+ let dropConsoleValue;
69
+ if (dropConsole === undefined) {
70
+ dropConsoleValue = false;
71
+ } else if (dropConsole === 'true') {
72
+ dropConsoleValue = true;
73
+ } else if (dropConsole === 'false') {
74
+ dropConsoleValue = false;
75
+ } else {
76
+ dropConsoleValue = dropConsole;
77
+ }
78
+ return dropConsoleValue;
79
+ };
80
+ const createCompressValue = dropConsoleValue => {
81
+ const result = {};
82
+ if (typeof dropConsoleValue === 'boolean') {
83
+ result.drop_console = dropConsoleValue;
84
+ } else if (typeof dropConsoleValue === 'string') {
85
+ result.pure_funcs = dropConsoleValue.split(',').map(item => item.trim()).filter(Boolean).map(item => `console.${item}`);
86
+ }
87
+ return result;
88
+ };
89
+ if (mode === _CompileMode.default.DEVELOPMENT) {
90
+ return new _core.rspack.SwcJsMinimizerRspackPlugin({
91
+ minimizerOptions: {
92
+ module: true,
93
+ minify: false,
94
+ mangle: false,
95
+ compress: {
96
+ defaults: false,
97
+ ...createCompressValue(translateDropConsole())
98
+ }
99
+ }
100
+ });
101
+ } else {
102
+ return new _core.rspack.SwcJsMinimizerRspackPlugin({
103
+ minimizerOptions: {
104
+ module: true,
105
+ compress: {
106
+ ...createCompressValue(translateDropConsole())
107
+ }
108
+ }
109
+ });
110
+ }
111
+ }
52
112
  createWebpackConfig(param) {
53
113
  const configurator = this.getConfigurator(param);
54
114
  if (!configurator) {
@@ -88,11 +148,8 @@ class JavascriptCompiler {
88
148
  }]
89
149
  },
90
150
  optimization: {
91
- minimizer: [new _core.rspack.SwcJsMinimizerRspackPlugin({
92
- minimizerOptions: {
93
- module: true
94
- }
95
- })]
151
+ minimize: true,
152
+ minimizer: [this.createMinimizerRspackPlugin(param)]
96
153
  },
97
154
  resolve: {
98
155
  extensions: ['.js', '.ts', '.ux']
@@ -48,6 +48,10 @@ export default interface IJavascriptCompileOption extends ICompileParam {
48
48
  * 启用应用自动化测试
49
49
  */
50
50
  enableE2e?: boolean;
51
+ /**
52
+ * 启用 png8 压缩
53
+ */
54
+ enableImagePng8?: boolean;
51
55
  /**
52
56
  * 启用代码体积分析,会生成 report.html 文件,可查看打包后各模块占用体积
53
57
  */
@@ -56,6 +60,17 @@ export default interface IJavascriptCompileOption extends ICompileParam {
56
60
  * 是否自动补全 manifest.json 中的 features 配置
57
61
  */
58
62
  completeFeature?: boolean;
63
+ /**
64
+ * 是否删除 console
65
+ *
66
+ * true:删除全部 console
67
+ * string: 指定要去除的console,如'log'、'warn'、'error'
68
+ *
69
+ * @example true
70
+ *
71
+ * @example log,warn
72
+ */
73
+ dropConsole?: boolean | string;
59
74
  /**
60
75
  * 获取远程证书
61
76
  */
@@ -0,0 +1,37 @@
1
+ declare class ImageIcu {
2
+ static EXT: string;
3
+ static INCLUDE_LIST: string[];
4
+ /**
5
+ * @private
6
+ * @returns
7
+ */
8
+ getBinPath(): string;
9
+ /**
10
+ * 文件转换为 bin 格式
11
+ * @param {*} resourcePath 源文件
12
+ * @param {*} folder 产物目录
13
+ * @param {'lvgl'|'png'} format
14
+ * @returns
15
+ */
16
+ translate(resourcePath: string, folder: string, format?: string): string | undefined;
17
+ /**
18
+ * 资源转换为 bin 文件路径
19
+ *
20
+ * 如果是 png, jpg,jpeg, webp, bmp,则转换为 .bin 后缀;其它类型不处理
21
+ *
22
+ * **此方法仅返回转换后的路径, 不会实际转换文件**
23
+ * @param {string} path
24
+ * @example assetsToBinPath('images/test.png') // images/test.bin
25
+ *
26
+ * @return {string} 可转则返回对应的 bin 路径;否则返回''
27
+ */
28
+ static assetsToBinPath(assetsPath: string): string;
29
+ /**
30
+ * 是否可转换
31
+ * @param {string} assetsPath
32
+ * @param {'lvgl'|'png'} format
33
+ * @returns
34
+ */
35
+ static canTranslate(assetsPath: string, format?: string): boolean;
36
+ }
37
+ export default ImageIcu;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _os = _interopRequireDefault(require("os"));
9
+ var _fs = _interopRequireDefault(require("fs"));
10
+ var _child_process = require("child_process");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ class ImageIcu {
13
+ static EXT = '.bin';
14
+ static INCLUDE_LIST = ['.png', '.jpg', '.jpeg', '.webp', '.bmp'];
15
+ /**
16
+ * @private
17
+ * @returns
18
+ */
19
+ getBinPath() {
20
+ const platform = _os.default.platform();
21
+ const arch = _os.default.arch();
22
+ let result = _path.default.join(__dirname, `icu_${platform}_${arch}`);
23
+ if (platform === 'win32') {
24
+ result += '.exe';
25
+ }
26
+ return result;
27
+ }
28
+
29
+ /**
30
+ * 文件转换为 bin 格式
31
+ * @param {*} resourcePath 源文件
32
+ * @param {*} folder 产物目录
33
+ * @param {'lvgl'|'png'} format
34
+ * @returns
35
+ */
36
+ translate(resourcePath, folder) {
37
+ let format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'lvgl';
38
+ if (!_fs.default.existsSync(resourcePath) || !ImageIcu.canTranslate(resourcePath)) {
39
+ return;
40
+ }
41
+ const binPath = this.getBinPath();
42
+ if (_fs.default.existsSync(binPath)) {
43
+ const pathParse = _path.default.parse(resourcePath);
44
+
45
+ // 后缀,lvgl格式的后缀为"bin", 其它保持原后缀
46
+ const extDic = {
47
+ lvgl: ImageIcu.EXT,
48
+ png: pathParse.ext
49
+ };
50
+ const ext = extDic[format];
51
+ if (!_fs.default.existsSync(folder)) {
52
+ _fs.default.mkdirSync(folder, {
53
+ recursive: true
54
+ });
55
+ }
56
+ const command = `${binPath} convert ${resourcePath} -O ${folder} -F ${format} -C i8 -r`;
57
+ const message = (0, _child_process.execSync)(command).toString('utf-8');
58
+ if (message) {
59
+ console.log(`convert error: path=${resourcePath} message=${message}`);
60
+ return '';
61
+ }
62
+ const result = _path.default.join(pathParse.dir, `${pathParse.name}${ext}`);
63
+ return result;
64
+ } else {
65
+ return resourcePath;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * 资源转换为 bin 文件路径
71
+ *
72
+ * 如果是 png, jpg,jpeg, webp, bmp,则转换为 .bin 后缀;其它类型不处理
73
+ *
74
+ * **此方法仅返回转换后的路径, 不会实际转换文件**
75
+ * @param {string} path
76
+ * @example assetsToBinPath('images/test.png') // images/test.bin
77
+ *
78
+ * @return {string} 可转则返回对应的 bin 路径;否则返回''
79
+ */
80
+ static assetsToBinPath(assetsPath) {
81
+ if (!assetsPath) {
82
+ return '';
83
+ }
84
+ const pathParse = _path.default.parse(assetsPath);
85
+ if (ImageIcu.canTranslate(assetsPath)) {
86
+ return _path.default.join(pathParse.dir, `${pathParse.name}${ImageIcu.EXT}`);
87
+ }
88
+ return '';
89
+ }
90
+
91
+ /**
92
+ * 是否可转换
93
+ * @param {string} assetsPath
94
+ * @param {'lvgl'|'png'} format
95
+ * @returns
96
+ */
97
+ static canTranslate(assetsPath) {
98
+ let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'lvgl';
99
+ if (!assetsPath) {
100
+ return false;
101
+ }
102
+ const pathParse = _path.default.parse(assetsPath);
103
+ const ext = pathParse.ext.toLowerCase();
104
+ if (format === 'png') {
105
+ return ext === '.png';
106
+ }
107
+ return ImageIcu.INCLUDE_LIST.includes(ext);
108
+ }
109
+ }
110
+ var _default = exports.default = ImageIcu;
@@ -78,11 +78,14 @@ class UxConfig {
78
78
  worker: _UxAfterCompile.default.symlinkNodeModule,
79
79
  workerDescribe: 'Create a soft link to the node_modules folder'
80
80
  }, {
81
- worker: _UxAfterCompile.default.webpack,
82
- workerDescribe: 'Compile the project using webpack'
81
+ worker: _UxAfterCompile.default.compileJavascript,
82
+ workerDescribe: 'Compile javascript project'
83
83
  }, {
84
84
  worker: _UxAfterCompile.default.copyResource,
85
85
  workerDescribe: 'Copy resource files'
86
+ }, {
87
+ worker: _UxAfterCompile.default.compressResource,
88
+ workerDescribe: 'Compress resource files'
86
89
  }, {
87
90
  worker: _UxAfterCompile.default.jsc,
88
91
  workerDescribe: 'Generate jsc bytecode'
@@ -23,6 +23,7 @@ export declare class UxFileUtils {
23
23
  * 1. TypeError: Error
24
24
  * 2. 其它: Warn
25
25
  * 2. 检查 router.entry 是否存在于 router.pages
26
+ * 3. 检查icon是否存在
26
27
  *
27
28
  * @param projectPath 项目路径
28
29
  * @param sourceRoot 源码路径--相对项目根目录
@@ -47,6 +48,9 @@ export declare class UxFileUtils {
47
48
  * @param newFileList 新文件列表
48
49
  * @returns
49
50
  */
50
- static getDiffJSON(compilation: FileLaneCompilation, newFileList: string[], buildPath: string): string[];
51
+ static getDiffJSON(compilation: FileLaneCompilation, newFileList: string[], buildPath: string): {
52
+ path: string;
53
+ content: Buffer;
54
+ }[];
51
55
  }
52
56
  export default UxFileUtils;
@@ -42,6 +42,7 @@ class UxFileUtils {
42
42
  * 1. TypeError: Error
43
43
  * 2. 其它: Warn
44
44
  * 2. 检查 router.entry 是否存在于 router.pages
45
+ * 3. 检查icon是否存在
45
46
  *
46
47
  * @param projectPath 项目路径
47
48
  * @param sourceRoot 源码路径--相对项目根目录
@@ -60,15 +61,25 @@ class UxFileUtils {
60
61
  const errors = _CommonUtil.default.validateJson(jsonData, schema) || [];
61
62
 
62
63
  // 2
63
- const {
64
- entry,
65
- pages
66
- } = jsonData.router;
67
- if (!pages[entry]) {
68
- errors.push(new TypeError(`router.entry content: ${entry}, is missing in router.pages`));
64
+ if (jsonData.router) {
65
+ const {
66
+ entry,
67
+ pages
68
+ } = jsonData.router;
69
+ if (!pages[entry]) {
70
+ errors.push(new TypeError(`router.entry content: ${entry}, is missing in router.pages`));
71
+ }
72
+ }
73
+
74
+ // 3
75
+ if (jsonData.icon) {
76
+ const iconPath = _path.default.join(projectPath, sourceRoot, jsonData.icon);
77
+ if (!_fsExtra.default.existsSync(iconPath)) {
78
+ errors.push(new Error(`icon: "${jsonData.icon}" is missing in project`));
79
+ }
69
80
  }
70
81
  if (errors?.length) {
71
- const message = `Manifest.json error: ${errors.map((item, index) => {
82
+ const message = `error in ${path}: ${errors.map((item, index) => {
72
83
  return `\r\n${index + 1}. ${item.message}`;
73
84
  })}`;
74
85
  throw new Error(message);
@@ -124,16 +135,22 @@ class UxFileUtils {
124
135
  if (index !== -1) {
125
136
  // 1.2
126
137
  const oldContent = oldFileList[i].content?.toString('utf-8');
127
- const newContent = _fsExtra.default.readFileSync(_path.default.join(buildPath, newFileList[index]), 'utf-8');
128
- if (oldContent !== newContent) {
129
- diffList.push(newFileList[index]);
138
+ const newContent = _fsExtra.default.readFileSync(_path.default.join(buildPath, newFileList[index]));
139
+ if (oldContent !== newContent.toString()) {
140
+ diffList.push({
141
+ path: newFileList[index],
142
+ content: newContent
143
+ });
130
144
  }
131
145
  // 从newFileList中移除该项
132
146
  newFileList.splice(index, 1);
133
147
  }
134
148
  }
135
149
  // 2.
136
- newFileList.length > 0 && newFileList.forEach(file => diffList.push(file));
150
+ newFileList.length > 0 && newFileList.forEach(file => diffList.push({
151
+ path: file,
152
+ content: _fsExtra.default.readFileSync(_path.default.join(buildPath, file))
153
+ }));
137
154
  return diffList;
138
155
  }
139
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/aiotpack",
3
- "version": "2.0.5-beta.14",
3
+ "version": "2.0.5-beta.16",
4
4
  "description": "The process tool for packaging aiot projects.",
5
5
  "keywords": [
6
6
  "aiotpack"
@@ -19,14 +19,14 @@
19
19
  "test": "node ./__tests__/aiotpack.test.js"
20
20
  },
21
21
  "dependencies": {
22
- "@aiot-toolkit/generator": "2.0.5-beta.14",
23
- "@aiot-toolkit/parser": "2.0.5-beta.14",
24
- "@aiot-toolkit/shared-utils": "2.0.5-beta.14",
22
+ "@aiot-toolkit/generator": "2.0.5-beta.16",
23
+ "@aiot-toolkit/parser": "2.0.5-beta.16",
24
+ "@aiot-toolkit/shared-utils": "2.0.5-beta.16",
25
25
  "@hap-toolkit/aaptjs": "^2.0.0",
26
- "@rspack/core": "^1.1.8",
26
+ "@rspack/core": "^1.3.9",
27
27
  "aiot-parse5": "^1.0.2",
28
28
  "babel-loader": "^9.1.3",
29
- "file-lane": "2.0.5-beta.14",
29
+ "file-lane": "2.0.5-beta.16",
30
30
  "file-loader": "^6.2.0",
31
31
  "fs-extra": "^11.2.0",
32
32
  "hap-toolkit": "^2.0.0",
@@ -35,7 +35,6 @@
35
35
  "lodash": "^4.17.21",
36
36
  "ts-morph": "^19.0.0",
37
37
  "url-loader": "^4.1.1",
38
- "webpack": "^5.97.1",
39
38
  "webpack-bundle-analyzer": "^4.10.2",
40
39
  "webpack-sources": "^3.2.3"
41
40
  },
@@ -44,5 +43,5 @@
44
43
  "@types/jsrsasign": "^10.5.12",
45
44
  "@types/webpack-sources": "^3.2.3"
46
45
  },
47
- "gitHead": "63c6564afe9bedd9c034f03e8e457b6cfa6960ae"
46
+ "gitHead": "66255b9d69f9b15db10283d662f94f3d1f2486c3"
48
47
  }