Skip to content

Commit

Permalink
feat: 支持更好的增量编译
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Apr 18, 2019
1 parent 51ba6a9 commit dd1499d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const chalk = require('chalk');
const { assetType } = require('./consts');

module.exports = class Asset {
Expand Down Expand Up @@ -90,7 +91,7 @@ module.exports = class Asset {
if (targetPath || this.outputFilePath) {
if (this.__content != null) {
if (mpb.hasInit && mpb.isWatch) {
console.log('[watch]:输出文件', this.outputFilePath);
console.log(chalk.cyan('[watching-output]'), this.outputFilePath);
}
// TODO 做一个
// if (this.outputFilePath.includes('/mnt/d/project/mall-wxapp/dist')) {
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ const NpmRewrite = require('./plugin/npmRewrite');
const MinifyPlugin = require('./plugin/minifyPlugin');
const AppJSONPick = require('./plugin/appJSONPick');
const CopyImagePlugin = require('./plugin/copyImagePlugin');
const ProjectConfigPlugin = require('./plugin/projectConfigPlugin.js');
const CopyPlugin = require('./plugin/copyPlugin');
const CleanMbpPlugin = require('./plugin/cleanMbpPlugin.js');
const NodeEnvironmentPlugin = require('./node/NodeEnvironmentPlugin');

class Mpbuilder {
constructor(config) {
this.dest = config.output.path;
this.dest = path.resolve(process.cwd(), config.output.path);
this.src = path.resolve(process.cwd(), config.src);
this.config = config;
this.appEntry = {};
this.cwd = process.cwd();
this.hooks = {
addAsset: new AsyncSeriesBailHook(['asset']),
delAsset: new AsyncSeriesBailHook(['asset']),
start: new AsyncParallelHook(['compiler']),
start: new AsyncParallelHook(['mpb']),
beforeCompile: new AsyncParallelHook(['mpb']),
afterCompile: new AsyncParallelHook(['mpb']),
afterGenerateEntry: new AsyncSeriesBailHook(['afterGenerateEntry']),
Expand Down Expand Up @@ -100,6 +102,7 @@ class Mpbuilder {

async run() {
await this.loaderManager.initRules();
await this.hooks.start.promise(this.mpb);
await this.scan.run();
this.hasInit = true;
}
Expand All @@ -109,3 +112,5 @@ module.exports = Mpbuilder;
module.exports.AppJSONPick = AppJSONPick;
module.exports.CopyPlugin = CopyPlugin;
module.exports.CopyImagePlugin = CopyImagePlugin;
module.exports.ProjectConfigPlugin = ProjectConfigPlugin;
module.exports.CleanMbpPlugin = CleanMbpPlugin;
31 changes: 31 additions & 0 deletions src/plugin/cleanMbpPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Created by ximing on 2019-04-08.
*/
const del = require('del');
const chalk = require('chalk');

module.exports = class CleanMbpPlugin {
constructor(options) {
this.options = Object.assign(
{},
{
path: []
},
options
);
}

apply(mpb) {
mpb.hooks.start.tapPromise('CleanMbpPlugin', async () => {
if (Array.isArray(this.options.path) && this.options.path.length > 0) {
console.log(
chalk.gray('[CleanMbpPlugin]: '),
chalk.blue('删除文件:'),
this.options.path
);
await del(this.options.path);
}
return Promise.resolve();
});
}
};
86 changes: 86 additions & 0 deletions src/plugin/projectConfigPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Created by ximing on 2019-04-08.
*/
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const chalk = require('chalk');

// const Asset = require('../mpb/asset.js');

module.exports = class ProjectConfigPlugin {
constructor(options) {
this.options = Object.assign(
{},
{
projectname: '',
appId: ''
},
options
);
}

apply(mpb) {
mpb.hooks.start.tapPromise('ProjectConfigPlugin', async () => {
// 模拟一个文件出来
const distDir = path.resolve(process.cwd(), mpb.dest);
const projectConfigFile = path.join(distDir, 'project.config.json');
const isExist = fs.existsSync(projectConfigFile);
if (!isExist) {
console.log(
chalk.gray('[ProjectConfigPlugin]: '),
chalk.blue('project.config.json 不存在,重新生成')
);
await fse.outputJson(projectConfigFile, {
description: '项目配置文件',
packOptions: {
ignore: []
},
setting: {
urlCheck: false,
es6: false,
postcss: true,
minified: false,
newFeature: true
},
compileType: 'miniprogram',
libVersion: '2.0.3',
appid: this.options.appId,
projectname: this.options.projectname,
scripts: {
beforeCompile: '',
beforePreview: '',
beforeUpload: ''
},
condition: {
search: {
current: -1,
list: []
},
conversation: {
current: -1,
list: []
},
plugin: {
current: -1,
list: []
},
game: {
list: []
},
miniprogram: {
current: 31,
list: []
}
}
});
} else {
console.log(
chalk.gray('[ProjectConfigPlugin]: '),
chalk.blue('project.config.json 存在,不需要重新生成')
);
}
return Promise.resolve();
});
}
};
11 changes: 11 additions & 0 deletions src/watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const chokidar = require('chokidar');
const _ = require('lodash');
const chalk = require('chalk');
const Watchpack = require('watchpack');
const perf = require('execution-time')();
const { formatBuildTime } = require('./util');
const log = require('./log');

module.exports = class Watching {
constructor(mpb, handle = () => {}) {
Expand All @@ -12,6 +15,7 @@ module.exports = class Watching {
this.handle = handle;
this.watchTimer = Date.now();
this.watcher = null;
this.perfId = 0;
}

watch() {
Expand Down Expand Up @@ -83,8 +87,14 @@ module.exports = class Watching {
this.files = files;
}

generatePerfId() {
return `perf__${this.perfId}`;
}

async handleAsset(path, type) {
console.log(chalk.cyan('[watching-asset]'), path, type);
const perfId = this.generatePerfId();
perf.start(perfId);
const asset = this.mpb.assetManager.getAsset(path);
if (asset) {
if (type === 'change') {
Expand All @@ -97,6 +107,7 @@ module.exports = class Watching {
} else {
console.warn('[watching] 这里不应该 在assetManager里面找不到对应的文件');
}
log.info(`${chalk.green('[success]')},耗时:${formatBuildTime(perf.stop(perfId).time)}`);
const ps = this.pendingPaths;
if (ps.length) {
this.pendingPaths = [];
Expand Down

0 comments on commit dd1499d

Please sign in to comment.