Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/cli/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ module.exports = {
### optimization.allowEmptyAttributes
是否允许空属性,默认 `true`,不要改动该配置,除非你清楚自己要做什么:warning:。

## treeShake

- **类型**: `Boolean` default: `false`
- **用法**:

是否开启 `tree-shake` 。只有为 `true` 且非 `watch` 模式下 `tree-shake` 才会生效。

## plugins

目前支持的插件有:
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"@vuepress/theme-default": "^1.5.4",
"lodash": "^4.17.21",
"vue-server-renderer": "^2.6.12"
}
}
3 changes: 2 additions & 1 deletion packages/wxa-cli/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"no-trailing-spaces": "off",
"require-jsdoc": "off",
"camelcase": "warn",
"no-invalid-this": "warn"
"no-invalid-this": "warn",
"linebreak-style": ["off", "windows"]
},
"plugins": []
}
60 changes: 30 additions & 30 deletions packages/wxa-cli/package-lock.json

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

3 changes: 2 additions & 1 deletion packages/wxa-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"promise.prototype.finally": "^3.1.0",
"shelljs": "^0.8.3",
"tapable": "^1.0.0",
"valid-url": "^1.0.9"
"valid-url": "^1.0.9",
"lodash": "^4.17.21"
},
"publishConfig": {
"access": "public"
Expand Down
1 change: 1 addition & 0 deletions packages/wxa-cli/src/const/wxaConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class DefaultWxaConfigs {
allowEmptyAttributes: true,
transformPxToRpx: false,
},
treeShake: false,
};
}
}
62 changes: 61 additions & 1 deletion packages/wxa-cli/src/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import DependencyResolver from './helpers/dependencyResolver';
import ProgressTextBar from './helpers/progressTextBar';
import Preformance from './helpers/performance';
import simplify from './helpers/simplifyObj';
import {treeShake} from './tree-shake/index';

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

Expand Down Expand Up @@ -72,6 +73,7 @@ class Schedule {
// if is mounting compiler, all task will be blocked.
this.$isMountingCompiler = false;
this.progress = new ProgressTextBar(this.current, wxaConfigs);
this.$depJsPending = [];

// save all app configurations for compile time.
// such as global components.
Expand Down Expand Up @@ -120,17 +122,28 @@ class Schedule {

async $doDPA() {
let tasks = [];

while (this.$depPending.length) {
let dep = this.$depPending.shift();

if (this.wxaConfigs.treeShake && !this.cmdOptions.watch && dep.src.endsWith('.js')) {
this.$depJsPending.push(dep);
} else {
tasks.push(this.$parse(dep));
}

// debug('file to parse %O', dep);
tasks.push(this.$parse(dep));
}

let succ = await Promise.all(tasks);

if (this.$depPending.length === 0) {
// dependencies resolve complete
if (this.wxaConfigs.treeShake && !this.cmdOptions.watch) {
let sub = await this.$dojsDPA();
return succ.concat(sub);
}

this.progress.clean();
return succ;
} else {
Expand All @@ -139,6 +152,53 @@ class Schedule {
}
}

async $dojsDPA() {
this.$depPending = this.$depJsPending;
this.$depJsPending = [];
let entries = this.$depPending.map((item)=>({src: item.src, content: item.content}));
console.time('shake');
let contents = treeShake(
{
entry: entries,
resolveSrc: {
root: 'src',
alias: this.wxaConfigs.resolve.alias,
},
commonJS: {
enable: true,
},
}
);
console.timeEnd('shake');

let run = async () => {
let tasks = [];

while (this.$depPending.length) {
let dep = this.$depPending.shift();
let content = contents[dep.src] && contents[dep.src].formattedCode;
if (content) {
dep.content = content;
}

tasks.push(this.$parse(dep));
}

let succ = await Promise.all(tasks);

if (this.$depPending.length === 0) {
// dependencies resolve complete
this.progress.clean();
return succ;
} else {
let sub = await run();
return succ.concat(sub);
}
};

return run();
}

async $parse(dep) {
if (dep.color === COLOR.COMPILED) return dep;
if (dep.color === COLOR.CHANGED) dep.code = void(0);
Expand Down
39 changes: 39 additions & 0 deletions packages/wxa-cli/src/tree-shake/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
// {
// src: '', // 路径
// content: '', // 文件内容(可选)
// },
entry: [],
resolveSrc: {
// '/a/b/c',绝对路径根目录
// 例如src, '/a/b/c' 转换为 /src/a/b/b
root: 'src',
// {'@': 'src'},路径别名
alias: {},
npm: 'node_modules',
},
commonJS: {
enable: false,
// 无法追踪动态引入的模块
// 如果有模块被动态引入,需要在这里设置该模块文件路径
// 将跳过对该文件的 tree shake
dynamicRequireTargets: [],
// 设置 exports 上的哪些属性不会被转换为 esm
// 默认值有 '__esModule'
ingoreKeys: ['__esModule'],
},
parseOptions: {
plugins: [
['decorators', {decoratorsBeforeExport: true}],
'classProperties',
'exportNamespaceFrom',
'exportDefaultFrom',
'objectRestSpread',
],
sourceType: 'unambiguous',
},
generateOptions: {
decoratorsBeforeExport: true,
},
debug: false,
};
Loading