@we-debug/gulp-tool 0.50.0 → 0.52.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 +38 -0
- package/README.md +10 -0
- package/dist/index.js +52 -134
- package/dist/types.d.ts +1 -59
- package/dist/types.js +15 -0
- package/package.json +4 -5
- package/src/index.ts +51 -188
- package/src/types.ts +1 -94
- package/test/index.test.js +91 -0
- package/test/per-call-options.test.js +62 -0
- package/test/subpackage.test.js +130 -0
- package/test/ui.test.js +77 -0
- package/src/belongs.ts +0 -12
- package/src/lookup.ts +0 -42
- package/src/path.ts +0 -4
- package/src/plugin.ts +0 -63
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,44 @@
|
|
|
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.52.0](https://github.com/dlhandsome/we-debug/compare/v0.51.1...v0.52.0) (2026-12-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* resolve subpackage UI plugin host ([0c73838](https://github.com/dlhandsome/we-debug/commit/0c73838496548038dade71630038e7df09c5c10b))
|
|
12
|
+
* **compiler:** relocate copied npm dependencies ([b45f345](https://github.com/dlhandsome/we-debug/commit/b45f345c2b8a1e0ca0a984616dd57ce7147f8e19))
|
|
13
|
+
* **compiler:** relocate gulp compiled entries ([52cc367](https://github.com/dlhandsome/we-debug/commit/52cc36716b02ffe85dfc50da31816d555bab965f))
|
|
14
|
+
* **compiler:** retain async subpackage modules ([85c500e](https://github.com/dlhandsome/we-debug/commit/85c500e14e06ae3ccaa5d6a0cc90b32f23574847))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **gulp-tool:** support subpackage injection mode ([e729ed9](https://github.com/dlhandsome/we-debug/commit/e729ed91fa5e40f9dffe6646a757a0f38d5973c3))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# [0.51.0](https://github.com/dlhandsome/we-debug/compare/v0.50.0...v0.51.0) (2026-12-01)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Bug Fixes
|
|
29
|
+
|
|
30
|
+
* **compiler-core:** 恢复 beforeInit 在插件注册前执行的顺序 ([0c10c90](https://github.com/dlhandsome/we-debug/commit/0c10c90c8441e04d054db953e19dc2d464256f3c))
|
|
31
|
+
* **gulp-tool:** flush 中的异常以 PluginError 报告 ([211db0f](https://github.com/dlhandsome/we-debug/commit/211db0f6b25a5fd94175e34dd6714449ab743f85))
|
|
32
|
+
* **gulp-tool:** isolate per-call compiler options ([c92d116](https://github.com/dlhandsome/we-debug/commit/c92d1161ad73082dc4e01ee531d6cb8d9dbb1dee))
|
|
33
|
+
* **gulp-tool:** 初始化钩子沿用流中的首个文件 ([8e0fe23](https://github.com/dlhandsome/we-debug/commit/8e0fe2320c392647e7833a4ef1f7634c2b4400d8))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Features
|
|
37
|
+
|
|
38
|
+
* **gulp-tool:** 推送 UI 插件宿主、清单与插件文件 ([151dd23](https://github.com/dlhandsome/we-debug/commit/151dd233d48bc1c78e4a45188614b028f8be5531))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
6
44
|
# [0.50.0](https://github.com/dlhandsome/we-debug/compare/v0.49.2...v0.50.0) (2026-11-27)
|
|
7
45
|
|
|
8
46
|
**Note:** Version bump only for package @we-debug/gulp-tool
|
package/README.md
CHANGED
|
@@ -49,6 +49,10 @@ 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
51
|
plugins | 插件配置 | `array<{ package: string, options: object }>` | `` | - |
|
|
52
|
+
uiPlugins | 显式指定 UI 插件包名,指定后不再自动发现 | `string[]` | - | -
|
|
53
|
+
autoDiscoverUIPlugins | 是否从项目 `package.json` 依赖中自动发现 UI 插件 | `boolean` | `true` | -
|
|
54
|
+
npmDirname | UI 插件产物输出的 npm 目录,需与项目使用的 npm 处理插件一致(如 gulp-mp-npm 的 `npmDirname`) | `string` | `miniprogram_npm` | -
|
|
55
|
+
projectDir | `package.json` 与 `node_modules` 所在目录 | `string` | 当前工作目录 | -
|
|
52
56
|
|
|
53
57
|
## 插件
|
|
54
58
|
```@we-debug/gulp-tool``` 作为 ```we-debug``` 的一个编译增强型工具,可以帮助开发者在开发调试阶段提升效率,如果开发者希望在编译阶段做更多定制化操作,比如在 wxml 插入片段,给 json 新增字段等等,就需要用到插件能力了。
|
|
@@ -133,3 +137,9 @@ export default function (tool: ITool, options: PluginOption = {}): void {
|
|
|
133
137
|
}
|
|
134
138
|
```
|
|
135
139
|
|
|
140
|
+
## 自定义 UI 插件
|
|
141
|
+
|
|
142
|
+
依赖中声明了 `wedebug` 字段的包会被识别为 UI 插件。gulp-tool 会在每个流结束时,把宿主组件、清单与插件文件推入该流;一个流只推送它处理过的扩展名(例如只处理 `.js` 的流只推送 `.js` 文件),因此这些文件会和业务文件一样经过后续的 npm 处理、babel 等插件。插件开发方式见 we-debug 文档「自定义 UI 插件」。
|
|
143
|
+
|
|
144
|
+
插件的样式与静态文件(如 `.wxss`、`.wxs`、图片)只会由处理对应扩展名且经过 `weDebug()` 的流输出,因此处理 `.wxss` 或静态资源的流也必须经过 `weDebug()`。仅由其他流把 `.less` 编译成 `.wxss` 不算,除非 `weDebug()` 位于编译步骤之后。
|
|
145
|
+
|
package/dist/index.js
CHANGED
|
@@ -1,149 +1,67 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var fs_1 = __importDefault(require("fs"));
|
|
17
7
|
var path_1 = __importDefault(require("path"));
|
|
18
8
|
var lead_1 = __importDefault(require("lead"));
|
|
19
|
-
var pumpify_1 = __importDefault(require("pumpify"));
|
|
20
9
|
var through2_1 = __importDefault(require("through2"));
|
|
10
|
+
var vinyl_1 = __importDefault(require("vinyl"));
|
|
21
11
|
var plugin_error_1 = __importDefault(require("plugin-error"));
|
|
22
|
-
var
|
|
23
|
-
var belongs_1 = require("./belongs");
|
|
24
|
-
var path_2 = require("./path");
|
|
25
|
-
var plugin_1 = __importDefault(require("./plugin"));
|
|
12
|
+
var compiler_core_1 = require("@we-debug/compiler-core");
|
|
26
13
|
var constants_1 = require("./constants");
|
|
27
|
-
var
|
|
28
|
-
|
|
14
|
+
var initializedInjector = null;
|
|
15
|
+
function getInjector(options, file) {
|
|
16
|
+
if (!initializedInjector) {
|
|
17
|
+
var instance_1 = new compiler_core_1.Injector(options);
|
|
18
|
+
instance_1.init(file);
|
|
19
|
+
initializedInjector = instance_1;
|
|
20
|
+
return instance_1;
|
|
21
|
+
}
|
|
22
|
+
var instance = new compiler_core_1.Injector(options);
|
|
23
|
+
instance.adoptInitState(initializedInjector);
|
|
24
|
+
return instance;
|
|
25
|
+
}
|
|
29
26
|
function mpGlobalComp(options) {
|
|
30
27
|
if (options === void 0) { options = {}; }
|
|
31
|
-
var
|
|
32
|
-
var
|
|
33
|
-
var
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var plugins = options.plugins || [];
|
|
38
|
-
baseDir = (0, path_2.getAbsolutePath)(baseDir);
|
|
39
|
-
var isModule = compPath.indexOf('@we-debug/core') === 0;
|
|
40
|
-
if (!isModule && path_1.default.isAbsolute(compPath)) {
|
|
41
|
-
compPath = path_1.default.relative(baseDir, compPath);
|
|
42
|
-
}
|
|
43
|
-
if (path_1.default.isAbsolute(entryFile)) {
|
|
44
|
-
entryFile = path_1.default.relative(baseDir, entryFile);
|
|
45
|
-
}
|
|
46
|
-
function init() {
|
|
47
|
-
return through2_1.default.obj(function (file, encoding, callback) {
|
|
48
|
-
if (!hasInit) {
|
|
49
|
-
plugin_1.default.execLifecycle('beforeInit', file);
|
|
50
|
-
try {
|
|
51
|
-
pages = (0, lookup_1.lookupPages)(baseDir);
|
|
52
|
-
plugin_1.default.initPlugin(plugins, __assign(__assign({}, options), { pages: pages }));
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
throw new plugin_error_1.default(constants_1.PLUGIN_NAME, 'look up pages failed, please check your configure');
|
|
56
|
-
}
|
|
57
|
-
hasInit = true;
|
|
58
|
-
plugin_1.default.execLifecycle('onInit', file);
|
|
59
|
-
}
|
|
60
|
-
callback(null, file);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
function parseWxml() {
|
|
64
|
-
return through2_1.default.obj(function (file, encoding, callback) {
|
|
65
|
-
if (file.isNull())
|
|
66
|
-
return callback(null, file);
|
|
67
|
-
if (file.extname !== '.wxml')
|
|
68
|
-
return callback(null, file);
|
|
69
|
-
plugin_1.default.execLifecycle('wxml', file);
|
|
70
|
-
if (filter) {
|
|
71
|
-
var pathStr_1 = file.path.split(path_1.default.sep).join('/');
|
|
72
|
-
if (Array.isArray(filter)) {
|
|
73
|
-
var result = filter.some(function (item) {
|
|
74
|
-
return isDoFilter(item, pathStr_1);
|
|
75
|
-
});
|
|
76
|
-
if (result)
|
|
77
|
-
return callback(null, file);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
if (isDoFilter(filter, pathStr_1))
|
|
81
|
-
return callback(null, file);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if ((0, belongs_1.belongsPage)(file.path, pages)) {
|
|
85
|
-
plugin_1.default.execLifecycle('beforeInsertWxml', file);
|
|
86
|
-
var code = file.contents.toString();
|
|
87
|
-
var injectWxml = typeof wxmlRaw === 'function'
|
|
88
|
-
? wxmlRaw({
|
|
89
|
-
useCustomMask: code.indexOf('<page-container') > -1
|
|
90
|
-
})
|
|
91
|
-
: wxmlRaw;
|
|
92
|
-
code += injectWxml;
|
|
93
|
-
file.contents = Buffer.from(code);
|
|
94
|
-
plugin_1.default.execLifecycle('afterInsertWxml', file);
|
|
95
|
-
}
|
|
96
|
-
callback(null, file);
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
function isDoFilter(filter, path) {
|
|
100
|
-
return ((typeof filter === 'function' && !filter(path)) ||
|
|
101
|
-
(typeof filter === 'string' && path.indexOf(filter) >= 0) ||
|
|
102
|
-
(filter instanceof RegExp && filter.test(path)));
|
|
103
|
-
}
|
|
104
|
-
function parseJson() {
|
|
105
|
-
return through2_1.default.obj(function (file, encoding, callback) {
|
|
106
|
-
if (file.isNull())
|
|
107
|
-
return callback(null, file);
|
|
108
|
-
if (file.extname !== '.json')
|
|
109
|
-
return callback(null, file);
|
|
110
|
-
plugin_1.default.execLifecycle('json', file);
|
|
111
|
-
if ((0, belongs_1.belongsApp)(file.path, baseDir)) {
|
|
112
|
-
var code = file.contents.toString();
|
|
113
|
-
try {
|
|
114
|
-
var json = JSON.parse(code);
|
|
115
|
-
if (!json.usingComponents) {
|
|
116
|
-
json.usingComponents = {};
|
|
117
|
-
}
|
|
118
|
-
json.usingComponents[compName] = compPath;
|
|
119
|
-
code = JSON.stringify(json, null, 2);
|
|
120
|
-
}
|
|
121
|
-
catch (e) {
|
|
122
|
-
throw new plugin_error_1.default(constants_1.PLUGIN_NAME, e);
|
|
123
|
-
}
|
|
124
|
-
file.contents = Buffer.from(code);
|
|
125
|
-
}
|
|
126
|
-
callback(null, file);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
function parseScript() {
|
|
130
|
-
return through2_1.default.obj(function (file, encoding, callback) {
|
|
131
|
-
if (file.isNull())
|
|
132
|
-
return callback(null, file);
|
|
133
|
-
if (file.extname !== '.js')
|
|
28
|
+
var seenExtnames = {};
|
|
29
|
+
var streamInjector = null;
|
|
30
|
+
var stream = through2_1.default.obj(function (file, _encoding, callback) {
|
|
31
|
+
try {
|
|
32
|
+
var instance = streamInjector || (streamInjector = getInjector(options, file));
|
|
33
|
+
if (file.isNull() || file.isDirectory())
|
|
134
34
|
return callback(null, file);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
35
|
+
seenExtnames[file.extname] = true;
|
|
36
|
+
instance.transform(file);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
return callback(new plugin_error_1.default(constants_1.PLUGIN_NAME, e instanceof Error ? e.message : String(e)));
|
|
40
|
+
}
|
|
41
|
+
callback(null, file);
|
|
42
|
+
}, function (callback) {
|
|
43
|
+
var _this = this;
|
|
44
|
+
if (!streamInjector)
|
|
45
|
+
return callback();
|
|
46
|
+
try {
|
|
47
|
+
streamInjector.warnings.forEach(function (w) { return console.warn("[".concat(constants_1.PLUGIN_NAME, "] ").concat(w)); });
|
|
48
|
+
var base_1 = streamInjector.options.baseDir;
|
|
49
|
+
streamInjector.getOutputFiles().forEach(function (f) {
|
|
50
|
+
if (!seenExtnames[path_1.default.extname(f.relative)])
|
|
51
|
+
return;
|
|
52
|
+
_this.push(new vinyl_1.default({
|
|
53
|
+
cwd: process.cwd(),
|
|
54
|
+
base: base_1,
|
|
55
|
+
path: path_1.default.join(base_1, f.relative),
|
|
56
|
+
contents: f.contents || fs_1.default.readFileSync(f.sourcePath)
|
|
57
|
+
}));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
return callback(new plugin_error_1.default(constants_1.PLUGIN_NAME, e instanceof Error ? e.message : String(e)));
|
|
62
|
+
}
|
|
63
|
+
callback();
|
|
64
|
+
});
|
|
65
|
+
return (0, lead_1.default)(stream);
|
|
148
66
|
}
|
|
149
67
|
module.exports = mpGlobalComp;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,59 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export interface IAnyObject {
|
|
3
|
-
[key: string]: any;
|
|
4
|
-
}
|
|
5
|
-
export declare namespace WechatMiniprogram {
|
|
6
|
-
type AppJsonSubpage = {
|
|
7
|
-
root: string;
|
|
8
|
-
pages: string[];
|
|
9
|
-
};
|
|
10
|
-
type AppJsonSubpages = AppJsonSubpage[];
|
|
11
|
-
interface IAppJson {
|
|
12
|
-
pages: string[];
|
|
13
|
-
subpackages?: AppJsonSubpages;
|
|
14
|
-
subPackages?: AppJsonSubpages;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export type ProcessDefine = (file: File, encoding?: string) => File | void;
|
|
18
|
-
export interface IRegistration {
|
|
19
|
-
pluginName: string;
|
|
20
|
-
beforeInit?: ProcessDefine;
|
|
21
|
-
onInit?: ProcessDefine;
|
|
22
|
-
wxml?: ProcessDefine;
|
|
23
|
-
beforeInsertWxml?: ProcessDefine;
|
|
24
|
-
afterInsertWxml?: ProcessDefine;
|
|
25
|
-
json?: ProcessDefine;
|
|
26
|
-
script?: ProcessDefine;
|
|
27
|
-
beforeInsertScript?: ProcessDefine;
|
|
28
|
-
afterInsertScript?: ProcessDefine;
|
|
29
|
-
}
|
|
30
|
-
export type FilterOption = string | Function | RegExp;
|
|
31
|
-
export type Meta<T extends IAnyObject = IAnyObject> = ICompilerOption<T> & {
|
|
32
|
-
pages: string[];
|
|
33
|
-
};
|
|
34
|
-
export interface IPlugin<T extends IAnyObject = IAnyObject> {
|
|
35
|
-
lifecycles: IRegistration[];
|
|
36
|
-
meta: Meta<T>;
|
|
37
|
-
register(options: IRegistration): void;
|
|
38
|
-
getLifecycles(): IRegistration[];
|
|
39
|
-
execLifecycle(lifetime: keyof IRegistration, file: File): void;
|
|
40
|
-
loadPlugin(pkg: string | ICompilerPluginPackage): void;
|
|
41
|
-
initPlugin(packges: ICompilerPluginOption<T>[], meta: Meta): void;
|
|
42
|
-
}
|
|
43
|
-
export type ICompilerPluginPackage<T extends IAnyObject = IAnyObject> = (register: IPlugin, options: T) => void;
|
|
44
|
-
export type ICompilerPluginPackageOption<T extends IAnyObject = IAnyObject> = string | ICompilerPluginPackage<T>;
|
|
45
|
-
export interface ICompilerPluginOption<T extends IAnyObject = IAnyObject> {
|
|
46
|
-
package: ICompilerPluginPackageOption<T>;
|
|
47
|
-
options: T;
|
|
48
|
-
}
|
|
49
|
-
export interface ICompilerOption<T extends IAnyObject = IAnyObject> {
|
|
50
|
-
baseDir?: string;
|
|
51
|
-
wxml?: string | ((props: {
|
|
52
|
-
useCustomMask: boolean;
|
|
53
|
-
}) => string);
|
|
54
|
-
filter?: FilterOption;
|
|
55
|
-
compName?: string;
|
|
56
|
-
compPath?: string;
|
|
57
|
-
entryFile?: string;
|
|
58
|
-
plugins?: ICompilerPluginOption<T>[];
|
|
59
|
-
}
|
|
1
|
+
export * from '@we-debug/compiler-core';
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("@we-debug/compiler-core"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@we-debug/gulp-tool",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "we-debug, gulp",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/types.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"lib": "lib"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
|
-
"test": "
|
|
11
|
+
"test": "node test/index.test.js && node test/ui.test.js && node test/per-call-options.test.js && node test/subpackage.test.js"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"we-debug",
|
|
@@ -19,18 +19,17 @@
|
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@types/pumpify": "^1.4.4",
|
|
23
22
|
"@types/through2": "^2.0.41",
|
|
23
|
+
"@we-debug/compiler-core": "0.52.0",
|
|
24
24
|
"crypto": "^1.0.1",
|
|
25
25
|
"gogocode": "^1.0.55",
|
|
26
26
|
"lead": "^1.0.0",
|
|
27
27
|
"plugin-error": "^1.0.1",
|
|
28
|
-
"pumpify": "^2.0.1",
|
|
29
28
|
"through2": "^3.0.1",
|
|
30
29
|
"vinyl": "^3.0.0"
|
|
31
30
|
},
|
|
32
31
|
"readme": "ERROR: No README data found!",
|
|
33
32
|
"_id": "@we-debug/gulp-tool@0.1.3",
|
|
34
33
|
"_commitid": "f35f53c",
|
|
35
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "bc98652915ac460bb4015b9918c32f899fe049a5"
|
|
36
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,204 +1,67 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
1
2
|
import path from 'path';
|
|
2
3
|
import lead from 'lead';
|
|
3
|
-
import pumpify from 'pumpify';
|
|
4
4
|
import through from 'through2';
|
|
5
|
+
import Vinyl from 'vinyl';
|
|
5
6
|
import PluginError from 'plugin-error';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
} from './lookup';
|
|
9
|
-
import {
|
|
10
|
-
belongsPage,
|
|
11
|
-
belongsApp
|
|
12
|
-
} from './belongs';
|
|
13
|
-
import {
|
|
14
|
-
getAbsolutePath
|
|
15
|
-
} from './path';
|
|
16
|
-
import plugin from './plugin';
|
|
17
|
-
import {
|
|
18
|
-
PLUGIN_NAME
|
|
19
|
-
} from './constants';
|
|
20
|
-
import {
|
|
21
|
-
FilterOption,
|
|
22
|
-
ICompilerOption
|
|
23
|
-
} from './types';
|
|
7
|
+
import { Injector, ICompilerOption, IFile } from '@we-debug/compiler-core';
|
|
8
|
+
import { PLUGIN_NAME } from './constants';
|
|
24
9
|
|
|
25
|
-
let
|
|
26
|
-
let hasInit = false;
|
|
10
|
+
let initializedInjector: Injector | null = null;
|
|
27
11
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let compPath = options.compPath || '@we-debug/miniprogram/index/index';
|
|
35
|
-
let entryFile = options.entryFile || 'we-debug/index.js';
|
|
36
|
-
const plugins = options.plugins || [];
|
|
37
|
-
|
|
38
|
-
baseDir = getAbsolutePath(baseDir);
|
|
39
|
-
const isModule = compPath.indexOf('@we-debug/core') === 0;
|
|
40
|
-
|
|
41
|
-
if (!isModule && path.isAbsolute(compPath)) {
|
|
42
|
-
compPath = path.relative(baseDir, compPath);
|
|
12
|
+
function getInjector(options: ICompilerOption, file: IFile) {
|
|
13
|
+
if (!initializedInjector) {
|
|
14
|
+
const instance = new Injector(options);
|
|
15
|
+
instance.init(file);
|
|
16
|
+
initializedInjector = instance;
|
|
17
|
+
return instance;
|
|
43
18
|
}
|
|
19
|
+
const instance = new Injector(options);
|
|
20
|
+
instance.adoptInitState(initializedInjector);
|
|
21
|
+
return instance;
|
|
22
|
+
}
|
|
44
23
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
pages = lookupPages(baseDir);
|
|
61
|
-
plugin.initPlugin(plugins, { ...options, pages });
|
|
62
|
-
} catch (e) {
|
|
63
|
-
throw new PluginError(PLUGIN_NAME, 'look up pages failed, please check your configure');
|
|
64
|
-
}
|
|
65
|
-
hasInit = true;
|
|
66
|
-
// 执行 onInit 插件钩子
|
|
67
|
-
plugin.execLifecycle('onInit', file);
|
|
24
|
+
function mpGlobalComp(options: ICompilerOption = {}) {
|
|
25
|
+
// 只推送本流处理过的扩展名,保证 UI 文件经过与业务文件相同的后续处理
|
|
26
|
+
const seenExtnames: { [extname: string]: true } = {};
|
|
27
|
+
let streamInjector: Injector | null = null;
|
|
28
|
+
|
|
29
|
+
const stream = through.obj(
|
|
30
|
+
function (file: any, _encoding: any, callback: any) {
|
|
31
|
+
try {
|
|
32
|
+
const instance = streamInjector || (streamInjector = getInjector(options, file as IFile));
|
|
33
|
+
if (file.isNull() || file.isDirectory()) return callback(null, file);
|
|
34
|
+
seenExtnames[file.extname] = true;
|
|
35
|
+
instance.transform(file as IFile);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return callback(new PluginError(PLUGIN_NAME, e instanceof Error ? e.message : String(e)));
|
|
68
38
|
}
|
|
69
39
|
callback(null, file);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
plugin.execLifecycle('wxml', file);
|
|
85
|
-
|
|
86
|
-
// 如果命中filter,则跳过
|
|
87
|
-
if (filter) {
|
|
88
|
-
const pathStr = file.path.split(path.sep).join('/'); // 兼容不同操作系统的路径分隔符
|
|
89
|
-
if (Array.isArray(filter)) {
|
|
90
|
-
const result = filter.some(item => {
|
|
91
|
-
return isDoFilter(item, pathStr);
|
|
92
|
-
});
|
|
93
|
-
if (result) return callback(null, file);
|
|
94
|
-
} else {
|
|
95
|
-
if (isDoFilter(filter, pathStr)) return callback(null, file);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// 如果属于 Page
|
|
100
|
-
if (belongsPage(file.path, pages)) {
|
|
101
|
-
// 插入 we-debug 片段之前执行
|
|
102
|
-
plugin.execLifecycle('beforeInsertWxml', file);
|
|
103
|
-
let code = file.contents.toString();
|
|
104
|
-
let injectWxml = typeof wxmlRaw === 'function'
|
|
105
|
-
? wxmlRaw({
|
|
106
|
-
useCustomMask: code.indexOf('<page-container') > -1
|
|
40
|
+
},
|
|
41
|
+
function (this: any, callback: any) {
|
|
42
|
+
if (!streamInjector) return callback();
|
|
43
|
+
try {
|
|
44
|
+
streamInjector.warnings.forEach(w => console.warn(`[${PLUGIN_NAME}] ${w}`));
|
|
45
|
+
const base = streamInjector.options.baseDir;
|
|
46
|
+
streamInjector.getOutputFiles().forEach(f => {
|
|
47
|
+
if (!seenExtnames[path.extname(f.relative)]) return;
|
|
48
|
+
this.push(
|
|
49
|
+
new Vinyl({
|
|
50
|
+
cwd: process.cwd(),
|
|
51
|
+
base,
|
|
52
|
+
path: path.join(base, f.relative),
|
|
53
|
+
contents: f.contents || fs.readFileSync(f.sourcePath as string)
|
|
107
54
|
})
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
file.contents = Buffer.from(code);
|
|
113
|
-
// 插入 we-debug 片段之后执行
|
|
114
|
-
plugin.execLifecycle('afterInsertWxml', file);
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return callback(new PluginError(PLUGIN_NAME, e instanceof Error ? e.message : String(e)));
|
|
115
59
|
}
|
|
60
|
+
callback();
|
|
61
|
+
}
|
|
62
|
+
);
|
|
116
63
|
|
|
117
|
-
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 是否需要过滤
|
|
123
|
-
* @returns [Boolean] true-需要过滤;false-不需要过滤
|
|
124
|
-
*/
|
|
125
|
-
function isDoFilter(filter: FilterOption, path: string) {
|
|
126
|
-
return (
|
|
127
|
-
(typeof filter === 'function' && !filter(path)) ||
|
|
128
|
-
(typeof filter === 'string' && path.indexOf(filter) >= 0) ||
|
|
129
|
-
(filter instanceof RegExp && filter.test(path))
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* 处理 json
|
|
135
|
-
*/
|
|
136
|
-
function parseJson() {
|
|
137
|
-
// @ts-ignore
|
|
138
|
-
return through.obj((file, encoding, callback) => {
|
|
139
|
-
if (file.isNull()) return callback(null, file);
|
|
140
|
-
|
|
141
|
-
// 如果不是 wxml 文件, 则跳过
|
|
142
|
-
if (file.extname !== '.json') return callback(null, file);
|
|
143
|
-
// 检测到项目 json 文件后立即执行
|
|
144
|
-
plugin.execLifecycle('json', file);
|
|
145
|
-
|
|
146
|
-
// 如果是 app.json
|
|
147
|
-
if (belongsApp(file.path, baseDir)) {
|
|
148
|
-
let code = file.contents.toString();
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
const json = JSON.parse(code);
|
|
152
|
-
|
|
153
|
-
if (!json.usingComponents) {
|
|
154
|
-
json.usingComponents = {};
|
|
155
|
-
}
|
|
156
|
-
json.usingComponents[compName] = compPath;
|
|
157
|
-
|
|
158
|
-
code = JSON.stringify(json, null, 2);
|
|
159
|
-
} catch (e) {
|
|
160
|
-
throw new PluginError(PLUGIN_NAME, e);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
file.contents = Buffer.from(code);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
callback(null, file);
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 处理 js
|
|
172
|
-
*/
|
|
173
|
-
function parseScript() {
|
|
174
|
-
// @ts-ignore
|
|
175
|
-
return through.obj((file, encoding, callback) => {
|
|
176
|
-
if (file.isNull()) return callback(null, file);
|
|
177
|
-
|
|
178
|
-
// 如果不是 JS 文件, 则跳过
|
|
179
|
-
if (file.extname !== '.js') return callback(null, file);
|
|
180
|
-
// 检测到项目 js 文件后立即执行
|
|
181
|
-
plugin.execLifecycle('script', file);
|
|
182
|
-
|
|
183
|
-
// 如果是 app.js
|
|
184
|
-
if (belongsApp(file.path, baseDir) || file.relative === 'app.js') {
|
|
185
|
-
// app.js 插入 we-debug import 片段前执行
|
|
186
|
-
plugin.execLifecycle('beforeInsertScript', file);
|
|
187
|
-
let code = file.contents.toString();
|
|
188
|
-
|
|
189
|
-
code = `require('${entryFile}')\n` + code;
|
|
190
|
-
file.contents = Buffer.from(code);
|
|
191
|
-
// app.js 插入 we-debug import 片段后执行
|
|
192
|
-
plugin.execLifecycle('afterInsertScript', file);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
callback(null, file);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const pipeline = [init(), parseWxml(), parseJson(), parseScript()];
|
|
200
|
-
|
|
201
|
-
return lead(new pumpify.obj(pipeline));
|
|
64
|
+
return lead(stream);
|
|
202
65
|
}
|
|
203
66
|
|
|
204
67
|
module.exports = mpGlobalComp;
|
package/src/types.ts
CHANGED
|
@@ -1,94 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export interface IAnyObject {
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export namespace WechatMiniprogram {
|
|
8
|
-
export type AppJsonSubpage = {
|
|
9
|
-
root: string;
|
|
10
|
-
pages: string[];
|
|
11
|
-
};
|
|
12
|
-
export type AppJsonSubpages = AppJsonSubpage[];
|
|
13
|
-
|
|
14
|
-
export interface IAppJson {
|
|
15
|
-
pages: string[];
|
|
16
|
-
subpackages?: AppJsonSubpages;
|
|
17
|
-
subPackages?: AppJsonSubpages;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type ProcessDefine = (file: File, encoding?: string) => File | void;
|
|
22
|
-
|
|
23
|
-
export interface IRegistration {
|
|
24
|
-
// 插件名称
|
|
25
|
-
pluginName: string;
|
|
26
|
-
// tool 初始化前执行
|
|
27
|
-
beforeInit?: ProcessDefine;
|
|
28
|
-
// tool 初始化时执行
|
|
29
|
-
onInit?: ProcessDefine;
|
|
30
|
-
// 检测到项目 wxml 文件后立即执行
|
|
31
|
-
wxml?: ProcessDefine;
|
|
32
|
-
// 插入 we-debug 片段之前执行
|
|
33
|
-
beforeInsertWxml?: ProcessDefine;
|
|
34
|
-
// 插入 we-debug 片段前执行
|
|
35
|
-
afterInsertWxml?: ProcessDefine;
|
|
36
|
-
// 检测到项目 json 文件后立即执行
|
|
37
|
-
json?: ProcessDefine;
|
|
38
|
-
// 检测到项目 js 文件后立即执行
|
|
39
|
-
script?: ProcessDefine;
|
|
40
|
-
// app.js 插入 we-debug import 片段前执行
|
|
41
|
-
beforeInsertScript?: ProcessDefine;
|
|
42
|
-
// app.js 插入 we-debug import 片段后执行
|
|
43
|
-
afterInsertScript?: ProcessDefine;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type FilterOption = string | Function | RegExp;
|
|
47
|
-
|
|
48
|
-
export type Meta<T extends IAnyObject = IAnyObject> = ICompilerOption<T> & { pages: string[] };
|
|
49
|
-
|
|
50
|
-
export interface IPlugin<T extends IAnyObject = IAnyObject> {
|
|
51
|
-
/** 插件生命周期钩子声明 */
|
|
52
|
-
lifecycles: IRegistration[];
|
|
53
|
-
/** 插件元信息 */
|
|
54
|
-
meta: Meta<T>;
|
|
55
|
-
/** 注册插件 */
|
|
56
|
-
register(options: IRegistration): void;
|
|
57
|
-
/** 获取插件生命周期钩子声明 */
|
|
58
|
-
getLifecycles(): IRegistration[];
|
|
59
|
-
/** 执行插件生命周期钩子声明 */
|
|
60
|
-
execLifecycle(lifetime: keyof IRegistration, file: File): void;
|
|
61
|
-
/** 加载插件 */
|
|
62
|
-
loadPlugin(pkg: string | ICompilerPluginPackage): void;
|
|
63
|
-
/** 初始化插件列表 */
|
|
64
|
-
initPlugin(packges: ICompilerPluginOption<T>[], meta: Meta): void;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export type ICompilerPluginPackage<T extends IAnyObject = IAnyObject> = (register: IPlugin, options: T) => void;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 插件
|
|
71
|
-
*/
|
|
72
|
-
export type ICompilerPluginPackageOption<T extends IAnyObject = IAnyObject> = string | ICompilerPluginPackage<T>;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 插件参数
|
|
76
|
-
*/
|
|
77
|
-
export interface ICompilerPluginOption<T extends IAnyObject = IAnyObject> {
|
|
78
|
-
package: ICompilerPluginPackageOption<T>;
|
|
79
|
-
options: T;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 编译函数入口参数
|
|
84
|
-
*/
|
|
85
|
-
export interface ICompilerOption<T extends IAnyObject = IAnyObject> {
|
|
86
|
-
/** */
|
|
87
|
-
baseDir?: string;
|
|
88
|
-
wxml?: string | ((props: { useCustomMask: boolean }) => string);
|
|
89
|
-
filter?: FilterOption;
|
|
90
|
-
compName?: string;
|
|
91
|
-
compPath?: string;
|
|
92
|
-
entryFile?: string;
|
|
93
|
-
plugins?: ICompilerPluginOption<T>[];
|
|
94
|
-
}
|
|
1
|
+
export * from '@we-debug/compiler-core';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const Vinyl = require('vinyl');
|
|
6
|
+
const weDebug = require('../dist');
|
|
7
|
+
|
|
8
|
+
function createProject() {
|
|
9
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'we-debug-gulp-'));
|
|
10
|
+
const files = {
|
|
11
|
+
'src/app.json': JSON.stringify({
|
|
12
|
+
pages: ['pages/index/index', 'pages/sheet/sheet'],
|
|
13
|
+
subpackages: [{ root: 'sub', pages: ['detail/detail'] }]
|
|
14
|
+
}),
|
|
15
|
+
'src/app.js': 'App({})',
|
|
16
|
+
'src/pages/index/index.wxml': '<view>index</view>',
|
|
17
|
+
'src/pages/sheet/sheet.wxml': '<page-container show />',
|
|
18
|
+
'src/sub/detail/detail.wxml': '<view>detail</view>',
|
|
19
|
+
'src/components/card/card.wxml': '<view>card</view>'
|
|
20
|
+
};
|
|
21
|
+
Object.keys(files).forEach(f => {
|
|
22
|
+
const abs = path.join(root, f);
|
|
23
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
24
|
+
fs.writeFileSync(abs, files[f]);
|
|
25
|
+
});
|
|
26
|
+
return root;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function walk(dir, files = []) {
|
|
30
|
+
fs.readdirSync(dir, { withFileTypes: true }).forEach(entry => {
|
|
31
|
+
const abs = path.join(dir, entry.name);
|
|
32
|
+
if (entry.isDirectory()) walk(abs, files);
|
|
33
|
+
else files.push(abs);
|
|
34
|
+
});
|
|
35
|
+
return files;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function run(root, options) {
|
|
39
|
+
const src = path.join(root, 'src');
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
const out = {};
|
|
42
|
+
const stream = weDebug(options);
|
|
43
|
+
stream.on('data', file => {
|
|
44
|
+
out[file.relative.split(path.sep).join('/')] = String(file.contents);
|
|
45
|
+
});
|
|
46
|
+
stream.on('error', reject);
|
|
47
|
+
stream.on('end', () => resolve(out));
|
|
48
|
+
const firstFile = path.join(src, 'components/card/card.wxml');
|
|
49
|
+
const files = [firstFile, ...walk(src).filter(abs => abs !== firstFile)];
|
|
50
|
+
files.forEach(abs => {
|
|
51
|
+
stream.write(new Vinyl({ cwd: root, base: src, path: abs, contents: fs.readFileSync(abs) }));
|
|
52
|
+
});
|
|
53
|
+
stream.end();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
(async function main() {
|
|
58
|
+
const root = createProject();
|
|
59
|
+
const lifecycleFiles = {};
|
|
60
|
+
function lifecyclePlugin(plugin) {
|
|
61
|
+
plugin.register({
|
|
62
|
+
pluginName: 'lifecycle-test',
|
|
63
|
+
onInit(file) {
|
|
64
|
+
lifecycleFiles.onInit = file.relative;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const out = await run(root, {
|
|
70
|
+
baseDir: path.join(root, 'src'),
|
|
71
|
+
entryFile: './utils/we-debug/index',
|
|
72
|
+
filter: 'sub/',
|
|
73
|
+
plugins: [{ package: lifecyclePlugin, options: {} }]
|
|
74
|
+
});
|
|
75
|
+
assert.deepStrictEqual(lifecycleFiles, { onInit: 'components/card/card.wxml' });
|
|
76
|
+
assert.strictEqual(out['pages/index/index.wxml'], '<view>index</view><we-debug useCustomMask="{{false}}" />');
|
|
77
|
+
assert.strictEqual(out['pages/sheet/sheet.wxml'], '<page-container show /><we-debug useCustomMask="{{true}}" />');
|
|
78
|
+
assert.strictEqual(out['sub/detail/detail.wxml'], '<view>detail</view>');
|
|
79
|
+
assert.strictEqual(out['components/card/card.wxml'], '<view>card</view>');
|
|
80
|
+
assert.strictEqual(out['app.js'], `require('./utils/we-debug/index')\nApp({})`);
|
|
81
|
+
assert.deepStrictEqual(JSON.parse(out['app.json']).usingComponents, {
|
|
82
|
+
'we-debug': '@we-debug/miniprogram/index/index'
|
|
83
|
+
});
|
|
84
|
+
} finally {
|
|
85
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
86
|
+
}
|
|
87
|
+
console.log('✓ gulp-tool 注入行为');
|
|
88
|
+
})().catch(e => {
|
|
89
|
+
console.error('✗ gulp-tool 注入行为\n', e);
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const Vinyl = require('vinyl');
|
|
6
|
+
const weDebug = require('../dist');
|
|
7
|
+
|
|
8
|
+
function run(root, options) {
|
|
9
|
+
const src = path.join(root, 'src');
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const output = {};
|
|
12
|
+
const stream = weDebug(Object.assign({ baseDir: src, projectDir: root }, options));
|
|
13
|
+
stream.on('data', file => {
|
|
14
|
+
output[file.relative.split(path.sep).join('/')] = String(file.contents);
|
|
15
|
+
});
|
|
16
|
+
stream.on('error', reject);
|
|
17
|
+
stream.on('end', () => resolve(output));
|
|
18
|
+
['pages/index/index.wxml', 'app.json'].forEach(relative => {
|
|
19
|
+
const abs = path.join(src, relative);
|
|
20
|
+
stream.write(new Vinyl({ cwd: root, base: src, path: abs, contents: fs.readFileSync(abs) }));
|
|
21
|
+
});
|
|
22
|
+
stream.end();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
(async function main() {
|
|
27
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'we-debug-gulp-options-'));
|
|
28
|
+
fs.mkdirSync(path.join(root, 'src/pages/index'), { recursive: true });
|
|
29
|
+
fs.writeFileSync(path.join(root, 'src/app.json'), JSON.stringify({ pages: ['pages/index/index'] }));
|
|
30
|
+
fs.writeFileSync(path.join(root, 'src/pages/index/index.wxml'), '<view>index</view>');
|
|
31
|
+
|
|
32
|
+
let initCount = 0;
|
|
33
|
+
const plugin = tool => {
|
|
34
|
+
tool.register({
|
|
35
|
+
pluginName: 'init-once',
|
|
36
|
+
onInit() {
|
|
37
|
+
initCount++;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const first = await run(root, {
|
|
44
|
+
wxml: '<first-debug />',
|
|
45
|
+
compName: 'first-debug',
|
|
46
|
+
plugins: [{ package: plugin }]
|
|
47
|
+
});
|
|
48
|
+
const second = await run(root, {
|
|
49
|
+
wxml: '<second-debug />',
|
|
50
|
+
compName: 'second-debug'
|
|
51
|
+
});
|
|
52
|
+
assert.strictEqual(first['pages/index/index.wxml'], '<view>index</view><first-debug />');
|
|
53
|
+
assert.strictEqual(second['pages/index/index.wxml'], '<view>index</view><second-debug />');
|
|
54
|
+
assert.strictEqual(initCount, 1);
|
|
55
|
+
} finally {
|
|
56
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
57
|
+
}
|
|
58
|
+
console.log('✓ gulp-tool 每次调用使用独立选项且只初始化一次');
|
|
59
|
+
})().catch(e => {
|
|
60
|
+
console.error('✗ gulp-tool 每次调用使用独立选项且只初始化一次\n', e);
|
|
61
|
+
process.exitCode = 1;
|
|
62
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const Vinyl = require('vinyl');
|
|
6
|
+
const weDebug = require('../dist');
|
|
7
|
+
|
|
8
|
+
function write(root, files) {
|
|
9
|
+
Object.keys(files).forEach(f => {
|
|
10
|
+
const abs = path.join(root, f);
|
|
11
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
12
|
+
fs.writeFileSync(abs, files[f]);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function run(root, relatives, outputBase) {
|
|
17
|
+
const src = path.join(root, 'src');
|
|
18
|
+
const base = outputBase || src;
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const out = [];
|
|
21
|
+
const stream = weDebug({
|
|
22
|
+
baseDir: src,
|
|
23
|
+
projectDir: root,
|
|
24
|
+
npmDirname: 'npm',
|
|
25
|
+
entryFile: './utils/we-debug/index',
|
|
26
|
+
subpackage: true
|
|
27
|
+
});
|
|
28
|
+
stream.on('data', file => out.push(file));
|
|
29
|
+
stream.on('error', reject);
|
|
30
|
+
stream.on('end', () => resolve(out));
|
|
31
|
+
relatives.forEach(r => {
|
|
32
|
+
const source = path.join(src, r);
|
|
33
|
+
stream.write(new Vinyl({ cwd: root, base, path: path.join(base, r), contents: fs.readFileSync(source) }));
|
|
34
|
+
});
|
|
35
|
+
stream.end();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
(async function main() {
|
|
40
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'we-debug-gulp-sub-'));
|
|
41
|
+
write(root, {
|
|
42
|
+
'package.json': JSON.stringify({
|
|
43
|
+
devDependencies: { '@we-debug/miniprogram': '1.0.0', 'demo-plugin': '1.0.0' }
|
|
44
|
+
}),
|
|
45
|
+
'node_modules/@we-debug/miniprogram/package.json': JSON.stringify({
|
|
46
|
+
name: '@we-debug/miniprogram',
|
|
47
|
+
miniprogram: 'dist'
|
|
48
|
+
}),
|
|
49
|
+
'node_modules/@we-debug/miniprogram/dist/index/index.js': 'Component({})',
|
|
50
|
+
'node_modules/@we-debug/miniprogram/dist/index/index.json': JSON.stringify({
|
|
51
|
+
component: true,
|
|
52
|
+
componentGenerics: {
|
|
53
|
+
'plugin-host': {
|
|
54
|
+
default: '../host-empty/index'
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
'node_modules/@we-debug/miniprogram/dist/index/index.wxml': '<view />',
|
|
59
|
+
'node_modules/demo-plugin/package.json': JSON.stringify({
|
|
60
|
+
name: 'demo-plugin',
|
|
61
|
+
miniprogram: 'dist',
|
|
62
|
+
wedebug: { components: { panel: 'panel/index' } }
|
|
63
|
+
}),
|
|
64
|
+
'node_modules/demo-plugin/dist/index.js': 'module.exports = {}',
|
|
65
|
+
'node_modules/demo-plugin/dist/panel/index.js': 'Component({})',
|
|
66
|
+
'node_modules/demo-plugin/dist/panel/index.json': '{"component":true}',
|
|
67
|
+
'src/app.json': JSON.stringify({ pages: ['pages/index/index'] }),
|
|
68
|
+
'src/app.js': 'App({})',
|
|
69
|
+
'src/pages/index/index.wxml': '<view>index</view>',
|
|
70
|
+
'src/utils/we-debug/index.js': [
|
|
71
|
+
`require("../../npm/@we-debug/miniprogram/index.js")`,
|
|
72
|
+
`require("../../npm/demo-plugin/index.js")`,
|
|
73
|
+
`require("../../npm/unrelated/index.js")`
|
|
74
|
+
].join('\n')
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const scripts = await run(root, ['app.js', 'utils/we-debug/index.js']);
|
|
78
|
+
const byPath = {};
|
|
79
|
+
scripts.forEach(file => {
|
|
80
|
+
byPath[file.relative.split(path.sep).join('/')] = file;
|
|
81
|
+
});
|
|
82
|
+
assert.deepStrictEqual(Object.keys(byPath).sort(), [
|
|
83
|
+
'app.js',
|
|
84
|
+
'we-debug-pkg/entry/index.js',
|
|
85
|
+
'we-debug-pkg/miniprogram_npm/@we-debug/miniprogram/index/index.js',
|
|
86
|
+
'we-debug-pkg/miniprogram_npm/demo-plugin/index.js',
|
|
87
|
+
'we-debug-pkg/miniprogram_npm/demo-plugin/panel/index.js',
|
|
88
|
+
'we-debug-pkg/placeholder/index.js',
|
|
89
|
+
'we-debug-pkg/we-debug-host/index.js',
|
|
90
|
+
'we-debug-pkg/we-debug-host/manifest.js',
|
|
91
|
+
'we-debug-stub.js'
|
|
92
|
+
]);
|
|
93
|
+
assert.strictEqual(byPath['app.js'].contents.toString(), `require('./we-debug-stub.js')\nApp({})`);
|
|
94
|
+
assert.strictEqual(
|
|
95
|
+
byPath['we-debug-pkg/entry/index.js'].contents.toString(),
|
|
96
|
+
[
|
|
97
|
+
`require("../miniprogram_npm/@we-debug/miniprogram/index.js")`,
|
|
98
|
+
`require("../miniprogram_npm/demo-plugin/index.js")`,
|
|
99
|
+
`require("../../npm/unrelated/index.js")`
|
|
100
|
+
].join('\n')
|
|
101
|
+
);
|
|
102
|
+
assert.strictEqual(
|
|
103
|
+
byPath['we-debug-pkg/placeholder/index.js'].contents.toString(),
|
|
104
|
+
`require('../we-debug-host/manifest.js');\nrequire('../entry/index.js');\nPage({});\n`
|
|
105
|
+
);
|
|
106
|
+
assert.strictEqual(byPath['we-debug-stub.js'].base, path.join(root, 'src'));
|
|
107
|
+
console.log('✓ gulp-tool 分包模式搬迁入口并推送分包文件');
|
|
108
|
+
|
|
109
|
+
const dist = path.join(root, 'dist');
|
|
110
|
+
const compiledScripts = await run(root, ['app.js', 'utils/we-debug/index.js'], dist);
|
|
111
|
+
const compiledByPath = {};
|
|
112
|
+
compiledScripts.forEach(file => {
|
|
113
|
+
compiledByPath[file.relative.split(path.sep).join('/')] = file;
|
|
114
|
+
});
|
|
115
|
+
assert.ok(compiledByPath['we-debug-pkg/entry/index.js']);
|
|
116
|
+
assert.strictEqual(
|
|
117
|
+
compiledByPath['we-debug-pkg/entry/index.js'].contents.toString(),
|
|
118
|
+
[
|
|
119
|
+
`require("../miniprogram_npm/@we-debug/miniprogram/index.js")`,
|
|
120
|
+
`require("../miniprogram_npm/demo-plugin/index.js")`,
|
|
121
|
+
`require("../../npm/unrelated/index.js")`
|
|
122
|
+
].join('\n')
|
|
123
|
+
);
|
|
124
|
+
assert.strictEqual(compiledByPath['app.js'].contents.toString(), `require('./we-debug-stub.js')\nApp({})`);
|
|
125
|
+
assert.ok(compiledByPath['we-debug-stub.js']);
|
|
126
|
+
console.log('✓ gulp-tool 支持编译后 Vinyl 路径');
|
|
127
|
+
})().catch(e => {
|
|
128
|
+
console.error('✗ gulp-tool 分包模式搬迁入口并推送分包文件\n', e);
|
|
129
|
+
process.exitCode = 1;
|
|
130
|
+
});
|
package/test/ui.test.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const Vinyl = require('vinyl');
|
|
6
|
+
const weDebug = require('../dist');
|
|
7
|
+
|
|
8
|
+
function write(root, files) {
|
|
9
|
+
Object.keys(files).forEach(f => {
|
|
10
|
+
const abs = path.join(root, f);
|
|
11
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
12
|
+
fs.writeFileSync(abs, files[f]);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function run(root, relatives) {
|
|
17
|
+
const src = path.join(root, 'src');
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const out = [];
|
|
20
|
+
const stream = weDebug({ baseDir: src, projectDir: root, npmDirname: 'npm' });
|
|
21
|
+
stream.on('data', file => out.push(file));
|
|
22
|
+
stream.on('error', reject);
|
|
23
|
+
stream.on('end', () => resolve(out));
|
|
24
|
+
relatives.forEach(r => {
|
|
25
|
+
const abs = path.join(src, r);
|
|
26
|
+
stream.write(new Vinyl({ cwd: root, base: src, path: abs, contents: fs.readFileSync(abs) }));
|
|
27
|
+
});
|
|
28
|
+
stream.end();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
(async function main() {
|
|
33
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'we-debug-gulp-ui-'));
|
|
34
|
+
write(root, {
|
|
35
|
+
'package.json': JSON.stringify({ dependencies: { 'demo-plugin': '1.0.0' } }),
|
|
36
|
+
'node_modules/demo-plugin/package.json': JSON.stringify({
|
|
37
|
+
name: 'demo-plugin',
|
|
38
|
+
miniprogram: 'dist',
|
|
39
|
+
wedebug: { components: { panel: 'panel/index' } }
|
|
40
|
+
}),
|
|
41
|
+
'node_modules/demo-plugin/dist/panel/index.json': '{"component":true}',
|
|
42
|
+
'node_modules/demo-plugin/dist/panel/index.js': 'Component({})',
|
|
43
|
+
'node_modules/demo-plugin/dist/panel/index.wxml': '<view>panel</view>',
|
|
44
|
+
'node_modules/demo-plugin/dist/panel/index.wxss': '.a{}',
|
|
45
|
+
'src/app.json': JSON.stringify({ pages: ['pages/index/index'] }),
|
|
46
|
+
'src/app.js': 'App({})',
|
|
47
|
+
'src/pages/index/index.wxml': '<view>index</view>'
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const wxmlOnly = await run(root, ['pages/index/index.wxml']);
|
|
51
|
+
assert.deepStrictEqual(wxmlOnly.map(file => file.relative.split(path.sep).join('/')).sort(), [
|
|
52
|
+
'npm/demo-plugin/panel/index.wxml',
|
|
53
|
+
'pages/index/index.wxml',
|
|
54
|
+
'we-debug-host/index.wxml'
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
const scripts = await run(root, ['app.js', 'app.json']);
|
|
58
|
+
assert.deepStrictEqual(scripts.map(file => file.relative.split(path.sep).join('/')).sort(), [
|
|
59
|
+
'app.js',
|
|
60
|
+
'app.json',
|
|
61
|
+
'npm/demo-plugin/panel/index.js',
|
|
62
|
+
'npm/demo-plugin/panel/index.json',
|
|
63
|
+
'we-debug-host/index.js',
|
|
64
|
+
'we-debug-host/index.json',
|
|
65
|
+
'we-debug-host/manifest.js'
|
|
66
|
+
]);
|
|
67
|
+
const pluginScript = scripts.find(
|
|
68
|
+
file => file.relative.split(path.sep).join('/') === 'npm/demo-plugin/panel/index.js'
|
|
69
|
+
);
|
|
70
|
+
assert.strictEqual(pluginScript.contents.toString(), 'Component({})');
|
|
71
|
+
const hostJson = scripts.find(file => file.relative.split(path.sep).join('/') === 'we-debug-host/index.json');
|
|
72
|
+
assert.strictEqual(hostJson.base, path.join(root, 'src'));
|
|
73
|
+
console.log('✓ gulp-tool 按扩展名推送 UI 文件');
|
|
74
|
+
})().catch(e => {
|
|
75
|
+
console.error('✗ gulp-tool 按扩展名推送 UI 文件\n', e);
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
});
|
package/src/belongs.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
|
|
3
|
-
export function belongsPage (fullPath: string, pages: string[] = []) {
|
|
4
|
-
const file = path.parse(fullPath);
|
|
5
|
-
return pages.indexOf(path.join(file.dir, file.name)) > -1;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export function belongsApp (fullPath: string, projectDir: string) {
|
|
9
|
-
const file = path.parse(fullPath);
|
|
10
|
-
const app = path.format({ dir: projectDir, name: 'app' });
|
|
11
|
-
return app === path.join(file.dir, file.name);
|
|
12
|
-
};
|
package/src/lookup.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import {
|
|
3
|
-
getAbsolutePath
|
|
4
|
-
} from './path';
|
|
5
|
-
import {
|
|
6
|
-
WechatMiniprogram,
|
|
7
|
-
} from './types';
|
|
8
|
-
|
|
9
|
-
export function lookupPages (projectDir: string) {
|
|
10
|
-
let pages: string[] = [];
|
|
11
|
-
let subpackages: WechatMiniprogram.AppJsonSubpages = [];
|
|
12
|
-
|
|
13
|
-
projectDir = getAbsolutePath(projectDir);
|
|
14
|
-
const appPath = path.format({
|
|
15
|
-
dir: projectDir,
|
|
16
|
-
name: 'app',
|
|
17
|
-
ext: '.json'
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const appJson: WechatMiniprogram.IAppJson = require(appPath);
|
|
22
|
-
|
|
23
|
-
pages = appJson.pages;
|
|
24
|
-
subpackages = appJson.subpackages || appJson.subPackages || [];
|
|
25
|
-
|
|
26
|
-
pages = pages.map(p => {
|
|
27
|
-
return path.resolve(projectDir, p);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
subpackages.forEach(sub => {
|
|
31
|
-
sub.pages.forEach(p => {
|
|
32
|
-
let relativePath = path.join(sub.root, p);
|
|
33
|
-
|
|
34
|
-
pages.push(path.resolve(projectDir, relativePath));
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
} catch (err) {
|
|
38
|
-
throw new Error(err);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return pages;
|
|
42
|
-
};
|
package/src/path.ts
DELETED
package/src/plugin.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import PluginError from 'plugin-error';
|
|
2
|
-
import {
|
|
3
|
-
IRegistration,
|
|
4
|
-
ICompilerPluginOption,
|
|
5
|
-
IPlugin,
|
|
6
|
-
ICompilerPluginPackageOption,
|
|
7
|
-
Meta
|
|
8
|
-
} from './types';
|
|
9
|
-
import File from 'vinyl';
|
|
10
|
-
|
|
11
|
-
const PLUGIN_NAME = '@we-debug/gulp-tool';
|
|
12
|
-
|
|
13
|
-
class Plugin implements IPlugin {
|
|
14
|
-
/** 子插件生命周期 */
|
|
15
|
-
lifecycles: IRegistration[] = [];
|
|
16
|
-
/** 插件元信息 */
|
|
17
|
-
meta: Meta = {
|
|
18
|
-
pages: []
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
getLifecycles() {
|
|
22
|
-
return this.lifecycles;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
register(options: IRegistration) {
|
|
26
|
-
this.lifecycles.push(options);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
execLifecycle(lifetime: keyof IRegistration, file: File) {
|
|
30
|
-
this.lifecycles.forEach(i => {
|
|
31
|
-
const lifecycle = i[lifetime];
|
|
32
|
-
if (typeof lifecycle === 'function') {
|
|
33
|
-
lifecycle.call(this, file);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
loadPlugin(pkg: ICompilerPluginPackageOption) {
|
|
39
|
-
if (typeof pkg === 'string') {
|
|
40
|
-
return require(pkg);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (typeof pkg === 'function') {
|
|
44
|
-
return pkg;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
initPlugin(packges: ICompilerPluginOption[], meta: Meta) {
|
|
49
|
-
this.meta = meta;
|
|
50
|
-
|
|
51
|
-
packges.map(p => {
|
|
52
|
-
try {
|
|
53
|
-
// 加载插件
|
|
54
|
-
let fn = this.loadPlugin(p.package);
|
|
55
|
-
fn.call(this, this, p.options);
|
|
56
|
-
} catch (e) {
|
|
57
|
-
throw new PluginError(PLUGIN_NAME, `load package ${p.package} error: ', ${e}`);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export default new Plugin();
|