@we-debug/gulp-tool 0.17.0 → 0.18.1

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.18.1](https://github.com/dlhandsome/we-debug/compare/v0.18.0...v0.18.1) (2023-10-17)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * 解决we-debug gulp tool在某些情况下无法识别app.js的问题 ([767fb71](https://github.com/dlhandsome/we-debug/commit/767fb7162d267553ea9d5321f155062948cdaaf0))
12
+
13
+
14
+
15
+
16
+
17
+ # [0.18.0](https://github.com/dlhandsome/we-debug/compare/v0.17.3...v0.18.0) (2023-08-18)
18
+
19
+
20
+ ### Features
21
+
22
+ * gulp-tool支持通过配置给未设置id的组件设置id ([13d5415](https://github.com/dlhandsome/we-debug/commit/13d54155f9c95665c13e82b8bbe10155297a5edb))
23
+
24
+
25
+
26
+
27
+
6
28
  # [0.17.0](https://github.com/dlhandsome/we-debug/compare/v0.16.1...v0.17.0) (2021-08-02)
7
29
 
8
30
  **Note:** Version bump only for package @we-debug/gulp-tool
package/index.js CHANGED
@@ -5,13 +5,14 @@ const through = require('through2');
5
5
  const PluginError = require('plugin-error');
6
6
  const { lookupPages } = require('./lib/lookup');
7
7
  const { belongsPage, belongsApp } = require('./lib/belongs');
8
- const { getAbsolutePath } = require('./lib/utils');
8
+ const { getAbsolutePath } = require('./lib/path');
9
+ const injectComponentId = require('./lib/inject-component-id');
9
10
 
10
11
  const PLUGIN_NAME = '@we-debug/gulp-tool';
11
12
 
12
13
  let pages = [];
13
14
 
14
- module.exports = function mpGlobalComp(options = {}) {
15
+ function mpGlobalComp(options = {}) {
15
16
  //
16
17
  let baseDir = options.baseDir || 'src';
17
18
  const wxmlRaw = options.wxml || '<we-debug />';
@@ -142,7 +143,7 @@ module.exports = function mpGlobalComp(options = {}) {
142
143
  if (file.extname !== '.js') return callback(null, file);
143
144
 
144
145
  // 如果是 app.js
145
- if (belongsApp(file.path, baseDir)) {
146
+ if (belongsApp(file.path, baseDir) || file.relative === 'app.js') {
146
147
  let code = file.contents.toString();
147
148
 
148
149
  code = `require('${entryFile}')\n` + code;
@@ -156,4 +157,8 @@ module.exports = function mpGlobalComp(options = {}) {
156
157
  const pipeline = [init(), parseWxml(), parseJson(), parseScript()];
157
158
 
158
159
  return lead(pumpify.obj(pipeline));
159
- };
160
+ }
161
+
162
+ mpGlobalComp.injectComponentId = injectComponentId;
163
+
164
+ module.exports = mpGlobalComp;
@@ -0,0 +1,61 @@
1
+ const setAttributeToAST = (ast, attrName, attrValue) => {
2
+ if (!ast || typeof attrName !== 'string') {
3
+ return;
4
+ }
5
+
6
+ // 如果属性存在,则直接修改属性
7
+ let isModified = false;
8
+ const attrs = ast.attr('content.attributes') || [];
9
+ for (let i = 0; i < attrs.length; i++) {
10
+ const attr = attrs[i];
11
+ if (attr.key.content === attrName) {
12
+ attr.value.content = attrValue;
13
+ isModified = true;
14
+ break;
15
+ }
16
+ }
17
+
18
+ // 如果属性不存在,则新增属性
19
+ if (!isModified) {
20
+ attrs.push({
21
+ key: {
22
+ type: 'token:attribute-key',
23
+ content: attrName
24
+ },
25
+ startWrapper: {
26
+ type: 'token:attribute-value-wrapper-start',
27
+ content: '"'
28
+ },
29
+ value: {
30
+ type: 'token:attribute-value',
31
+ content: attrValue
32
+ },
33
+ endWrapper: {
34
+ type: 'token:attribute-value-wrapper-end',
35
+ content: '"'
36
+ }
37
+ });
38
+ }
39
+ ast.attr('content.attributes', attrs);
40
+ };
41
+
42
+ const getAttributeValueWithAST = (ast, attrName) => {
43
+ if (!ast || typeof attrName !== 'string') {
44
+ return;
45
+ }
46
+
47
+ const attrs = ast.attr('content.attributes') || [];
48
+ for (let i = 0; i < attrs.length; i++) {
49
+ const attr = attrs[i];
50
+ if (attr.key.content === attrName) {
51
+ return attr.value.content;
52
+ }
53
+ }
54
+
55
+ return;
56
+ };
57
+
58
+ module.exports = {
59
+ setAttributeToAST,
60
+ getAttributeValueWithAST
61
+ };
@@ -0,0 +1,92 @@
1
+ const lead = require('lead');
2
+ const $ = require('gogocode');
3
+ const pumpify = require('pumpify');
4
+ const through = require('through2');
5
+ const { md5 } = require('./md5');
6
+ const { getAppJsonPath, getAbsoluteComponentPath } = require('./path');
7
+ const { setAttributeToAST, getAttributeValueWithAST } = require('./gogocode');
8
+
9
+ const PLUGIN_NAME = 'we-debug-record-playback';
10
+ let componentsInAppJson = [];
11
+
12
+ module.exports = function (options = {}) {
13
+ const { baseDir } = options || {};
14
+ if (typeof baseDir !== 'string' || !baseDir) {
15
+ throw new Error(`[${PLUGIN_NAME}]请传入正确的baseDir,用于标识遍历的根目录。`);
16
+ }
17
+
18
+ let isInit = false;
19
+
20
+ /** 初始化,获取app.json路径和所有页面路径 */
21
+ const init = () =>
22
+ through.obj((file, encoding, callback) => {
23
+ if (!isInit) {
24
+ try {
25
+ parseAppJsonComponents();
26
+ injectComponentId();
27
+ } catch (e) {
28
+ throw new Error(`[${PLUGIN_NAME}]遍历文件失败,请检查设置是否正确。`);
29
+ }
30
+ isInit = true;
31
+ }
32
+ callback(null, file);
33
+ });
34
+
35
+ /** 解析app.json中引用的组件 */
36
+ const parseAppJsonComponents = () => {
37
+ const appJsonPath = getAppJsonPath(baseDir);
38
+
39
+ componentsInAppJson = require(appJsonPath).usingComponents || [];
40
+ Object.getOwnPropertyNames(componentsInAppJson).forEach(componentName => {
41
+ componentsInAppJson[componentName] = getAbsoluteComponentPath(
42
+ componentsInAppJson[componentName],
43
+ appJsonPath.slice(0, appJsonPath.lastIndexOf('/'))
44
+ );
45
+ });
46
+ };
47
+
48
+ /** 解析页面/页面中引用的组件 */
49
+ const parseChildComponents = path => {
50
+ const usingComponents = require(`${path}.json`).usingComponents || {};
51
+ const componentsInJson = {};
52
+ Object.getOwnPropertyNames(usingComponents).forEach(componentName => {
53
+ componentsInJson[componentName] = getAbsoluteComponentPath(
54
+ usingComponents[componentName],
55
+ path.slice(0, path.lastIndexOf('/'))
56
+ );
57
+ });
58
+ return componentsInJson;
59
+ };
60
+
61
+ const injectComponentId = () =>
62
+ through.obj((file, encoding, callback) => {
63
+ if (file.extname === '.wxml') {
64
+ const gogo = $.loadFile(file.path, { parseOptions: { language: 'html' } });
65
+ const path = file.path.slice(0, -5);
66
+ const usingComponents = {
67
+ ...(parseChildComponents(path) || {}),
68
+ ...componentsInAppJson
69
+ };
70
+ Object.getOwnPropertyNames(usingComponents).forEach(component =>
71
+ gogo
72
+ .replace(`<${component} $$$0 />`, `<${component} $$$0></${component}>`)
73
+ .find(`<${component}></${component}>`)
74
+ .each(item => {
75
+ if (typeof getAttributeValueWithAST(item, 'id') === 'undefined') {
76
+ const itemString = item.generate();
77
+ const id = md5(itemString);
78
+ setAttributeToAST(item, 'id', id);
79
+ }
80
+ })
81
+ );
82
+ const res = gogo.generate();
83
+ // eslint-disable-next-line no-param-reassign
84
+ file.contents = Buffer.from(res);
85
+ }
86
+ callback(null, file);
87
+ });
88
+
89
+ const pipeline = [init(), injectComponentId()];
90
+
91
+ return lead(pumpify.obj(pipeline));
92
+ };
package/lib/lookup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const path = require('path');
2
- const { getAbsolutePath } = require('./utils');
2
+ const { getAbsolutePath } = require('./path');
3
3
 
4
4
  exports.lookupPages = function (projectDir) {
5
5
  let pages = [];
package/lib/md5.js ADDED
@@ -0,0 +1,13 @@
1
+ const crypto = require('crypto');
2
+
3
+ const md5 = src => {
4
+ if (typeof src !== 'string') {
5
+ return '';
6
+ }
7
+ const hash = crypto.createHash('md5');
8
+ return hash.update(src).digest('hex');
9
+ };
10
+
11
+ module.exports = {
12
+ md5
13
+ };
package/lib/path.js ADDED
@@ -0,0 +1,22 @@
1
+ const path = require('path');
2
+
3
+ /** 获取绝对路径 */
4
+ const getAbsolutePath = (p, src) => (path.isAbsolute(p) ? p : path.resolve(src || process.cwd(), p));
5
+
6
+ /** 通过项目入口,获取app.json的绝对路径 */
7
+ const getAppJsonPath = projectDir => {
8
+ const projectDirAbsolute = getAbsolutePath(projectDir);
9
+ return path.format({ dir: projectDirAbsolute, name: 'app', ext: '.json' });
10
+ };
11
+
12
+ /** 获取组件绝对路径 */
13
+ const getAbsoluteComponentPath = (componentPath = '', baseDir = '') => {
14
+ const isGlobalPath = componentPath[0] === '@';
15
+ return isGlobalPath ? componentPath : getAbsolutePath(componentPath, baseDir);
16
+ };
17
+
18
+ module.exports = {
19
+ getAbsolutePath,
20
+ getAppJsonPath,
21
+ getAbsoluteComponentPath
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@we-debug/gulp-tool",
3
- "version": "0.17.0",
3
+ "version": "0.18.1",
4
4
  "description": "we-debug, gulp",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -18,6 +18,8 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
+ "crypto": "^1.0.1",
22
+ "gogocode": "^1.0.55",
21
23
  "lead": "^1.0.0",
22
24
  "plugin-error": "^1.0.1",
23
25
  "pumpify": "^2.0.1",
@@ -26,5 +28,5 @@
26
28
  "readme": "ERROR: No README data found!",
27
29
  "_id": "@we-debug/gulp-tool@0.1.3",
28
30
  "_commitid": "f35f53c",
29
- "gitHead": "83da542ca54dca9b8b128444f06db7e0f790d9f2"
31
+ "gitHead": "9c521c1f24e013c868140bd4286466ebb3ea2f1f"
30
32
  }
package/lib/utils.js DELETED
@@ -1,5 +0,0 @@
1
- const path = require('path');
2
-
3
- exports.getAbsolutePath = function (p) {
4
- return path.isAbsolute(p) ? p : path.resolve(process.cwd(), p);
5
- };