@aiot-toolkit/aiotpack 2.0.2-beta.1 → 2.0.2-beta.10

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.
Files changed (28) hide show
  1. package/lib/compiler/javascript/JavascriptCompiler.js +13 -2
  2. package/lib/compiler/javascript/interface/IJavascriptCompileOption.d.ts +9 -1
  3. package/lib/compiler/javascript/vela/VelaWebpackConfigurator.js +11 -2
  4. package/lib/compiler/javascript/vela/utils/UxCompileUtil.js +1 -1
  5. package/lib/compiler/javascript/vela/utils/ZipUtil.js +1 -1
  6. package/lib/compiler/javascript/vela/utils/webpackLoader/addColSourceMap.d.ts +3 -0
  7. package/lib/compiler/javascript/vela/utils/webpackLoader/addColSourceMap.js +59 -0
  8. package/lib/compiler/javascript/vela/utils/webpackLoader/extractMapData.d.ts +3 -0
  9. package/lib/compiler/javascript/vela/utils/webpackLoader/{splitMap.js → extractMapData.js} +2 -2
  10. package/lib/config/UxConfig.d.ts +7 -18
  11. package/lib/config/UxConfig.js +55 -10
  12. package/lib/config/XtsConfig.d.ts +5 -2
  13. package/lib/config/XtsConfig.js +12 -3
  14. package/lib/{utils → followWorks}/ux/UxFollowWorks.js +14 -9
  15. package/lib/loader/ux/AppUxLoader.js +14 -2
  16. package/lib/loader/ux/HmlLoader.d.ts +24 -0
  17. package/lib/loader/ux/HmlLoader.js +63 -0
  18. package/lib/loader/ux/JsLoader.d.ts +2 -0
  19. package/lib/loader/ux/JsLoader.js +2 -1
  20. package/lib/loader/ux/UxLoader.js +2 -2
  21. package/lib/{utils → preWorks}/ux/UxPreWorks.js +1 -1
  22. package/lib/utils/PreWorkUtils.js +7 -4
  23. package/lib/utils/ux/UxLoaderUtils.d.ts +4 -1
  24. package/lib/utils/ux/UxLoaderUtils.js +35 -15
  25. package/package.json +8 -5
  26. package/lib/compiler/javascript/vela/utils/webpackLoader/splitMap.d.ts +0 -3
  27. /package/lib/{utils → followWorks}/ux/UxFollowWorks.d.ts +0 -0
  28. /package/lib/{utils → preWorks}/ux/UxPreWorks.d.ts +0 -0
@@ -77,10 +77,10 @@ class JavascriptCompiler {
77
77
  module: {
78
78
  rules: [
79
79
  {
80
- test: /\.js$/,
80
+ test: /\.ux$/,
81
81
  use: [
82
82
  {
83
- loader: path_1.default.join(__dirname, '../javascript/vela/utils/webpackLoader/splitMap.js')
83
+ loader: path_1.default.join(__dirname, '../javascript/vela/utils/webpackLoader/extractMapData.js')
84
84
  }
85
85
  ]
86
86
  }
@@ -108,6 +108,17 @@ class JavascriptCompiler {
108
108
  Array.isArray(readyRules) && readyRules.length > 0
109
109
  ? [...configuratorRules, ...readyRules]
110
110
  : configuratorRules;
111
+ // 判断devtool类型 inline-source-map时,添加第0列关系映射
112
+ if (devtool === 'inline-source-map') {
113
+ result.module.rules.unshift({
114
+ test: /\.ux$/,
115
+ use: [
116
+ {
117
+ loader: path_1.default.join(__dirname, '../javascript/vela/utils/webpackLoader/addColSourceMap.js')
118
+ }
119
+ ]
120
+ });
121
+ }
111
122
  }
112
123
  if (configurator.createPlugins) {
113
124
  result.plugins = configurator.createPlugins();
@@ -36,9 +36,17 @@ export default interface IJavascriptCompileOption extends ICompileParam {
36
36
  /**
37
37
  * 禁用 jsc
38
38
  */
39
- disabledJSC?: boolean;
39
+ disabledJsc?: boolean;
40
40
  /**
41
41
  * 启用 protobuf
42
42
  */
43
43
  enableProtobuf?: boolean;
44
+ /**
45
+ * 启用应用自动化测试
46
+ */
47
+ enableE2e?: boolean;
48
+ /**
49
+ * 启用代码体积分析,会生成 report.html 文件,可查看打包后各模块占用体积
50
+ */
51
+ enableStats?: boolean;
44
52
  }
@@ -10,10 +10,20 @@ const WrapPlugin_1 = __importDefault(require("./plugin/WrapPlugin"));
10
10
  const UxCompileUtil_1 = __importDefault(require("./utils/UxCompileUtil"));
11
11
  class VelaWebpackConfigurator {
12
12
  createPlugins() {
13
- return [
13
+ const result = [
14
14
  // 给 入口js 添加包裹
15
15
  new WrapPlugin_1.default()
16
16
  ];
17
+ // 如果开启 stats 参数,则添加 webpack-bundle-analyzer 插件
18
+ if (this.param.enableStats) {
19
+ const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
20
+ result.push(new BundleAnalyzerPlugin({
21
+ analyzerMode: 'static',
22
+ openAnalyzer: false,
23
+ excludeAssets: /^@(system|service)\./
24
+ }));
25
+ }
26
+ return result;
17
27
  }
18
28
  /**
19
29
  * 通过读取 manifest.json 生成 entry
@@ -40,7 +50,6 @@ class VelaWebpackConfigurator {
40
50
  {
41
51
  loader: require.resolve('babel-loader'),
42
52
  options: {
43
- // configFile: getBabelConfigJsPath(cwd),
44
53
  cwd: this.param.projectPath,
45
54
  cacheDirectory: true
46
55
  }
@@ -130,7 +130,7 @@ class UxCompileUtil {
130
130
  * @returns
131
131
  */
132
132
  static getExtensionList(withDot = true) {
133
- const result = ['js'];
133
+ const result = ['ux'];
134
134
  if (withDot) {
135
135
  return result.map((item) => `.${item}`);
136
136
  }
@@ -180,7 +180,7 @@ class ZipUtil {
180
180
  return !(item.startsWith(UxCompileUtil_1.default.DIGEST_ZIP_DIR) ||
181
181
  (param.mode === CompileMode_1.default.PRODUCTION && mapReg.test(item)));
182
182
  })
183
- .map((item) => path_1.default.relative(dist, item));
183
+ .map((item) => path_1.default.relative(dist, item).split(path_1.default.sep).join(path_1.default.posix.sep));
184
184
  result.sort((a, b) => {
185
185
  const indexA = getFileIndex(a);
186
186
  const indexB = getFileIndex(b);
@@ -0,0 +1,3 @@
1
+ import { LoaderContext } from 'webpack';
2
+ declare function addColSourceMap(this: LoaderContext<any>, source: string, map: any): Promise<void>;
3
+ export default addColSourceMap;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const SourceMapUtil_1 = __importDefault(require("@aiot-toolkit/parser/lib/ux/translate/vela/utils/SourceMapUtil"));
16
+ function addColSourceMap(source, map) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const callback = this.async();
19
+ if (!map) {
20
+ callback(null, source, map);
21
+ }
22
+ let { consumer, tempGenerator } = yield SourceMapUtil_1.default.createConsumerAndGenerator(JSON.stringify(map));
23
+ let recordLine = -1;
24
+ consumer.eachMapping((mapping) => {
25
+ if (mapping.source === consumer.sources[consumer.sources.length - 1]) {
26
+ if (recordLine < mapping.generatedLine &&
27
+ (mapping.generatedColumn !== 0 || mapping.originalColumn !== 0)) {
28
+ tempGenerator.addMapping({
29
+ generated: {
30
+ line: mapping.generatedLine,
31
+ column: 0
32
+ },
33
+ original: {
34
+ line: mapping.originalLine,
35
+ column: 0
36
+ },
37
+ source: mapping.source,
38
+ name: ''
39
+ });
40
+ }
41
+ recordLine = mapping.generatedLine;
42
+ }
43
+ tempGenerator.addMapping({
44
+ generated: {
45
+ line: mapping.generatedLine,
46
+ column: mapping.generatedColumn
47
+ },
48
+ original: {
49
+ line: mapping.originalLine,
50
+ column: mapping.originalColumn
51
+ },
52
+ source: mapping.source,
53
+ name: mapping.name
54
+ });
55
+ });
56
+ callback(null, source, JSON.parse(tempGenerator.toString()));
57
+ });
58
+ }
59
+ exports.default = addColSourceMap;
@@ -0,0 +1,3 @@
1
+ import { LoaderContext } from 'webpack';
2
+ declare function extractMapData(this: LoaderContext<any>, source: string, map: any): void;
3
+ export default extractMapData;
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const path_1 = __importDefault(require("path"));
8
- function splitMap(source, map) {
8
+ function extractMapData(source, map) {
9
9
  const enterFilePath = this.resourcePath;
10
10
  // 获取当前文件夹和文件名
11
11
  const dirName = path_1.default.dirname(enterFilePath);
@@ -17,4 +17,4 @@ function splitMap(source, map) {
17
17
  }
18
18
  this.callback(null, source, map && JSON.parse(map));
19
19
  }
20
- exports.default = splitMap;
20
+ exports.default = extractMapData;
@@ -1,30 +1,19 @@
1
1
  import { IFileLaneConfig } from 'file-lane';
2
2
  import IJavascriptCompileOption from '../compiler/javascript/interface/IJavascriptCompileOption';
3
- import AppUxLoader from '../loader/ux/AppUxLoader';
4
- import JsLoader from '../loader/ux/JsLoader';
5
- import UxLoader from '../loader/ux/UxLoader';
3
+ import { IRule } from 'file-lane/lib/interface/IFileLaneConfig';
6
4
  declare class UxConfig implements IFileLaneConfig<IJavascriptCompileOption> {
7
5
  readonly projectPath: string;
8
6
  constructor(projectPath: string);
9
- exclude: RegExp[];
7
+ exclude: (RegExp | ((filePath: string) => boolean))[];
10
8
  get output(): string;
11
9
  module: {
12
- rules: ({
13
- test: string[];
14
- loader: (typeof AppUxLoader)[];
15
- exclude?: undefined;
16
- } | {
17
- test: RegExp[];
18
- exclude: RegExp[];
19
- loader: (typeof UxLoader)[];
20
- } | {
21
- test: RegExp[];
22
- loader: (typeof JsLoader)[];
23
- exclude?: undefined;
24
- })[];
10
+ rules: IRule[];
25
11
  };
26
12
  preWorks: import("file-lane/lib/interface/IFileLaneConfig").PreWork<IJavascriptCompileOption>[];
27
- followWorks: import("file-lane/lib/interface/IFileLaneConfig").FollowWork<IJavascriptCompileOption>[];
13
+ followWorks: {
14
+ worker: import("file-lane/lib/interface/IFileLaneConfig").FollowWork<IJavascriptCompileOption>;
15
+ workerDescribe: string;
16
+ }[];
28
17
  watchIgnores: RegExp[];
29
18
  }
30
19
  export default UxConfig;
@@ -9,12 +9,32 @@ const JsLoader_1 = __importDefault(require("../loader/ux/JsLoader"));
9
9
  const PngLoader_1 = __importDefault(require("../loader/ux/PngLoader"));
10
10
  const UxLoader_1 = __importDefault(require("../loader/ux/UxLoader"));
11
11
  const PreWorkUtils_1 = __importDefault(require("../utils/PreWorkUtils"));
12
- const UxFollowWorks_1 = __importDefault(require("../utils/ux/UxFollowWorks"));
13
- const UxPreWorks_1 = __importDefault(require("../utils/ux/UxPreWorks"));
12
+ const UxFollowWorks_1 = __importDefault(require("../followWorks/ux/UxFollowWorks"));
13
+ const UxPreWorks_1 = __importDefault(require("../preWorks/ux/UxPreWorks"));
14
+ const HmlLoader_1 = __importDefault(require("../loader/ux/HmlLoader"));
15
+ const FileUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/FileUtil"));
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const parser_1 = require("@aiot-toolkit/parser");
14
18
  class UxConfig {
15
19
  constructor(projectPath) {
16
20
  this.projectPath = projectPath;
17
- this.exclude = [/node_modules/, /dist/, /build/];
21
+ this.exclude = [
22
+ /node_modules/,
23
+ /dist/,
24
+ /build/,
25
+ (filePath) => {
26
+ // 如果 是 script, style文件,且同路径下存在同名 hml,则忽略
27
+ const { ext, name } = path_1.default.parse(filePath);
28
+ if (![...parser_1.ExtensionConfig.SCRIPTS, ...parser_1.ExtensionConfig.STYLES].includes(ext)) {
29
+ return false;
30
+ }
31
+ const hmlPath = FileUtil_1.default.updatePath(filePath, `${name}${parser_1.ExtensionConfig.HML}`);
32
+ if (fs_1.default.existsSync(hmlPath)) {
33
+ return true;
34
+ }
35
+ return false;
36
+ }
37
+ ];
18
38
  this.module = {
19
39
  rules: [
20
40
  {
@@ -26,6 +46,10 @@ class UxConfig {
26
46
  exclude: [/app\.ux/],
27
47
  loader: [UxLoader_1.default]
28
48
  },
49
+ {
50
+ test: [/.+\.hml$/],
51
+ loader: [HmlLoader_1.default, UxLoader_1.default]
52
+ },
29
53
  {
30
54
  test: [/.+\.js$/],
31
55
  loader: [JsLoader_1.default]
@@ -44,13 +68,34 @@ class UxConfig {
44
68
  PreWorkUtils_1.default.getGlobalVar
45
69
  ];
46
70
  this.followWorks = [
47
- UxFollowWorks_1.default.symlinkNodeModule,
48
- UxFollowWorks_1.default.webpack,
49
- UxFollowWorks_1.default.copyResource,
50
- UxFollowWorks_1.default.jsc,
51
- UxFollowWorks_1.default.toRpk,
52
- UxFollowWorks_1.default.moveBackResult,
53
- UxFollowWorks_1.default.protobuf
71
+ {
72
+ worker: UxFollowWorks_1.default.symlinkNodeModule,
73
+ workerDescribe: 'Create a soft link to the node_modules folder'
74
+ },
75
+ {
76
+ worker: UxFollowWorks_1.default.webpack,
77
+ workerDescribe: 'Compile the project using webpack'
78
+ },
79
+ {
80
+ worker: UxFollowWorks_1.default.copyResource,
81
+ workerDescribe: 'Copy resource files'
82
+ },
83
+ {
84
+ worker: UxFollowWorks_1.default.jsc,
85
+ workerDescribe: 'Generate jsc bytecode'
86
+ },
87
+ {
88
+ worker: UxFollowWorks_1.default.protobuf,
89
+ workerDescribe: 'Generate protobuf json'
90
+ },
91
+ {
92
+ worker: UxFollowWorks_1.default.toRpk,
93
+ workerDescribe: 'Package the project into an RPK file'
94
+ },
95
+ {
96
+ worker: UxFollowWorks_1.default.moveBackResult,
97
+ workerDescribe: 'Migrate temporary project'
98
+ }
54
99
  ];
55
100
  this.watchIgnores = [/node_modules/];
56
101
  }
@@ -1,5 +1,6 @@
1
1
  import { IFileLaneConfig } from 'file-lane';
2
2
  import XtsLoader from '../loader/xts/XtsLoader';
3
+ import { PreWork } from 'file-lane/lib/interface/IFileLaneConfig';
3
4
  /**
4
5
  * XtsConfig
5
6
  */
@@ -12,7 +13,9 @@ declare class XtsConfig implements IFileLaneConfig {
12
13
  loader: (typeof XtsLoader)[];
13
14
  }[];
14
15
  };
15
- preWorks: import("file-lane/lib/interface/IFileLaneConfig").PreWork<import("../interface/ICompileOptions").IXtsCompileOptions>[];
16
- followWorks: import("file-lane/lib/interface/IFileLaneConfig").FollowWork<import("../interface/ICompileOptions").IXtsCompileOptions>[];
16
+ preWorks: PreWork[];
17
+ followWorks: {
18
+ worker: import("file-lane/lib/interface/IFileLaneConfig").FollowWork<import("../interface/ICompileOptions").IXtsCompileOptions>;
19
+ }[];
17
20
  }
18
21
  export default XtsConfig;
@@ -7,7 +7,6 @@ const entryTemplate_1 = require("../followWorks/xts/entryTemplate");
7
7
  const generateRpk_1 = require("../followWorks/xts/generateRpk");
8
8
  const ts2wasm_1 = require("../followWorks/xts/ts2wasm");
9
9
  const XtsLoader_1 = __importDefault(require("../loader/xts/XtsLoader"));
10
- const preInstall_1 = require("../preWorks/xts/preInstall");
11
10
  /**
12
11
  * XtsConfig
13
12
  */
@@ -23,8 +22,18 @@ class XtsConfig {
23
22
  }
24
23
  ]
25
24
  };
26
- this.preWorks = [preInstall_1.preInstall];
27
- this.followWorks = [entryTemplate_1.generateEntryFile, ts2wasm_1.ts2wasm, generateRpk_1.generateRpk];
25
+ this.preWorks = [];
26
+ this.followWorks = [
27
+ {
28
+ worker: entryTemplate_1.generateEntryFile
29
+ },
30
+ {
31
+ worker: ts2wasm_1.ts2wasm
32
+ },
33
+ {
34
+ worker: generateRpk_1.generateRpk
35
+ }
36
+ ];
28
37
  }
29
38
  }
30
39
  exports.default = XtsConfig;
@@ -26,7 +26,7 @@ const JavascriptCompiler_1 = __importDefault(require("../../compiler/javascript/
26
26
  const JavascriptDefaultCompileOption_1 = __importDefault(require("../../compiler/javascript/JavascriptDefaultCompileOption"));
27
27
  const Jsc_1 = __importDefault(require("../../compiler/javascript/vela/utils/Jsc"));
28
28
  const ZipUtil_1 = __importDefault(require("../../compiler/javascript/vela/utils/ZipUtil"));
29
- const UxFileUtils_1 = __importDefault(require("./UxFileUtils"));
29
+ const UxFileUtils_1 = __importDefault(require("../../utils/ux/UxFileUtils"));
30
30
  const BinaryPlugin = require('@aiot-toolkit/parser/lib/ux/translate/vela/protobuf/BinaryPlugin');
31
31
  /**
32
32
  * UxFollowWorks
@@ -78,7 +78,7 @@ UxFollowWorks.protobuf = (context, config, compilerOption) => __awaiter(void 0,
78
78
  }
79
79
  });
80
80
  UxFollowWorks.jsc = (context, config, compilerOption) => __awaiter(void 0, void 0, void 0, function* () {
81
- if (compilerOption && compilerOption.disabledJSC === false) {
81
+ if (compilerOption && compilerOption.disabledJsc === false) {
82
82
  return new Jsc_1.default(context.projectPath, path_1.default.join(context.projectPath, context.output, compilerOption.outputPath)).jsc();
83
83
  }
84
84
  });
@@ -242,12 +242,17 @@ UxFollowWorks.symlinkNodeModule = (context, config, compilerOption) => __awaiter
242
242
  if (!compilerOption) {
243
243
  return;
244
244
  }
245
- const foldList = ['node_modules'];
246
- foldList.forEach((item) => {
247
- const sourcePath = path_1.default.join(context.projectPath, item);
248
- if (fs_extra_1.default.existsSync(sourcePath)) {
249
- fs_extra_1.default.symlinkSync(sourcePath, path_1.default.join(compilerOption.projectPath, item), 'junction');
250
- }
251
- });
245
+ try {
246
+ const foldList = ['node_modules'];
247
+ foldList.forEach((item) => {
248
+ const sourcePath = path_1.default.join(context.projectPath, item);
249
+ if (fs_extra_1.default.existsSync(sourcePath)) {
250
+ fs_extra_1.default.symlinkSync(sourcePath, path_1.default.join(compilerOption.projectPath, item), 'junction');
251
+ }
252
+ });
253
+ }
254
+ catch (error) {
255
+ throw new Error(`${(error === null || error === void 0 ? void 0 : error.toString()) || 'unknown error'}. Please check whether the file system of the current disk supports the creation of soft links.`);
256
+ }
252
257
  });
253
258
  exports.default = UxFollowWorks;
@@ -13,6 +13,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const UxLoaderUtils_1 = __importDefault(require("../../utils/ux/UxLoaderUtils"));
16
+ const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const shared_utils_1 = require("@aiot-toolkit/shared-utils");
16
19
  /**
17
20
  * 处理app.ux的Loader
18
21
  * AppUxLoader
@@ -20,8 +23,17 @@ const UxLoaderUtils_1 = __importDefault(require("../../utils/ux/UxLoaderUtils"))
20
23
  class AppUxLoader {
21
24
  parser(files) {
22
25
  return __awaiter(this, void 0, void 0, function* () {
23
- const resultFiles = [];
24
- yield UxLoaderUtils_1.default.compileAppUxToJavascript(files[0], resultFiles, this.context, this.compilerOption);
26
+ const { projectPath } = this.context;
27
+ const { files: resultFiles, logs } = yield UxLoaderUtils_1.default.compileAppUxToJavascript(files[0], this.context, this.compilerOption);
28
+ if (logs) {
29
+ logs.forEach((item) => {
30
+ ColorConsole_1.default.logger(Object.assign(Object.assign({}, item), { filePath: item.filePath ? path_1.default.relative(projectPath, item.filePath) : '' }));
31
+ });
32
+ // 如果有 error throw,则停止
33
+ if (logs.some((item) => item.level && [shared_utils_1.LOG_LEVEL.Throw].includes(item.level))) {
34
+ throw new Error('stop!');
35
+ }
36
+ }
25
37
  return resultFiles;
26
38
  });
27
39
  }
@@ -0,0 +1,24 @@
1
+ import { IFileLaneContext, IFileParam, ILoader } from 'file-lane';
2
+ /**
3
+ * HmlLoader
4
+ */
5
+ declare class HmlLoader implements ILoader {
6
+ context?: IFileLaneContext | undefined;
7
+ compilerOption?: any;
8
+ parser(files: IFileParam<any>[]): IFileParam<any>[] | Promise<IFileParam<any>[]>;
9
+ /**
10
+ * 包裹 hml 文件
11
+ *
12
+ * # 路径
13
+ * 如果存在同路径的 ux 后缀,报错;否则转换为同路径的 ux 后缀
14
+ *
15
+ * # 内容
16
+ * 1. 给hml的内容加上<template></template>
17
+ * 2. 如果存在同路径同名的 script文件,则加到<script></script>中
18
+ * 3. 如果存在同路径同名的 style文件,则加到<style></style>中
19
+ *
20
+ * @param file
21
+ */
22
+ private wrapHml;
23
+ }
24
+ export default HmlLoader;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const parser_1 = require("@aiot-toolkit/parser");
9
+ /**
10
+ * HmlLoader
11
+ */
12
+ class HmlLoader {
13
+ parser(files) {
14
+ return files.map((file) => {
15
+ return this.wrapHml(file);
16
+ });
17
+ }
18
+ /**
19
+ * 包裹 hml 文件
20
+ *
21
+ * # 路径
22
+ * 如果存在同路径的 ux 后缀,报错;否则转换为同路径的 ux 后缀
23
+ *
24
+ * # 内容
25
+ * 1. 给hml的内容加上<template></template>
26
+ * 2. 如果存在同路径同名的 script文件,则加到<script></script>中
27
+ * 3. 如果存在同路径同名的 style文件,则加到<style></style>中
28
+ *
29
+ * @param file
30
+ */
31
+ wrapHml(file) {
32
+ const { path, content } = file;
33
+ const uxExt = parser_1.ExtensionConfig.UX;
34
+ const getFilePath = (ext) => {
35
+ const pathParsed = path_1.default.parse(path);
36
+ pathParsed.ext = ext;
37
+ pathParsed.base = pathParsed.name + ext;
38
+ return path_1.default.format(pathParsed);
39
+ };
40
+ const uxPath = getFilePath(uxExt);
41
+ if (fs_1.default.existsSync(uxPath)) {
42
+ throw new Error(`${uxPath} already exists`);
43
+ }
44
+ const scriptExts = parser_1.ExtensionConfig.SCRIPTS;
45
+ const styleExts = parser_1.ExtensionConfig.STYLES;
46
+ const scriptPath = scriptExts
47
+ .map((item) => getFilePath(item))
48
+ .find((item) => fs_1.default.existsSync(item));
49
+ const stylePath = styleExts.map((item) => getFilePath(item)).find((item) => fs_1.default.existsSync(item));
50
+ let uxContent = ['<template>', content, '</template>'].join('\n');
51
+ if (scriptPath) {
52
+ uxContent += ['<script>', fs_1.default.readFileSync(scriptPath, 'utf-8'), '</script>'].join('\n');
53
+ }
54
+ if (stylePath) {
55
+ uxContent += ['<style>', fs_1.default.readFileSync(stylePath, 'utf-8'), '</style>'].join('\n');
56
+ }
57
+ return {
58
+ path,
59
+ content: uxContent
60
+ };
61
+ }
62
+ }
63
+ exports.default = HmlLoader;
@@ -1,9 +1,11 @@
1
1
  import { IFileLaneContext, IFileParam, ILoader } from 'file-lane';
2
+ import IJavascriptCompileOption from '../../compiler/javascript/interface/IJavascriptCompileOption';
2
3
  /**
3
4
  * JsLoader
4
5
  */
5
6
  declare class JsLoader implements ILoader {
6
7
  context: IFileLaneContext;
8
+ compilerOption: IJavascriptCompileOption;
7
9
  parser(files: IFileParam<any>[]): IFileParam<any>[] | Promise<IFileParam<any>[]>;
8
10
  }
9
11
  export default JsLoader;
@@ -23,11 +23,12 @@ class JsLoader {
23
23
  const options = {
24
24
  filePath: item.path,
25
25
  projectPath: this.context.projectPath,
26
+ content: item.content.toString(),
26
27
  onLog
27
28
  };
28
29
  return {
29
30
  path: item.path,
30
- content: new ScriptToTypescript_1.default(project, options)
31
+ content: new ScriptToTypescript_1.default(project, options, this.compilerOption)
31
32
  .translate({
32
33
  content: new ScriptParser_1.default(options).parser(item.content.toString()).ast.content
33
34
  }, [])
@@ -23,7 +23,7 @@ class UxLoader {
23
23
  const { projectPath } = this.context;
24
24
  for (const file of files) {
25
25
  // 转换每个文件
26
- const { files, logs } = yield UxLoaderUtils_1.default.compileUxToJavascript(file, this.context, false, this.compilerOption);
26
+ const { files: compiledFiles, logs } = yield UxLoaderUtils_1.default.compileUxToJavascript(file, this.context, false, this.compilerOption);
27
27
  if (logs) {
28
28
  logs.forEach((item) => {
29
29
  ColorConsole_1.default.logger(Object.assign(Object.assign({}, item), { filePath: item.filePath ? path_1.default.relative(projectPath, item.filePath) : '' }));
@@ -33,7 +33,7 @@ class UxLoader {
33
33
  throw new Error('stop!');
34
34
  }
35
35
  }
36
- resultFiles.push(...files);
36
+ resultFiles.push(...compiledFiles);
37
37
  }
38
38
  return resultFiles;
39
39
  });
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  var _a;
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- const UxFileUtils_1 = __importDefault(require("./UxFileUtils"));
16
+ const UxFileUtils_1 = __importDefault(require("../../utils/ux/UxFileUtils"));
17
17
  /**
18
18
  * UxPreWorks
19
19
  */
@@ -19,6 +19,7 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
19
19
  const path_1 = __importDefault(require("path"));
20
20
  const TranslateCache_1 = __importDefault(require("../../../parser/lib/ux/translate/vela/TranslateCache"));
21
21
  const UxFileUtils_1 = __importDefault(require("./ux/UxFileUtils"));
22
+ const parser_1 = require("@aiot-toolkit/parser");
22
23
  const BinaryPlugin = require('@aiot-toolkit/parser/lib/ux/translate/vela/protobuf/BinaryPlugin');
23
24
  /**
24
25
  * PreWorkUtils
@@ -51,13 +52,14 @@ class PreWorkUtils {
51
52
  Object.keys(pages).map((page) => {
52
53
  const pageContent = pages[page];
53
54
  const entryDir = path_1.default.join(srcPath, page);
54
- const entryPage = path_1.default.join(entryDir, `${pageContent.component}.ux`);
55
- if (fs_extra_1.default.existsSync(entryPage)) {
56
- entryList.push(entryPage);
55
+ const entryPages = parser_1.ExtensionConfig.TEMPLATES.map((item) => `${pageContent.component}${item}`);
56
+ const entry = entryPages.find((item) => fs_extra_1.default.existsSync(path_1.default.join(entryDir, item)));
57
+ if (entry) {
58
+ entryList.push(path_1.default.join(entryDir, entry));
57
59
  }
58
60
  else {
59
61
  // 路径不存在
60
- ColorConsole_1.default.throw(`### manifest ### path '${entryPage}' does not exist`);
62
+ ColorConsole_1.default.throw(`### manifest ### path '${entryPages.join(' | ')}' does not exist`);
61
63
  }
62
64
  });
63
65
  context['entries'] = entryList;
@@ -99,6 +101,7 @@ PreWorkUtils.clean = (context, _, config, compileOption) => __awaiter(void 0, vo
99
101
  BinaryPlugin.reset();
100
102
  BinaryPlugin.config = {
101
103
  projectPath: context.projectPath,
104
+ outputProjectPath: path_1.default.join(context.projectPath, context.output),
102
105
  source: compileOption === null || compileOption === void 0 ? void 0 : compileOption.sourceRoot,
103
106
  output: compileOption === null || compileOption === void 0 ? void 0 : compileOption.outputPath
104
107
  };
@@ -24,7 +24,10 @@ declare class UxLoaderUtils {
24
24
  * @param resultFiles
25
25
  * @param context
26
26
  */
27
- static compileAppUxToJavascript(file: IFileParam, resultFiles: IFileParam[], context: IFileLaneContext, compileOption: IJavascriptCompileOption): Promise<void>;
27
+ static compileAppUxToJavascript(file: IFileParam, context: IFileLaneContext, compileOption: IJavascriptCompileOption): Promise<{
28
+ files: IFileParam<any>[];
29
+ logs: ILog[];
30
+ }>;
28
31
  /**
29
32
  * 判断是否为src/app.ux
30
33
  * @param filePath
@@ -40,10 +40,11 @@ const UxParser_1 = __importDefault(require("@aiot-toolkit/parser/lib/ux/parser/U
40
40
  const UxToTypescript_1 = __importDefault(require("@aiot-toolkit/parser/lib/ux/translate/vela/UxToTypescript"));
41
41
  const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
42
42
  const FileUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/FileUtil"));
43
- const StringUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/StringUtil"));
44
- const parse5 = __importStar(require("parse5"));
43
+ const parse5 = __importStar(require("aiot-parse5"));
45
44
  const path_1 = __importDefault(require("path"));
45
+ const fs_extra_1 = __importDefault(require("fs-extra"));
46
46
  const ts_morph_1 = require("ts-morph");
47
+ const CommonUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/CommonUtil"));
47
48
  const BinaryPlugin = require('@aiot-toolkit/parser/lib/ux/translate/vela/protobuf/BinaryPlugin');
48
49
  const { extractFunctions } = require('@aiot-toolkit/parser/lib/ux/translate/vela/protobuf/protobufControl');
49
50
  /**
@@ -74,38 +75,56 @@ class UxLoaderUtils {
74
75
  logs
75
76
  };
76
77
  }
78
+ // 区分页面组件和子组件
79
+ const isPageUx = UxLoaderUtils.isPageUx(context, filePath);
77
80
  // 配置转换参数
78
81
  const options = {
79
82
  projectPath: context.projectPath,
80
83
  filePath,
84
+ fileType: isAppUx ? 'app' : isPageUx ? 'page' : '',
85
+ content: content.toString(),
81
86
  onLog: (log) => {
82
87
  logs.push(log);
83
88
  }
84
89
  };
90
+ // 收集图片资源
91
+ const ImageResources = [];
92
+ const collectImageResource = (originFilePath) => {
93
+ const assetDir = 'dynamicAssets';
94
+ const sourcePath = path_1.default.resolve(options.projectPath, compilerOption.sourceRoot);
95
+ const assetPath = path_1.default.resolve(sourcePath, assetDir);
96
+ const { name, ext } = path_1.default.parse(originFilePath);
97
+ const hashName = CommonUtil_1.default.calcImageDigest(originFilePath);
98
+ const newFilePath = path_1.default.join(assetPath, `${name}-${hashName}${ext}`);
99
+ const relativePath = path_1.default.posix.sep + path_1.default.relative(sourcePath, newFilePath).split(path_1.default.sep).join(path_1.default.posix.sep);
100
+ ImageResources.push({
101
+ path: newFilePath,
102
+ content: fs_extra_1.default.readFileSync(originFilePath)
103
+ });
104
+ return relativePath;
105
+ };
85
106
  // 开始转换
86
107
  const globalVar = ('globalVar' in context && context.globalVar) || {};
87
- const parserResult = yield new UxParser_1.default(options, globalVar).parser(content.toString(), fullName);
108
+ const parserResult = yield new UxParser_1.default(options, compilerOption, globalVar, collectImageResource).parser();
88
109
  // 区分app.ux和一般ux
89
110
  // app.ux解析结果中加上manifest.json的内容
90
111
  const manifestJson = `require('./manifest.json')`;
91
- // 区分页面组件和子组件
92
- const isPageUx = UxLoaderUtils.isPageUx(context, filePath);
93
112
  const integrateFunction = (appImport, appStyleTree, appTemplateTree, appScriptTree) => {
94
113
  // script代码放在第三个位置不能变化,影响更新source map
95
114
  return isAppUx
96
115
  ? [
97
116
  `${appImport.join('\n')}`,
98
- `var $app_style$ = ${UxLoaderUtils.wrapStyle(StringUtil_1.default.arrayTostring(appStyleTree, true), file, compilerOption)}`,
99
- `var $app_script$ = ${UxLoaderUtils.handleScriptContent(false, appScriptTree)}\n`,
100
- `$app_script$({}, $app_exports$, $app_require$);\n\n`,
117
+ `var $app_style$ = ${UxLoaderUtils.wrapStyle(JSON.stringify(appStyleTree), file, compilerOption)}`,
118
+ `var $app_script$ = ${UxLoaderUtils.handleScriptContent(false, appScriptTree)}`,
119
+ `$app_script$({}, $app_exports$, $app_require$);`,
101
120
  `$app_exports$.default.style = $app_style$;`,
102
121
  `$app_exports$.default.manifest = ${manifestJson}`
103
122
  ]
104
123
  : [
105
124
  `${appImport.join('\n')}`,
106
- `var $app_style$ = ${UxLoaderUtils.wrapStyle(StringUtil_1.default.arrayTostring(appStyleTree, true), file, compilerOption)}\n`,
125
+ `var $app_style$ = ${UxLoaderUtils.wrapStyle(JSON.stringify(appStyleTree), file, compilerOption)}`,
107
126
  `var $app_script$ = ${UxLoaderUtils.handleScriptContent(isPageUx, appScriptTree)}`,
108
- `var $app_template$ = ${UxLoaderUtils.wrapTempalte(appTemplateTree, file, compilerOption)}\n`,
127
+ `var $app_template$ = ${UxLoaderUtils.wrapTempalte(appTemplateTree, file, compilerOption)}`,
109
128
  `${UxLoaderUtils.getReturnType(isPageUx)} function ($app_exports$) {`,
110
129
  `$app_script$({}, $app_exports$, $app_require$);`,
111
130
  `$app_exports$.default.template = $app_template$;`,
@@ -124,13 +143,14 @@ class UxLoaderUtils {
124
143
  return {
125
144
  files: [
126
145
  {
127
- path: FileUtil_1.default.updatePath(filePath, newFileName),
146
+ path: filePath,
128
147
  content: code
129
148
  },
130
149
  {
131
- path: FileUtil_1.default.updatePath(filePath, `${name}.js.map`),
150
+ path: FileUtil_1.default.updatePath(filePath, `${name}${ext}.map`),
132
151
  content: sourceMap.toString()
133
- }
152
+ },
153
+ ...ImageResources
134
154
  ],
135
155
  logs
136
156
  };
@@ -142,7 +162,7 @@ class UxLoaderUtils {
142
162
  * @param resultFiles
143
163
  * @param context
144
164
  */
145
- static compileAppUxToJavascript(file, resultFiles, context, compileOption) {
165
+ static compileAppUxToJavascript(file, context, compileOption) {
146
166
  return __awaiter(this, void 0, void 0, function* () {
147
167
  const { path: filePath, content } = file;
148
168
  let appContent = content;
@@ -153,7 +173,7 @@ class UxLoaderUtils {
153
173
  }
154
174
  // 解析script和style两部分
155
175
  const compileResult = yield UxLoaderUtils.compileUxToJavascript({ path: filePath, content: appContent }, context, isAppUx, compileOption);
156
- resultFiles.push(...compileResult.files);
176
+ return compileResult;
157
177
  });
158
178
  }
159
179
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/aiotpack",
3
- "version": "2.0.2-beta.1",
3
+ "version": "2.0.2-beta.10",
4
4
  "description": "The process tool for packaging aiot projects.",
5
5
  "keywords": [
6
6
  "aiotpack"
@@ -19,24 +19,27 @@
19
19
  "test": "node ./__tests__/aiotpack.test.js"
20
20
  },
21
21
  "dependencies": {
22
- "@aiot-toolkit/generator": "2.0.2-beta.1",
23
- "@aiot-toolkit/parser": "2.0.2-beta.1",
22
+ "@aiot-toolkit/generator": "2.0.2-beta.10",
23
+ "@aiot-toolkit/parser": "2.0.2-beta.10",
24
24
  "@hap-toolkit/aaptjs": "^2.0.0",
25
25
  "babel-loader": "^9.1.3",
26
26
  "del": "^4.1.0",
27
27
  "fast-glob": "^3.3.2",
28
- "file-lane": "2.0.2-beta.1",
28
+ "file-lane": "2.0.2-beta.10",
29
29
  "file-loader": "^6.2.0",
30
30
  "fs-extra": "^11.2.0",
31
31
  "jsrsasign": "^7.2.2",
32
32
  "jszip": "^3.10.1",
33
+ "source-map": "^0.7.4",
33
34
  "url-loader": "^4.1.1",
34
35
  "webpack": "^5.89.0",
36
+ "webpack-bundle-analyzer": "^4.10.1",
35
37
  "webpack-sources": "^3.2.3"
36
38
  },
37
39
  "devDependencies": {
40
+ "@types/fs-extra": "^11.0.4",
38
41
  "@types/jsrsasign": "^10.5.12",
39
42
  "@types/webpack-sources": "^3.2.3"
40
43
  },
41
- "gitHead": "9244af36e617295ea4ff11a2f4f07f0e4f9f5f59"
44
+ "gitHead": "00d23c1ad1306a6c72bb007685de9fd4a6ee61af"
42
45
  }
@@ -1,3 +0,0 @@
1
- import { LoaderContext } from 'webpack';
2
- declare function splitMap(this: LoaderContext<any>, source: string, map: any): void;
3
- export default splitMap;
File without changes