@aiot-toolkit/parser 2.0.6-beta.2 → 2.0.6-beta.3

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.
@@ -3,7 +3,7 @@ import IOptions from '../config/IOptions';
3
3
  import { IStyleAst } from '../interface/IStyleAst';
4
4
  import ITranslateOption from '../interface/ITranslateOption';
5
5
  import { IImageFunction } from '../interface/IImageResource';
6
- import { RawSourceMap, SourceMapConsumer } from 'source-map';
6
+ import { SourceMapConsumer } from 'source-map';
7
7
  /**
8
8
  * StyleParser
9
9
  * 1. 根据语言类型分别转为CSS样式
@@ -15,9 +15,10 @@ declare class StyleParser implements IParser<IStyleAst> {
15
15
  readonly compilerOption: ITranslateOption;
16
16
  collectImageResource: IImageFunction;
17
17
  readonly styleLineOffset: number;
18
- nodeMap: RawSourceMap | string;
18
+ nodeMap: string;
19
19
  sourceMapConsumer: SourceMapConsumer;
20
20
  private styleMapUtil;
21
+ private isCss;
21
22
  readonly QUICKAPP_CONFIG = "quickapp.config.js";
22
23
  constructor(options: IOptions, compilerOption: ITranslateOption, collectImageResource: IImageFunction, styleLineOffset: number);
23
24
  parser(content: string, fileName: string): Promise<ParserResult<IStyleAst>>;
@@ -32,19 +32,21 @@ const {
32
32
  * 3. 把解析结果转换为目标格式
33
33
  */
34
34
  class StyleParser {
35
+ // 是否是css样式代码
36
+ isCss = true;
35
37
  QUICKAPP_CONFIG = 'quickapp.config.js';
36
38
  constructor(options, compilerOption, collectImageResource, styleLineOffset) {
37
39
  this.options = options;
38
40
  this.compilerOption = compilerOption;
39
41
  this.collectImageResource = collectImageResource;
40
42
  this.styleLineOffset = styleLineOffset;
41
- this.nodeMap = {
43
+ this.nodeMap = JSON.stringify({
42
44
  version: 3,
43
45
  sources: [],
44
46
  names: [],
45
47
  mappings: '',
46
48
  file: ''
47
- };
49
+ });
48
50
  }
49
51
  async parser(content, fileName) {
50
52
  const {
@@ -147,7 +149,8 @@ class StyleParser {
147
149
  relativeUrls: true,
148
150
  sourceMap: {
149
151
  sourceMapURL: `${baseName}.map`,
150
- outputSourceFiles: true
152
+ outputSourceFiles: true,
153
+ sourceMapRootpath: 'file:///'
151
154
  }
152
155
  }, (error, output) => {
153
156
  if (error) {
@@ -161,6 +164,7 @@ class StyleParser {
161
164
  CSSCode = output?.css ? output.css : '';
162
165
  if (output?.map) {
163
166
  this.nodeMap = output.map;
167
+ this.isCss && (this.isCss = false);
164
168
  }
165
169
  resolve(CSSCode);
166
170
  }
@@ -177,7 +181,7 @@ class StyleParser {
177
181
  onLog,
178
182
  filePath
179
183
  } = this.options;
180
- const url = `file://${filePath}`;
184
+ const url = `file:///${filePath}`;
181
185
  const alias = this.getAlias();
182
186
 
183
187
  // sass 的 alias 处理依赖当前文件路径
@@ -196,6 +200,7 @@ class StyleParser {
196
200
  });
197
201
  if (Result.sourceMap) {
198
202
  this.nodeMap = JSON.stringify(Result.sourceMap);
203
+ this.isCss && (this.isCss = false);
199
204
  }
200
205
  return Result.css;
201
206
  } catch (error) {
@@ -217,23 +222,29 @@ class StyleParser {
217
222
  onLog,
218
223
  filePath
219
224
  } = this.options;
225
+ const newFilePath = this.isCss ? filePath : `file:///${filePath}`;
220
226
  const alias = this.getAlias();
221
227
  try {
222
228
  const result = await (0, _postcss.default)([(0, _postcssImport.default)({
223
- resolve: id => {
229
+ resolve: (id, basedir) => {
230
+ // 处理所有路径中的 file:/// 前缀
231
+ const cleanId = id.replace(/^file:\/\/\//, '');
232
+ const cleanBase = basedir.replace(/^file:\/\/\//, '');
233
+ const filePath = _path.default.resolve(cleanBase, cleanId);
224
234
  for (const [key, value] of Object.entries(alias)) {
225
- if (id.startsWith(key)) {
226
- return id.replace(key, value);
235
+ if (filePath.startsWith(key)) {
236
+ return filePath.replace(key, value);
227
237
  }
228
238
  }
229
- return id;
239
+ return filePath;
230
240
  }
231
241
  }), _postcssUrl.default]).process(sourceContent, {
232
- from: filePath,
242
+ from: newFilePath,
233
243
  map: {
234
244
  prev: this.nodeMap,
235
245
  inline: false,
236
- annotation: true
246
+ sourcesContent: true,
247
+ absolute: this.isCss
237
248
  }
238
249
  });
239
250
  this.nodeMap = result.map.toString();
@@ -307,7 +318,7 @@ class StyleParser {
307
318
  type: '',
308
319
  prelude: [],
309
320
  block: [],
310
- position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc) : undefined
321
+ position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc, false) : undefined
311
322
  };
312
323
  const {
313
324
  type,
@@ -330,7 +341,7 @@ class StyleParser {
330
341
  name: '',
331
342
  prelude: [],
332
343
  rules: [],
333
- position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc) : undefined
344
+ position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc, false) : undefined
334
345
  };
335
346
  const {
336
347
  type,
@@ -376,7 +387,7 @@ class StyleParser {
376
387
  type: '',
377
388
  name: '',
378
389
  block: [],
379
- position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc) : undefined
390
+ position: sourceNode.loc ? this.styleMapUtil.transfromLocToPosition(sourceNode.loc, false) : undefined
380
391
  };
381
392
  const {
382
393
  type,
@@ -494,7 +505,7 @@ class StyleParser {
494
505
  let value = '';
495
506
  nodeValue.children.map(v => {
496
507
  let tempTargetValue;
497
- const tempPosition = v.loc ? this.styleMapUtil.transfromLocToPosition(v.loc) : undefined;
508
+ const tempPosition = v.loc ? this.styleMapUtil.transfromLocToPosition(v.loc, false) : undefined;
498
509
  switch (v.type) {
499
510
  case 'Operator':
500
511
  value = _cssTree.default.generate(v);
@@ -578,7 +589,7 @@ class StyleParser {
578
589
  targetList.push({
579
590
  name: nodeName,
580
591
  value: valueList,
581
- position: node.loc ? this.styleMapUtil.transfromLocToPosition(node.loc) : undefined
592
+ position: node.loc ? this.styleMapUtil.transfromLocToPosition(node.loc, false) : undefined
582
593
  });
583
594
  }
584
595
  return targetList;
@@ -94,8 +94,14 @@ class StyleToTypescript {
94
94
  const selector = node[i];
95
95
  const {
96
96
  type,
97
- name
97
+ name: orgName
98
98
  } = selector;
99
+
100
+ // 去除名称中的转义符,例如 源码是 .w-20\% {}, 希望生成 w-20%
101
+ // 注意:此处的反转义不能用 JSON.parse
102
+ // 原因:js中 % 不需要转义,但css选择器中%需要转义,两者规则不一样,所以不能使用 JSON.parse
103
+ // 示例:js可以写'abc%', 但css选择器中必须写'abc\%'
104
+ const name = orgName.replace(/\\(.{1})/g, '$1');
99
105
  // 选择器类型转为数字
100
106
  const selectorIndex = (0, _StyleSelectorType.findSelectorIndex)(type);
101
107
  if (type === _StyleSelectorType.Selector.COMBINATOR) {
@@ -29,7 +29,7 @@ const ATTRIBUTE_CONFIG = {
29
29
  return $classValue$;
30
30
  }`;
31
31
  }
32
- return `[${JSON.parse(result).split(' ').map(item => `"${item.trim()}"`).filter(Boolean).join(',')}]`;
32
+ return `[${JSON.parse(result).split(' ').map(item => item.trim()).filter(Boolean).map(item => JSON.stringify(item)).join(',')}]`;
33
33
  }
34
34
  },
35
35
  style: {
@@ -18,11 +18,10 @@ declare class StyleMapUtil {
18
18
  * 3.4 原始位置的endColumn根据map结果,发现存在1个位置的差异,所以-1
19
19
  * 3.5 source存储来源的文件
20
20
  * @param location 生成结果中节点的位置信息
21
- * @param consumer 经过解析器获取的map信息生成的consumer对象
22
- * @param offset 当前样式代码在ux文件中的偏移量
21
+ * @param useAbsolutePath 是否生成绝对路径
23
22
  * @returns
24
23
  */
25
- transfromLocToPosition(location: CssLocation): {
24
+ transfromLocToPosition(location: CssLocation, useAbsolutePath?: boolean): {
26
25
  pos: number;
27
26
  end: number;
28
27
  startLine: number;
@@ -25,11 +25,11 @@ class StyleMapUtil {
25
25
  * 3.4 原始位置的endColumn根据map结果,发现存在1个位置的差异,所以-1
26
26
  * 3.5 source存储来源的文件
27
27
  * @param location 生成结果中节点的位置信息
28
- * @param consumer 经过解析器获取的map信息生成的consumer对象
29
- * @param offset 当前样式代码在ux文件中的偏移量
28
+ * @param useAbsolutePath 是否生成绝对路径
30
29
  * @returns
31
30
  */
32
31
  transfromLocToPosition(location) {
32
+ let useAbsolutePath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
33
33
  const {
34
34
  projectPath
35
35
  } = this.options;
@@ -41,7 +41,7 @@ class StyleMapUtil {
41
41
  line: location.end.line,
42
42
  column: location.end.column
43
43
  });
44
- let nodeRelativePath = '';
44
+ let nodePath = '';
45
45
  if (nodeStartPosition.source?.endsWith('.ux') && nodeStartPosition.line) {
46
46
  nodeStartPosition.line = nodeStartPosition.line - 1 + this.lineOffset;
47
47
  }
@@ -54,7 +54,8 @@ class StyleMapUtil {
54
54
  if (decodeStr.startsWith('file://')) {
55
55
  decodeStr = (0, _url.fileURLToPath)(decodeStr);
56
56
  }
57
- nodeRelativePath = _path.default.relative(projectPath, decodeStr).replaceAll(_path.default.sep, _path.default.posix.sep);
57
+ // useAbsolutePath为false,返回相对路径
58
+ nodePath = (useAbsolutePath ? decodeStr : _path.default.relative(projectPath, decodeStr)).replaceAll(_path.default.sep, _path.default.posix.sep);
58
59
  }
59
60
  return {
60
61
  pos: 0,
@@ -63,7 +64,7 @@ class StyleMapUtil {
63
64
  startColumn: nodeStartPosition.column || 0,
64
65
  endLine: nodeEndPosition.line || 0,
65
66
  endColumn: nodeEndPosition.column || 0,
66
- source: nodeRelativePath
67
+ source: nodePath
67
68
  };
68
69
  }
69
70
  }
@@ -351,7 +351,6 @@ class StyleUtil {
351
351
  endOffset: (error.offset || 0) + propertyLength,
352
352
  source: css
353
353
  });
354
- continue;
355
354
  }
356
355
  }
357
356
  return syntaxErrors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/parser",
3
- "version": "2.0.6-beta.2",
3
+ "version": "2.0.6-beta.3",
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.2",
23
+ "@aiot-toolkit/shared-utils": "2.0.6-beta.3",
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.2",
34
+ "file-lane": "2.0.6-beta.3",
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": "d0057f9e62ac191d21445787667217cfa4aff1e9"
63
+ "gitHead": "2e2ba4b1d293218736343237d963fe93c5fb27a8"
64
64
  }