Skip to content

Commit

Permalink
perf(cli): added compiler cache to improve compile speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 16, 2019
1 parent c775031 commit 7baf668
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 284 deletions.
23 changes: 12 additions & 11 deletions packages/cli/core/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class Compile extends Hook {
let absolutePath = path.resolve(filepath);
if (this.involved[absolutePath]) {
this.logger.silly('watch', `Watcher triggered by file changes: ${absolutePath}`);
this.clear('watch').start();
this.start();
}
}
})
Expand All @@ -313,26 +313,27 @@ class Compile extends Hook {
applyCompiler (node, ctx) {
let compiler;

this.assets.add(ctx.file);
ctx.id = this.assets.add(ctx.file);

if (node.lang) {
let compilerOptions = this.options.compilers[node.lang] || [];

/*
if (['css', 'wxss', 'wxml', 'js', 'json'].indexOf(node.lang) > -1) {
let parser = this.parsers[node.type];
node.code = node.content;
return parser.parse(node, ctx);
}*/

let hookKey = 'wepy-compiler-' + node.lang;

if (!this.hasHook(hookKey)) {
throw `Missing plugins ${hookKey}`;
}

this.involved[ctx.file] = 1;
return this.hookUnique(hookKey, node, ctx)
.then(node => {

let task;

if (ctx.useCache && node.compiled) { // If file is not changed, and compiled cache exsit.
task = Promise.resolve(node);
} else {
task = this.hookUnique(hookKey, node, ctx);
}
return task.then(node => {
return this.hookAsyncSeq('before-wepy-parser-' + node.type, { node, ctx });
})
.then(({ node, ctx }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/core/init/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
exports = module.exports = function initParser (ins) {
['wpy', 'script', 'style', 'template', 'config', 'component', 'wxs'].forEach(k => {
['wpy', 'script', 'style', 'template', 'config', 'component', 'wxs', 'file'].forEach(k => {
require('../plugins/parser/' + k).call(ins);
});
};
4 changes: 3 additions & 1 deletion packages/cli/core/plugins/build/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ exports = module.exports = function () {
let t = this.assets.type(file);
let d = this.assets.data(file);

if (t.component && !t.dep) { // If it's a component and it's not a dependences
if (t.npm && t.dep && !t.component) {
// do nothing, they are vendors
} else if (t.component && !t.dep) { // If it's a component and it's not a dependences
// do nothing
} else {
if (!t.url) {
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/core/plugins/helper/sfcCustomBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ exports = module.exports = function () {
if (!sfc.customBlocks || sfc.customBlocks.length === 0)
return sfc;

sfc.customBlocks.forEach(block => {
({sfc, block} = this.hookSeq('sfc-custom-block-' + block.type, {sfc, block}));
sfc.customBlocks = sfc.customBlocks.filter(block => {
let hookKey = 'sfc-custom-block-' + block.type;
let has = this.hasHook(hookKey);
if (has) {
({sfc, block} = this.hookSeq(hookKey, {sfc, block}));
}
return !has;
});

return sfc;
Expand All @@ -15,6 +20,7 @@ exports = module.exports = function () {
if (!sfc.config) {
sfc.config = block;
sfc.config.lang = sfc.config.lang || 'json';
sfc.config.type = 'config';
} else {
this.logger.warn('config', 'mutiple config is defined');
}
Expand All @@ -25,6 +31,7 @@ exports = module.exports = function () {
if (!sfc.wxs)
sfc.wxs = [];
block.lang = block.attrs.lang || 'js';
block.type = 'wxs';
sfc.wxs.push(block);
return {sfc, block};
});
Expand Down
25 changes: 14 additions & 11 deletions packages/cli/core/plugins/parser/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ const loaderUtils = require('loader-utils');

exports = module.exports = function () {
this.register('wepy-parser-config', function (rst, ctx) {

// If file is not changed, then use cache.
if (ctx.useCache && ctx.sfc.config.parsed) {
return Promise.resolve(true);
}

if (!rst) {
return {
ctx.sfc.config.parsed = {
output: {},
components: []
};
return Promise.resolve(true);
}

let configString = rst.content.replace(/^\n*/, '').replace(/\n*$/, '');
Expand All @@ -26,9 +33,10 @@ exports = module.exports = function () {
let componentKeys = Object.keys(config.usingComponents);

if (componentKeys.length === 0) {
return Promise.resolve({
ctx.sfc.config.parsed = {
output: config
});
};
return Promise.resolve(true);
}

let resolvedUsingComponents = {};
Expand Down Expand Up @@ -70,17 +78,12 @@ exports = module.exports = function () {

return Promise.all(plist).then(() => {
config.usingComponents = resolvedUsingComponents;
return {
ctx.sfc.config.parsed = {
output: config,
components: parseComponents
}
};
return true;
});

config.usingComponents = resolvedUsingComponents;
return {
output: config,
components: parseComponents
};
});

this.register('wepy-parser-config-component-module', function (name, prefix, source, target, ctx) {
Expand Down
68 changes: 68 additions & 0 deletions packages/cli/core/plugins/parser/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Tencent is pleased to support the open source community by making WePY available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const loaderUtils = require('loader-utils');

const hashUtil = require('../../util/hash');

exports = module.exports = function () {

this.register('wepy-parser-file', function (node, depFileCtx) {

let file = depFileCtx.file;
let fileContent = fs.readFileSync(file, 'utf-8');
let fileHash = hashUtil.hash(fileContent);

if (this.compiled[file] && fileHash === this.compiled[file].hash) { // 文件 hash 一致,说明文件无修改
depFileCtx = this.compiled[file];
depFileCtx.useCache = true;

return Promise.resolve(depFileCtx);
} else {
this.compiled[file] = depFileCtx;
depFileCtx.hash = fileHash;

let ext = path.extname(file);
this.assets.add(depFileCtx.file, { npm: depFileCtx.npm, dep: depFileCtx.dep, component: depFileCtx.component, type: depFileCtx.type });
if (ext === '.js') {
if (depFileCtx.npm) {
return this.applyCompiler({ type: 'script', lang: 'js', content: fileContent }, depFileCtx);
} else {
return this.applyCompiler({ type: 'script', lang: 'babel', content: fileContent }, depFileCtx);
}
} else {
if (ext === this.options.wpyExt) {
// TODO: why they import a wpy file.
this.hookUnique('error-handler', 'script', {
ctx: depFileCtx,
type: 'error',
message: `Can not import a wepy component, please use "usingComponents" to declear a component`,
title: 'dependence'
}, {
sourcemap: node.compiled.map,
start: dep.loc.start
});
throw new Error('EXIT');
} else {
this.hookUnique('error-handler', 'script', {
ctx: depFileCtx,
type: 'error',
message: `Unrecognized import extension: ${depFileCtx.file}`,
title: 'dependence'
}, {
sourcemap: node.compiled.map,
start: dep.loc.start
});
throw new Error('EXIT');
}
}
}
});
}
Loading

0 comments on commit 7baf668

Please sign in to comment.