@we-debug/gulp-tool 0.18.1 → 0.20.0

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,33 @@
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.20.0](https://github.com/dlhandsome/we-debug/compare/v0.19.0...v0.20.0) (2023-12-12)
7
+
8
+
9
+ ### Features
10
+
11
+ * **gulp-tool:** add plugin docs ([fed1056](https://github.com/dlhandsome/we-debug/commit/fed1056508ee5d7dae614199f03bca2b2205e0fd))
12
+
13
+
14
+
15
+
16
+
17
+ # [0.19.0](https://github.com/dlhandsome/we-debug/compare/v0.18.1...v0.19.0) (2023-11-01)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * fixed eslint ([c5765fe](https://github.com/dlhandsome/we-debug/commit/c5765fe4495e82f39a88894804b8b55b7ed216c8))
23
+
24
+
25
+ ### Features
26
+
27
+ * **gulp-tool:** support plugins ([7eb8e97](https://github.com/dlhandsome/we-debug/commit/7eb8e97b87e5918026b0ddb27b7beaba30cf3448))
28
+
29
+
30
+
31
+
32
+
6
33
  ## [0.18.1](https://github.com/dlhandsome/we-debug/compare/v0.18.0...v0.18.1) (2023-10-17)
7
34
 
8
35
 
package/README.md CHANGED
@@ -48,3 +48,86 @@ filter | 页面过滤参数,可以是正则、字符串、函数或这三者
48
48
  compName | we-debug 全局组件名 | `string` | `we-debug` | - |
49
49
  compPath | we-debug 全局组件位置,可填绝对路径/相对baseDir的路径 | `string` | `@we-debug/core/component/index/index` | - |
50
50
  entryFile | we-debug 初始化文件位置,可填绝对路径/相对baseDir的路径 | `string` | `./we-debug/index.js` | - |
51
+ plugins | 插件配置 | `array<{ package: string, options: object }>` | `` | - |
52
+
53
+ ## 插件
54
+ ```@we-debug/gulp-tool``` 作为 ```we-debug``` 的一个编译增强型工具,可以帮助开发者在开发调试阶段提升效率,如果开发者希望在编译阶段做更多定制化操作,比如在 wxml 插入片段,给 json 新增字段等等,就需要用到插件能力了。
55
+
56
+ 想要使用一个插件,你只需要 require() 它,然后把它添加到 plugins 数组中。多数插件可以通过选项(options)自定义。
57
+
58
+ **gulpfile.js**
59
+
60
+ ```javascript
61
+ const gulp = require('gulp')
62
+ const weDebug = require('@we-debug/gulp-tool')
63
+ const wedebugGulpXXX = require('wedebug-gulp-xxx')
64
+
65
+ gulp
66
+ .src('dist/**/*.*')
67
+ .pipe(
68
+ weDebug({
69
+ baseDir: 'dist',
70
+ // 配置 we-debug gulp 第三方插件
71
+ plugins: [
72
+ {
73
+ package: wedebugGulpXXX,
74
+ options: { // 插件配置项,由第三方插件自由定义
75
+ // xxx
76
+ }
77
+ }
78
+ ]
79
+ })
80
+ )
81
+ ```
82
+
83
+ ## 开发插件
84
+
85
+ we-debug gulp 插件需要暴露一个函数,并且在函数中进行插件注册
86
+
87
+ ```typescript
88
+
89
+ interface IMeta {
90
+ baseDir: string;
91
+ entryFile?: string;
92
+ }
93
+
94
+ interface ITool {
95
+ meta: IMeta;
96
+ register: (options: IRegistration) => void
97
+ }
98
+
99
+ interface PluginOption {
100
+ [props: string]: any
101
+ }
102
+
103
+ interface IRegistration {
104
+ // 插件名称
105
+ pluginName: string;
106
+ // tool 初始化前执行
107
+ beforeInit?: () => void;
108
+ // tool 初始化时执行
109
+ onInit?: () => void;
110
+ // 检测到项目 wxml 文件后立即执行
111
+ wxml?: ProcessDefine;
112
+ // 插入 we-debug 片段之前执行
113
+ beforeInsertWxml?: ProcessDefine;
114
+ // 插入 we-debug 片段前执行
115
+ afterInsertWxml?: ProcessDefine;
116
+ // 检测到项目 json 文件后立即执行
117
+ json?: ProcessDefine;
118
+ // 检测到项目 js 文件后立即执行
119
+ script?: ProcessDefine;
120
+ // app.js 插入 we-debug import 片段前执行
121
+ beforeInsertScript?: ProcessDefine;
122
+ // app.js 插入 we-debug import 片段后执行
123
+ afterInsertScript?: ProcessDefine;
124
+ }
125
+
126
+ export default function (tool: ITool, options: PluginOption = {}): void {
127
+ // 注册插件
128
+ tool.register({
129
+ // ...
130
+ })
131
+ }
132
+ ```
133
+
package/index.js CHANGED
@@ -7,8 +7,8 @@ const { lookupPages } = require('./lib/lookup');
7
7
  const { belongsPage, belongsApp } = require('./lib/belongs');
8
8
  const { getAbsolutePath } = require('./lib/path');
9
9
  const injectComponentId = require('./lib/inject-component-id');
10
-
11
- const PLUGIN_NAME = '@we-debug/gulp-tool';
10
+ const plugin = require('./lib/plugin');
11
+ const { PLUGIN_NAME } = require('./lib/constants');
12
12
 
13
13
  let pages = [];
14
14
 
@@ -20,6 +20,7 @@ function mpGlobalComp(options = {}) {
20
20
  let compName = options.compName || 'we-debug';
21
21
  let compPath = options.compPath || '@we-debug/core/component/index/index';
22
22
  let entryFile = options.entryFile || 'we-debug/index.js';
23
+ const plugins = options.plugins || [];
23
24
 
24
25
  baseDir = getAbsolutePath(baseDir);
25
26
  const isModule = compPath.indexOf('@we-debug/core') === 0;
@@ -39,12 +40,18 @@ function mpGlobalComp(options = {}) {
39
40
  function init() {
40
41
  return through.obj((file, encoding, callback) => {
41
42
  if (!mpGlobalComp.init) {
43
+ // 执行 beforeInit 插件钩子
44
+ plugin.execLifecycle('beforeInit', file);
45
+
42
46
  try {
43
47
  pages = lookupPages(baseDir);
48
+ plugin.initPlugin(plugins, { ...options, pages });
44
49
  } catch (e) {
45
50
  throw new PluginError(PLUGIN_NAME, 'look up pages failed, please check your configure');
46
51
  }
47
52
  mpGlobalComp.init = true;
53
+ // 执行 onInit 插件钩子
54
+ plugin.execLifecycle('onInit', file);
48
55
  }
49
56
  callback(null, file);
50
57
  });
@@ -56,6 +63,8 @@ function mpGlobalComp(options = {}) {
56
63
 
57
64
  // 如果不是 wxml 文件, 则跳过
58
65
  if (file.extname !== '.wxml') return callback(null, file);
66
+ // 执行 wxml 插件钩子
67
+ plugin.execLifecycle('wxml', file);
59
68
 
60
69
  // 如果命中filter,则跳过
61
70
  if (filter) {
@@ -72,11 +81,15 @@ function mpGlobalComp(options = {}) {
72
81
 
73
82
  // 如果属于 Page
74
83
  if (belongsPage(file.path, pages)) {
84
+ // 插入 we-debug 片段之前执行
85
+ plugin.execLifecycle('beforeInsertWxml', file);
75
86
  let code = file.contents.toString();
76
87
 
77
88
  code += wxmlRaw;
78
89
 
79
90
  file.contents = Buffer.from(code);
91
+ // 插入 we-debug 片段之后执行
92
+ plugin.execLifecycle('afterInsertWxml', file);
80
93
  }
81
94
 
82
95
  callback(null, file);
@@ -105,6 +118,8 @@ function mpGlobalComp(options = {}) {
105
118
 
106
119
  // 如果不是 wxml 文件, 则跳过
107
120
  if (file.extname !== '.json') return callback(null, file);
121
+ // 检测到项目 json 文件后立即执行
122
+ plugin.execLifecycle('json', file);
108
123
 
109
124
  // 如果是 app.json
110
125
  if (belongsApp(file.path, baseDir)) {
@@ -141,13 +156,19 @@ function mpGlobalComp(options = {}) {
141
156
 
142
157
  // 如果不是 JS 文件, 则跳过
143
158
  if (file.extname !== '.js') return callback(null, file);
159
+ // 检测到项目 js 文件后立即执行
160
+ plugin.execLifecycle('script', file);
144
161
 
145
162
  // 如果是 app.js
146
163
  if (belongsApp(file.path, baseDir) || file.relative === 'app.js') {
164
+ // app.js 插入 we-debug import 片段前执行
165
+ plugin.execLifecycle('beforeInsertScript', file);
147
166
  let code = file.contents.toString();
148
167
 
149
168
  code = `require('${entryFile}')\n` + code;
150
169
  file.contents = Buffer.from(code);
170
+ // app.js 插入 we-debug import 片段后执行
171
+ plugin.execLifecycle('afterInsertScript', file);
151
172
  }
152
173
 
153
174
  callback(null, file);
@@ -0,0 +1 @@
1
+ exports.PLUGIN_NAME = '@we-debug/gulp-tool';
package/lib/plugin.js ADDED
@@ -0,0 +1,55 @@
1
+ const PluginError = require('plugin-error');
2
+
3
+ const PLUGIN_NAME = '@we-debug/gulp-tool';
4
+
5
+ class Plugin {
6
+ /** 子插件生命周期 */
7
+ lifecycles = [];
8
+ /** 插件元信息 */
9
+ meta = {};
10
+
11
+ getLifecycles() {
12
+ return this.lifecycles;
13
+ }
14
+
15
+ register(options) {
16
+ this.lifecycles.push(options);
17
+ }
18
+
19
+ execLifecycle(lifetime, file) {
20
+ this.lifecycles.forEach(i => {
21
+ if (typeof i[lifetime] === 'function') {
22
+ i[lifetime].call(this, file);
23
+ }
24
+ });
25
+ }
26
+
27
+ loadPlugin(pkg) {
28
+ const pkgType = typeof pkg;
29
+
30
+ switch (pkgType) {
31
+ case 'function':
32
+ // 函数直接返回
33
+ return pkg;
34
+ case 'string':
35
+ // 字符串识别为 npm 包
36
+ return require(pkg);
37
+ }
38
+ }
39
+
40
+ initPlugin(packges, meta) {
41
+ this.meta = meta;
42
+
43
+ packges.map(p => {
44
+ try {
45
+ // 加载插件
46
+ let fn = this.loadPlugin(p.package);
47
+ fn.call(this, this, p.options);
48
+ } catch (e) {
49
+ throw new PluginError(PLUGIN_NAME, `load package ${p.package} error: ', ${e}`);
50
+ }
51
+ });
52
+ }
53
+ }
54
+
55
+ module.exports = new Plugin();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@we-debug/gulp-tool",
3
- "version": "0.18.1",
3
+ "version": "0.20.0",
4
4
  "description": "we-debug, gulp",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -28,5 +28,5 @@
28
28
  "readme": "ERROR: No README data found!",
29
29
  "_id": "@we-debug/gulp-tool@0.1.3",
30
30
  "_commitid": "f35f53c",
31
- "gitHead": "9c521c1f24e013c868140bd4286466ebb3ea2f1f"
31
+ "gitHead": "94f05738faa94a81e23abb4b0ea3971889abcaae"
32
32
  }