Skip to content

Commit

Permalink
Merge pull request #35 from dlhandsome/feat-plugin
Browse files Browse the repository at this point in the history
feat(gulp-tool): support plugins
  • Loading branch information
dlhandsome authored Nov 1, 2023
2 parents 9c521c1 + 7eb8e97 commit 414de6d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/compiler/gulp-tool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const { lookupPages } = require('./lib/lookup');
const { belongsPage, belongsApp } = require('./lib/belongs');
const { getAbsolutePath } = require('./lib/path');
const injectComponentId = require('./lib/inject-component-id');

const PLUGIN_NAME = '@we-debug/gulp-tool';
const plugin = require('./lib/plugin');
const { PLUGIN_NAME } = require('./lib/constants');

let pages = [];

Expand All @@ -20,6 +20,7 @@ function mpGlobalComp(options = {}) {
let compName = options.compName || 'we-debug';
let compPath = options.compPath || '@we-debug/core/component/index/index';
let entryFile = options.entryFile || 'we-debug/index.js';
const plugins = options.plugins || [];

baseDir = getAbsolutePath(baseDir);
const isModule = compPath.indexOf('@we-debug/core') === 0;
Expand All @@ -39,12 +40,18 @@ function mpGlobalComp(options = {}) {
function init() {
return through.obj((file, encoding, callback) => {
if (!mpGlobalComp.init) {
// 执行 beforeInit 插件钩子
plugin.execLifecycle('beforeInit', file);

try {
pages = lookupPages(baseDir);
plugin.initPlugin(plugins, { ...options, pages });
} catch (e) {
throw new PluginError(PLUGIN_NAME, 'look up pages failed, please check your configure');
}
mpGlobalComp.init = true;
// 执行 onInit 插件钩子
plugin.execLifecycle('onInit', file);
}
callback(null, file);
});
Expand All @@ -56,6 +63,8 @@ function mpGlobalComp(options = {}) {

// 如果不是 wxml 文件, 则跳过
if (file.extname !== '.wxml') return callback(null, file);
// 执行 wxml 插件钩子
plugin.execLifecycle('wxml', file);

// 如果命中filter,则跳过
if (filter) {
Expand All @@ -72,11 +81,15 @@ function mpGlobalComp(options = {}) {

// 如果属于 Page
if (belongsPage(file.path, pages)) {
// 插入 we-debug 片段之前执行
plugin.execLifecycle('beforeInsertWxml', file);
let code = file.contents.toString();

code += wxmlRaw;

file.contents = Buffer.from(code);
// 插入 we-debug 片段之后执行
plugin.execLifecycle('afterInsertWxml', file);
}

callback(null, file);
Expand Down Expand Up @@ -105,6 +118,8 @@ function mpGlobalComp(options = {}) {

// 如果不是 wxml 文件, 则跳过
if (file.extname !== '.json') return callback(null, file);
// 检测到项目 json 文件后立即执行
plugin.execLifecycle('json', file);

// 如果是 app.json
if (belongsApp(file.path, baseDir)) {
Expand Down Expand Up @@ -141,13 +156,19 @@ function mpGlobalComp(options = {}) {

// 如果不是 JS 文件, 则跳过
if (file.extname !== '.js') return callback(null, file);
// 检测到项目 js 文件后立即执行
plugin.execLifecycle('script', file);

// 如果是 app.js
if (belongsApp(file.path, baseDir) || file.relative === 'app.js') {
// app.js 插入 we-debug import 片段前执行
plugin.execLifecycle('beforeInsertScript', file);
let code = file.contents.toString();

code = `require('${entryFile}')\n` + code;
file.contents = Buffer.from(code);
// app.js 插入 we-debug import 片段后执行
plugin.execLifecycle('afterInsertScript', file);
}

callback(null, file);
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/gulp-tool/lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.PLUGIN_NAME = '@we-debug/gulp-tool';
55 changes: 55 additions & 0 deletions packages/compiler/gulp-tool/lib/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const PluginError = require('plugin-error');

const PLUGIN_NAME = '@we-debug/gulp-tool';

class Plugin {
/** 子插件生命周期 */
lifecycles = [];
/** 插件元信息 */
meta = {};

getLifecycles () {
return this.lifecycles;
}

register (options) {
this.lifecycles.push(options);
}

execLifecycle (lifetime, file) {
this.lifecycles.forEach(i => {
if (typeof i[lifetime] === 'function') {
i[lifetime].call(this, file);
}
})
}

loadPlugin(pkg) {
const pkgType = typeof pkg;

switch(pkgType) {
case 'function':
// 函数直接返回
return pkg;
case 'string':
// 字符串识别为 npm 包
return require(pkg);
}
}

initPlugin (packges, meta) {
this.meta = meta;

packges.map(p => {
try {
// 加载插件
let fn = this.loadPlugin(p.package);
fn.call(this, this, p.options);
} catch (e) {
throw new PluginError(PLUGIN_NAME, `load package ${p.package} error,please run 'npm install ${p.package}', ${e}`);
}
})
}
}

module.exports = new Plugin();

0 comments on commit 414de6d

Please sign in to comment.