Skip to content

Commit

Permalink
feat(cli): auto compact injected wxa runtime and node_modules file
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Mar 12, 2020
1 parent 2573239 commit ce2c9f1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
53 changes: 38 additions & 15 deletions packages/wxa-cli/src/compilers/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import path from 'path';
import {readFile} from '../utils';
import logger from '../helpers/logger';
import debugPKG from 'debug';
let parser = require('@babel/parser');

let debug = debugPKG('WXA:ScriptCompiler');
import generator from '@babel/generator';
import {parse} from '@babel/parser';

export default class scriptCompiler {
parse(filepath, code, configs) {
Expand All @@ -14,24 +11,50 @@ export default class scriptCompiler {

let ast;
try {
ast = parser.parse(code, {
plugins: [
['decorators', {decoratorsBeforeExport: true}],
'classProperties',
],
sourceType: 'unambiguous',
...configs,
});
ast = parseESCode(code, [], configs);
} catch (e) {
logger.error('编译失败', e);
return Promise.reject(e);
}

// debug('AST generate %O', ast);

return Promise.resolve({
ast,
kind: 'js',
});
}
}

export function parseESCode(code, plugins = [], options) {
plugins = [
['decorators', {decoratorsBeforeExport: true}],
'classProperties',
'jsx',
'typescript',
'exportNamespaceFrom',
'exportDefaultFrom',
'objectRestSpread',
...plugins,
];

return parse(
code,
{
plugins,
sourceType: 'unambiguous',
...options,
}
);
}

export function generateCodeFromAST(ast, options = {}, mdl) {
let opts = {};
if (mdl.isNodeModule || mdl.isWXARuntime) {
opts.concise = true;
opts.compact = true;
opts.minified = true;
}

opts = {...opts, ...options};

return generator(ast, opts, mdl.content);
}
2 changes: 1 addition & 1 deletion packages/wxa-cli/src/helpers/dependencyResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DependencyResolver {
let source = '';
let ext = '';
// resolve alias;
if (this.resolve.alias && !mdl.isNpm) lib = resolveAlias(lib, this.resolve.alias, mdl.meta.source);
if (this.resolve.alias && !mdl.isNodeModule) lib = resolveAlias(lib, this.resolve.alias, mdl.meta.source);

let pret = new PathParser().parse(lib);

Expand Down
10 changes: 5 additions & 5 deletions packages/wxa-cli/src/resolvers/ast/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import template from '@babel/template';
import generate from '@babel/generator';
import path from 'path';
import DependencyResolver from '../../helpers/dependencyResolver';
import debugPKG from 'debug';
import logger from '../../helpers/logger';
import {generateCodeFromAST} from '../../compilers/script';

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

Expand Down Expand Up @@ -184,7 +184,9 @@ export default class ASTManager {

libs = libs.concat(wxaSourceLibs);
// generate module code.
mdl.code = this.generate(mdl).code;
let {code, map} = this.generate(mdl);
mdl.code = code;
// mdl.sourceMap = map;
delete mdl.ast;
return libs;
}
Expand Down Expand Up @@ -231,8 +233,6 @@ export default class ASTManager {
generate(mdl) {
debug('generate start');
if (mdl.ast == null) return;

// debug('module to generate %O', mdl.ast);
return generate(mdl.ast, {});
return generateCodeFromAST(mdl.ast, {}, mdl);
}
}
7 changes: 5 additions & 2 deletions packages/wxa-cli/src/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Schedule {
this.meta = {
current: this.current,
wxaExt: 'wxa',
nodeModule: path.join(this.current, 'node_modules'),
libSrc: path.join(__dirname, '../lib-dist'),
libs: ['wxa_wrap.js'],
context: path.join(this.current, 'src'),
Expand Down Expand Up @@ -68,7 +69,8 @@ class Schedule {
this.$depPending = []; // pending dependencies
// this.$indexOfModule = [ROOT];
this.$indexOfModule = new Map([['__root__', ROOT]]); // all module
this.$isMountingCompiler = false; // if is mounting compiler, all task will be blocked.
// if is mounting compiler, all task will be blocked.
this.$isMountingCompiler = false;
this.progress = new ProgressTextBar(this.current, wxaConfigs);

// save all app configurations for compile time.
Expand Down Expand Up @@ -295,7 +297,8 @@ class Schedule {
let child = {
...dep,
color: COLOR.INIT,
isNpm: dep.pret.isNodeModule,
isNodeModule: dep.src.indexOf(this.meta.nodeModule) > -1,
isWXARuntime: dep.src.indexOf(this.meta.libSrc) > -1,
isPlugin: dep.pret.isPlugin,
reference: new Map([[mdl.src, mdl]]),
output: new Set([dep.meta.outputPath]),
Expand Down

0 comments on commit ce2c9f1

Please sign in to comment.