@aiot-toolkit/parser 2.0.6-beta.17 → 2.0.6-beta.19

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.
@@ -1,4 +1,5 @@
1
1
  import { Dictionary, Logger } from '@aiot-toolkit/shared-utils';
2
+ import { IAttributeNode } from '../interface/IIdentifierNode';
2
3
  interface IElement {
3
4
  /**
4
5
  * 别名
@@ -8,6 +9,19 @@ interface IElement {
8
9
  * @example <img /> 生成为 image
9
10
  */
10
11
  aliasName?: string;
12
+ /**
13
+ * 别名属性
14
+ *
15
+ * aliasName 生效时,会将别名属性合并到属性列表中
16
+ *
17
+ * @example
18
+ * ```
19
+ * // <button /> 生成为 <input type="button" />
20
+ * aliasName='input'
21
+ * aliasAttributes=[{name:'type',value:'button'}]
22
+ * ```
23
+ */
24
+ aliasAttributes?: IAttributeNode[];
11
25
  /**
12
26
  * 允许文字子节点
13
27
  *
@@ -1321,6 +1321,12 @@ const STYLE_ATTRIBUTE_CONFIG = exports.STYLE_ATTRIBUTE_CONFIG = {
1321
1321
  scrollSnapAlign: {
1322
1322
  // csstree-validator可校验
1323
1323
  // 按照一般转换即可,无需额外转换
1324
+ },
1325
+ selectedFontSize: {
1326
+ validate: validateLengthNode
1327
+ },
1328
+ selectedBackgroundColor: {
1329
+ validate: validColorNode
1324
1330
  }
1325
1331
  };
1326
1332
  /**
@@ -217,6 +217,17 @@ const elementConfig = {
217
217
  name: {}
218
218
  }
219
219
  },
220
+ button: {
221
+ aliasName: 'input',
222
+ aliasAttributes: [{
223
+ name: 'type',
224
+ value: 'button'
225
+ }],
226
+ attributes: {
227
+ name: {},
228
+ value: {}
229
+ }
230
+ },
220
231
  input: {
221
232
  events: ['change', 'enterkeyclick', 'selectionchange'],
222
233
  attributes: {
@@ -11,6 +11,7 @@ var _TemplateUtil = _interopRequireDefault(require("../translate/vela/utils/Temp
11
11
  var _UxUtil = _interopRequireDefault(require("../utils/UxUtil"));
12
12
  var _ScriptParser = _interopRequireDefault(require("./ScriptParser"));
13
13
  var _StyleParser = _interopRequireDefault(require("./StyleParser"));
14
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  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); }
16
17
  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; }
@@ -72,14 +73,51 @@ class UxParser {
72
73
  };
73
74
  break;
74
75
  case 'style':
75
- const cssContent = parse5.serialize(childNode);
76
+ // 如果 style 标签有 src 属性,则读取对应的文件内容,否则直接使用 style 标签的内容
77
+ const src = _UxUtil.default.getStyleSrc(childNode, this.options.filePath);
78
+ let cssContent = '';
79
+ const filePath = this.options.filePath;
80
+ if (src) {
81
+ // 如果有 src 属性,且 style 标签内有内容,则提示用户 style 标签内的内容会被 src 文件覆盖
82
+ if (childNode.childNodes && childNode.childNodes.length > 0) {
83
+ this.options.onLog({
84
+ level: _sharedUtils.Loglevel.WARN,
85
+ filePath: this.options.filePath,
86
+ position: {
87
+ startLine: childNode.sourceCodeLocation?.startLine || 0,
88
+ startColumn: 0
89
+ },
90
+ message: [`style tag with src should not have inline content, src file will be used directly`]
91
+ });
92
+ }
93
+ // 读取 src 文件内容,如果文件不存在,则提示用户
94
+ if (!_fsExtra.default.existsSync(src)) {
95
+ this.options.onLog({
96
+ level: _sharedUtils.Loglevel.WARN,
97
+ filePath: this.options.filePath,
98
+ position: {
99
+ startLine: childNode.sourceCodeLocation?.startLine || 0,
100
+ startColumn: 0
101
+ },
102
+ message: [`style src file does not exist: ${src}`]
103
+ });
104
+ } else {
105
+ cssContent = _fsExtra.default.readFileSync(src).toString();
106
+ // 解析 css 时需要用到文件路径,所以将options.filePath暂时替换为src,解析完成后再还原
107
+ this.options.filePath = src;
108
+ }
109
+ } else cssContent = parse5.serialize(childNode);
110
+
76
111
  // 获取lang值,组合成文件名
77
112
  const langType = _UxUtil.default.getLangType(childNode);
78
- const cssFileName = _UxUtil.default.spliceFileName(fileName, langType);
113
+ // 使用 src 时,cssFileName src 的文件名为准,否则以当前解析的文件名为准
114
+ const cssFileName = src ? _path.default.basename(src) : _UxUtil.default.spliceFileName(fileName, langType);
79
115
  // 转换全局变量为符合lang类型的字符串
80
116
  const globalStyle = _UxUtil.default.convertGlobalJson(this.globalVar, langType);
81
117
  // 解析CSS 转为 js格式的内容
82
118
  const cssParserResult = await new _StyleParser.default(this.options, this.compilerOption, this.collectImageResource, childNode.sourceCodeLocation?.startLine || 0).parser(`${cssContent}\n${globalStyle}`, cssFileName);
119
+ // 还原 this.options.filePath
120
+ this.options.filePath = filePath;
83
121
  // 多Style标签(TODO: 应该不支持多style标签)
84
122
  ast.style.styleContent.push(...cssParserResult.ast.styleContent);
85
123
  ast.style.content = parse5.serialize(childNode);
@@ -103,7 +103,7 @@ class ScriptToTypescript {
103
103
  let position = undefined;
104
104
  let message = babelError.message;
105
105
  if (loc) {
106
- const offsetLine = (sourceTree.sourceCodeLocation?.startLine || 0) - 1;
106
+ const offsetLine = (sourceTree.sourceCodeLocation?.startLine || 1) - 1;
107
107
  const startLine = loc.line + offsetLine;
108
108
  const startColumn = loc.column;
109
109
  // babel 错误信息中包含行列信息,需要替换
@@ -367,6 +367,7 @@ class TemplateUtil {
367
367
  }
368
368
  const elementConfig = getElementConfig(tag.name);
369
369
  const effectTag = elementConfig?.aliasName || tag.name;
370
+ const aliasAttributes = elementConfig?.aliasAttributes;
370
371
  const {
371
372
  globalInstance,
372
373
  dataInstance,
@@ -374,7 +375,7 @@ class TemplateUtil {
374
375
  } = this.context;
375
376
  const result = `${globalInstance}.${callerName}("${effectTag}", ${_sharedUtils.StringUtil.objectToString({
376
377
  [dataInstance]: dataInstanceValue,
377
- __opts__: await this.attributesToObject(attributes, identifiers, {
378
+ __opts__: await this.attributesToObject(aliasAttributes ? [...aliasAttributes, ...attributes] : attributes, identifiers, {
378
379
  nameToCamel: true
379
380
  })
380
381
  })}, ${children})`;
@@ -537,7 +537,7 @@ class StyleUtil {
537
537
  if (!(0, _StyleSelectorType.isSupportMediaType)(mediaTypeName, projectType)) {
538
538
  // 报错
539
539
  onLog({
540
- level: _sharedUtils.Loglevel.ERROR,
540
+ level: _sharedUtils.Loglevel.WARN,
541
541
  filePath: filePath,
542
542
  position: node.loc ? styleMapUil.transfromLocToPosition(node.loc) : undefined,
543
543
  message: [`Unsupported media type `, {
@@ -52,5 +52,14 @@ declare class UxUtil {
52
52
  projectPath: string;
53
53
  e2eConfigPath: string;
54
54
  }): IE2eConfig;
55
+ /**
56
+ * 获取 style 标签的 src 属性值
57
+ * 如果 src 是相对路径,则需要拼接路径
58
+ *
59
+ * @param node
60
+ * @param filePath
61
+ * @returns
62
+ */
63
+ static getStyleSrc(node: Element, filePath: string): string;
55
64
  }
56
65
  export default UxUtil;
@@ -132,5 +132,22 @@ class UxUtil {
132
132
  throw new Error(`e2e Configuration file does not exist: ${configFilePath}`);
133
133
  }
134
134
  }
135
+
136
+ /**
137
+ * 获取 style 标签的 src 属性值
138
+ * 如果 src 是相对路径,则需要拼接路径
139
+ *
140
+ * @param node
141
+ * @param filePath
142
+ * @returns
143
+ */
144
+ static getStyleSrc(node, filePath) {
145
+ for (let attr of node.attrs) {
146
+ if (attr.name === 'src') {
147
+ return UxUtil.isAbsoluteResource(attr.value) ? attr.value : _path.default.join(_path.default.dirname(filePath), attr.value);
148
+ }
149
+ }
150
+ return '';
151
+ }
135
152
  }
136
153
  var _default = exports.default = UxUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/parser",
3
- "version": "2.0.6-beta.17",
3
+ "version": "2.0.6-beta.19",
4
4
  "description": "Parse the source code of aiot and convert it to the AST (Abstract Syntax Tree) of the target code.",
5
5
  "keywords": [
6
6
  "aiot",
@@ -20,7 +20,7 @@
20
20
  "test": "node ./__tests__/parser.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/shared-utils": "2.0.6-beta.17",
23
+ "@aiot-toolkit/shared-utils": "2.0.6-beta.19",
24
24
  "@babel/core": "^7.23.6",
25
25
  "@babel/generator": "^7.24.10",
26
26
  "@babel/parser": "^7.24.8",
@@ -31,7 +31,7 @@
31
31
  "css-tree": "npm:aiot-css-tree@^2.3.1",
32
32
  "csstree-validator": "^3.0.0",
33
33
  "eslint": "^8.46.0",
34
- "file-lane": "2.0.6-beta.17",
34
+ "file-lane": "2.0.6-beta.19",
35
35
  "fs-extra": "^11.2.0",
36
36
  "google-protobuf": "^3.21.2",
37
37
  "less": "^4.2.0",
@@ -60,5 +60,5 @@
60
60
  "@types/tinycolor2": "^1.4.6",
61
61
  "babel-plugin-tester": "^11.0.4"
62
62
  },
63
- "gitHead": "d9d60157e951ebbc58f6a6f6fac63efd9d9d4fd5"
63
+ "gitHead": "29d48c29e9b35e01a722868daba622fd74baefc9"
64
64
  }