Skip to content

Commit c001891

Browse files
deepfuncGcaufy
authored andcommitted
refactor(cli): 优化 watch 时的编译效率
1 parent 0546088 commit c001891

File tree

2 files changed

+78
-41
lines changed

2 files changed

+78
-41
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

packages/cli/core/compile.js

+69-41
Original file line numberDiff line numberDiff line change
@@ -168,64 +168,85 @@ class Compile extends Hook {
168168
return initCompiler(this, this.options.compilers);
169169
}
170170

171-
start () {
171+
start (files) {
172172
if (this.running) {
173173
return;
174174
}
175+
175176
this.running = true;
177+
this.logger.info('build', 'start...');
178+
let parseComps;
179+
180+
if (files === undefined || files.length === 0) {
181+
// if files is empty, compile from entry
182+
parseComps = this.hookUnique('wepy-parser-wpy',
183+
{ path: this.options.entry, type: 'app' }).then(app => {
184+
let sfc = app.sfc;
185+
let script = sfc.script;
186+
let styles = sfc.styles;
187+
let config = sfc.config;
188+
189+
let appConfig = config.parsed.output;
190+
if (!appConfig.pages || appConfig.pages.length === 0) {
191+
appConfig.pages = [];
192+
this.hookUnique('error-handler', {
193+
type: 'warn',
194+
ctx: app,
195+
message: `Missing "pages" in App config`
196+
});
197+
}
198+
let pages = appConfig.pages.map(v => {
199+
return path.resolve(app.file, '..', v);
200+
});
176201

177-
this.hookUnique('wepy-parser-wpy', { path: this.options.entry, type: 'app' }).then(app => {
202+
if (appConfig.subPackages || appConfig.subpackages) {
203+
(appConfig.subpackages || appConfig.subPackages).forEach(sub => {
204+
sub.pages.forEach(v => {
205+
pages.push(path.resolve(app.file, '../'+sub.root || '', v));
206+
});
178207

179-
let sfc = app.sfc;
180-
let script = sfc.script;
181-
let styles = sfc.styles;
182-
let config = sfc.config;
208+
});
209+
}
183210

184-
let appConfig = config.parsed.output;
185-
if (!appConfig.pages || appConfig.pages.length === 0) {
186-
appConfig.pages = [];
187-
this.hookUnique('error-handler', {
188-
type: 'warn',
189-
ctx: app,
190-
message: `Missing "pages" in App config`
191-
});
192-
}
193-
let pages = appConfig.pages.map(v => {
194-
return path.resolve(app.file, '..', v);
195-
});
211+
let tasks = pages.map(v => {
212+
let file;
196213

197-
if (appConfig.subPackages || appConfig.subpackages) {
198-
(appConfig.subpackages || appConfig.subPackages).forEach(sub => {
199-
sub.pages.forEach(v => {
200-
pages.push(path.resolve(app.file, '../'+sub.root || '', v));
214+
file = v + this.options.wpyExt;
215+
if (fs.existsSync(file)) {
216+
return this.hookUnique('wepy-parser-wpy', { path: file, type: 'page' });
217+
}
218+
file = v + '.js';
219+
if (fs.existsSync(file)) {
220+
return this.hookUnique('wepy-parser-component', { path: file, type: 'page', npm: false });
221+
}
222+
this.hookUnique('error-handler', {
223+
type: 'error',
224+
ctx: app,
225+
message: `Can not resolve page: ${v}`
201226
});
202-
203227
});
204-
}
205-
206-
let tasks = pages.map(v => {
207-
let file;
208228

209-
file = v + this.options.wpyExt;
229+
this.hookSeq('build-app', app);
230+
this.hookUnique('output-app', app);
231+
return Promise.all(tasks);
232+
})
233+
} else {
234+
// else just compile these
235+
const tasks = files.map(file => {
210236
if (fs.existsSync(file)) {
211237
return this.hookUnique('wepy-parser-wpy', { path: file, type: 'page' });
212238
}
213-
file = v + '.js';
214-
if (fs.existsSync(file)) {
215-
return this.hookUnique('wepy-parser-component', { path: file, type: 'page', npm: false });
216-
}
217239
this.hookUnique('error-handler', {
218240
type: 'error',
219-
ctx: app,
220-
message: `Can not resolve page: ${v}`
241+
ctx: {},
242+
message: `Can not resolve page: ${file}`
221243
});
222244
});
223245

224-
this.hookSeq('build-app', app);
225-
this.hookUnique('output-app', app);
226-
return Promise.all(tasks);
227-
}).then(comps => {
246+
parseComps = Promise.all(tasks);
247+
}
228248

249+
parseComps.then(comps => {
229250
function buildComponents (comps) {
230251
if (!comps) {
231252
return null;
@@ -257,6 +278,7 @@ class Compile extends Hook {
257278
return null;
258279
}
259280
}
281+
260282
return buildComponents.bind(this)(comps);
261283
}).then(() => {
262284
let vendorData = this.hookSeq('build-vendor', {});
@@ -269,7 +291,7 @@ class Compile extends Hook {
269291
}).then(() => {
270292
this.hookSeq('process-done');
271293
this.running = false;
272-
this.logger.info('process finished');
294+
this.logger.info('build', 'finished');
273295
if (this.options.watch) {
274296
this.logger.info('watching...');
275297
this.watch();
@@ -320,7 +342,13 @@ class Compile extends Hook {
320342
}
321343
if (involvedFile) {
322344
this.logger.silly('watch', `Watcher triggered by file changes: ${absolutePath}`);
323-
this.start();
345+
if (absolutePath !== this.options.entry &&
346+
path.extname(absolutePath) === this.options.wpyExt) {
347+
// if changed file is wpyExt and is not entry, just compile it
348+
this.start([absolutePath]);
349+
} else {
350+
this.start();
351+
}
324352
}
325353
}
326354
})
@@ -432,4 +460,4 @@ exports = module.exports = (program) => {
432460
let opt = parseOptions.convert(program);
433461

434462
return new Compile(opt);
435-
}
463+
};

0 commit comments

Comments
 (0)