Skip to content

Commit

Permalink
feat(cli): split module into subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Apr 17, 2019
1 parent 43143cf commit c5c9a69
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 190 deletions.
2 changes: 1 addition & 1 deletion packages/wxa-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 25 additions & 33 deletions packages/wxa-cli/src/builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {readFile, applyPlugins, isFile} from './utils';
import {readFile, applyPlugins, isFile, getHash} from './utils';
import path from 'path';
import fs, {unlink} from 'fs';
import crypto from 'crypto';
import fs from 'fs';
import chokidar from 'chokidar';
import globby from 'globby';
import debugPKG from 'debug';
Expand All @@ -16,6 +15,7 @@ import {AsyncParallelHook, SyncBailHook, AsyncSeriesHook} from 'tapable';
import DependencyResolver from './helpers/dependencyResolver';
import root from './const/root';
import ProgressTextBar from './helpers/progressTextBar';
import color from './const/color';

let debug = debugPKG('WXA:Builder');
class Builder {
Expand Down Expand Up @@ -67,17 +67,17 @@ class Builder {
return this.loader.mount(this.wxaConfigs.use, cmd);
}

filterModule(arr) {
return arr.reduce((ret, dep)=>{
filterModule(indexedMap) {
let ret = [];
indexedMap.forEach((dep)=>{
if (
!/src\/_wxa/.test(dep.src)
) {
ret.push(dep.src);
}
});


return ret;
}, []);
return ret;
}

watch(cmd) {
Expand Down Expand Up @@ -105,22 +105,21 @@ class Builder {

logger.warn('change', filepath);
debug('WATCH file changed %s', filepath);
let mdl = this.schedule.$indexOfModule.find((module)=>module.src===filepath);
let mdl = this.schedule.$indexOfModule.get(filepath);
let isChange = true;
debug('Changed Module %O', mdl);
// module with code;
if (!mdl.isFile) {
let content = readFile(mdl.src);
debug('changed content %s', content);
let md5 = crypto.createHash('md5').update(content).digest('hex');

mdl.content = content;
mdl.code = void(0);
isChange = mdl.hash !== md5;
debug('OLD HASH %s, NEW HASH %s', mdl.hash, md5);
let hash = getHash(filepath);
isChange = mdl.hash !== hash;
debug('OLD HASH %s, NEW HASH %s', mdl.hash, hash);
}

if (isChange) {
mdl.color = color.CHANGED;
mdl.content = void(0);
mdl.code = void(0);

let changedDeps;
try {
this.schedule.$depPending.push(mdl);
Expand All @@ -129,8 +128,6 @@ class Builder {

changedDeps = await this.schedule.$doDPA();
await this.optimizeAndGenerate(changedDeps, cmd);
// logger.log('Done', '编译完成');
// debug('schedule dependencies Tree is %O', this.schedule.$indexOfModule);
} catch (e) {
logger.error('编译失败', e);
}
Expand Down Expand Up @@ -217,14 +214,7 @@ class Builder {
await this.schedule.doDPA();

this.schedule.perf.show();
debug('schedule dependencies Tree is %O', this.schedule.$indexOfModule.map((item)=>{
delete item.ast;
delete item.xml;

if (item.reference) delete item.reference;

return item;
}));
debug('schedule dependencies Tree is %O', this.schedule.$indexOfModule);

await this.optimizeAndGenerate(this.schedule.$indexOfModule, cmd);

Expand All @@ -244,25 +234,27 @@ class Builder {
* optimize all module in list and generate dest file.
* optiming and generating is parallel.
*
* @param {Array<Object>} list
* @param {Array<Object>} indexedMap
* @param {Object} cmdOptions cmd options
*/
async optimizeAndGenerate(list, cmdOptions) {
async optimizeAndGenerate(indexedMap, cmdOptions) {
try {
// module optimize, dependencies merge, minor.
let optimizer = new Optimizer(this.current, this.wxaConfigs, cmdOptions);
applyPlugins(this.schedule.wxaConfigs.plugins, optimizer);

let optimizeTasks = list.map((dep)=>{
return optimizer.do(dep);
let optimizeTasks = [];
indexedMap.forEach((dep)=>{
optimizeTasks.push(optimizer.do(dep));
});

await Promise.all(optimizeTasks);

// write module to dest, dependencies copy.
let generator = new Generator(this.current, this.schedule.meta, this.wxaConfigs, cmdOptions);
let generateTasks = list.map((mdl)=>{
return generator.do(mdl);
let generateTasks = [];
indexedMap.forEach((mdl)=>{
generateTasks.push(generator.do(mdl));
});

await Promise.all(generateTasks);
Expand Down
20 changes: 16 additions & 4 deletions packages/wxa-cli/src/compilers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ const jsOptions = {
* @class EmptyCompiler
*/
export default class Compiler {
constructor(resolve, meta, appConfigs) {
constructor(resolve, meta, appConfigs, scheduler) {
this.current = meta.current;
this.configs = {};
this.resolve = resolve;
this.meta = meta;
this.appConfigs = appConfigs;

this.$scheduer = scheduler;
}

destroy() {
this.appConfigs = null;
this.$scheduer = null;
}

async parse(mdl) {
Expand Down Expand Up @@ -131,8 +137,8 @@ export default class Compiler {
}

default: {
// return Promise.reject(`未识别的文件类型%{type}, 请检查是否添加指定的loader`);
return Promise.resolve({kind: 'other'});
// unknown type, can be define by other compiler.
return Promise.resolve({kind: mdl.sourceType || 'other'});
}
}
}
Expand Down Expand Up @@ -184,6 +190,12 @@ export default class Compiler {
children = new ComponentManager(this.resolve, this.meta, this.appConfigs).parse(mdl);
}

if (mdl.src === this.$scheduer.APP_CONFIG_PATH) {
// global components in wxa;
// delete custom field in app.json or wechat devtool will get wrong.
delete mdl.json['wxa.globalComponents'];
}


mdl.code = JSON.stringify(mdl.json, void(0), 4);
return children;
Expand Down
3 changes: 2 additions & 1 deletion packages/wxa-cli/src/const/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default {
kind: 'root',
isAbstract: true,
isROOT: true,
childNodes: [],
childNodes: new Map(),
package: '', // 主包
};
53 changes: 33 additions & 20 deletions packages/wxa-cli/src/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {writeFile, getDistPath, readFile, copy} from '../utils';
import debugPKG from 'debug';
import DependencyResolver from '../helpers/dependencyResolver';
import ProgressBar from '../helpers/progressTextBar';
import logger from '../helpers/logger';

let debug = debugPKG('WXA:Generator');

Expand All @@ -24,35 +25,47 @@ export default class Generator {
this.progress.draw(text, 'Generating', !this.cmdOptions.verbose);

debug('module to generate %O', mdl);
let outputPath;
if (!mdl.meta || !mdl.meta.outputPath) {
let dr = new DependencyResolver(this.resolve, this.meta);
outputPath = dr.getOutputPath(mdl.src, mdl.pret, mdl);
} else {
outputPath = mdl.meta.outputPath;
// let outputPath;
// if (!mdl.meta || !mdl.meta.outputPath) {
// let dr = new DependencyResolver(this.resolve, this.meta);
// outputPath = dr.getOutputPath(mdl.src, mdl.pret, mdl);
// } else {
// outputPath = mdl.meta.outputPath;
// }
if (!mdl.output) {
logger.errors('module 数据结构有问题', mdl);
return;
}

outputPath = this.tryTransFormExtension(outputPath);
debug('transform ext %s', outputPath);
mdl.meta.accOutputPath = outputPath;

if (mdl.isFile) {
copy(mdl.src, outputPath);
} else {
writeFile(outputPath, mdl.code);
}
mdl.output.forEach((info, outputPath)=>{
outputPath = this.tryTransFormExtension(outputPath, info.reference.kind);
debug('transform ext %s', outputPath);
info.reality = outputPath;
if (mdl.isFile) {
copy(mdl.src, outputPath);
} else {
writeFile(outputPath, mdl.code);
}
});
}

tryTransFormExtension(output) {
tryTransFormExtension(output, kind) {
if (this.wxaConfigs.target === 'wxa') {
// 小程序相关
let opath = path.parse(output);

let ext;
switch (opath.ext) {
case '.css': ext = '.wxss'; break;
case '.xml': ext = '.wxml'; break;
case '.ts': ext = '.js'; break;
switch (kind || opath.ext) {
case '.css':
case 'css':
ext = '.wxss'; break;
case '.xml':
case 'xml':
ext = '.wxml'; break;
case '.ts':
case 'ts':
case 'typescript':
ext = '.js'; break;
default: ext = opath.ext;
}

Expand Down
20 changes: 10 additions & 10 deletions packages/wxa-cli/src/helpers/dependencyResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class DependencyResolver {
getOutputPath(source, pret, mdl) {
if (pret.isRelative || pret.isAPPAbsolute || pret.isNodeModule || pret.isWXALib) {
let opath = pret.isWXALib ?
path.parse(path.join(this.meta.context, '_wxa', pret.name+pret.ext)) :
path.parse(source);
path.parse(path.join(this.meta.context, '_wxa', pret.name+pret.ext)) :
path.parse(source);

return this.getDistPath(opath);
} else if (pret.isPlugin || pret.isURI) {
Expand All @@ -124,7 +124,7 @@ class DependencyResolver {
let fileOutputPath = (
mdl.meta &&
mdl.meta.outputPath ||
this.getDistPath(path.parse(mdl.src))
this.getDistPath(path.parse(mdl.src), mdl)
);

resolved = './'+path.relative(path.parse(fileOutputPath).dir, libOutputPath);
Expand All @@ -145,18 +145,18 @@ class DependencyResolver {
return content;
}

getDistPath(opath) {
getDistPath(absPath, mdl) {
let relative;
opath = typeof opath === 'string' ? path.parse(opath) : opath;
absPath = typeof absPath === 'string' ? path.parse(absPath) : absPath;

if (path.relative(this.meta.current, opath.dir).indexOf('node_modules') === 0) {
relative = path.relative(path.join(this.meta.current, 'node_modules'), opath.dir);
relative = path.join('npm', relative);
if (path.relative(this.meta.current, absPath.dir).indexOf('node_modules') === 0) {
relative = path.relative(path.join(this.meta.current, 'node_modules'), absPath.dir);
relative = path.join(mdl.package, 'npm', relative);
} else {
relative = path.relative(this.meta.context, opath.dir);
relative = path.relative(this.meta.context, absPath.dir);
}

return path.join(this.meta.output.path, relative, opath.base);
return path.join(this.meta.output.path, relative, absPath.base);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/wxa-cli/src/resolvers/ast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default class ASTManager {
src: source,
pret: pret,
meta: {
source, outputPath,
source, outputPath, resolved,
},
});

Expand Down Expand Up @@ -150,6 +150,7 @@ export default class ASTManager {
libs = libs.concat(wxaSourceLibs);
// generate module code.
mdl.code = this.generate(mdl).code;
delete mdl.ast;
return libs;
}
/**
Expand Down
Loading

0 comments on commit c5c9a69

Please sign in to comment.