为什么Rollup.js没有树木我的库

时间:2017-05-17 20:15:45

标签: typescript rollupjs tree-shaking

我有一个实用程序库:goodcore 我在另一个项目中使用。它运行正常,但是当我使用Rollup捆绑和树木它时,它总是包含整个goodcore库,即使我只使用了一小部分没有链接的东西也包含了一些包含的文件

这两个项目都是Typescript并使用ES2015模块加载。

我使用:

引用了goodcore库
import { Arr, Pool, Range2, Timer, Util, Vec2 } from "goodcore";

和goodcore中的index.js看起来像:

export { Vec2 as Vec2 } from "./struct/Vec2";
export { Range2 as Range2 } from "./struct/Range2";
export { Rect as Rect } from "./struct/Rect";
export { List as List } from "./struct/List";
export { Dictionary as Dictionary } from "./struct/Dictionary";
export { Stack as Stack } from "./struct/Stack";
export { Tree as Tree } from "./struct/Tree";
export { Calc as Calc } from "./Calc";
export { Dom as Dom } from "./Dom";
export { Arr as Arr } from "./Arr";
export { Obj as Obj } from "./Obj";
export { Util as Util } from "./Util";
export { Timer as Timer } from "./Timer";
export { Uri as Uri } from "./Uri";
export { Poolable as Poolable } from "./standard/mixins/Poolable";
export { Initable as Initable } from "./standard/mixins/Initable";
export { Pool as Pool } from "./standard/Pool";
export { Integrate as Integrate } from "./Integration";
export { MocData as MocData } from "./MocData";
export { Cache as Cache } from "./standard/Cache";
export { KeyValuePair as KeyValuePair } from "./struct/KeyValuePair";
//# sourceMappingURL=index.js.map

我仍然最终得到了来自goodcore的所有东西,比如Tree,包含在我的其他项目的汇总版本中,而我的印象是树木挖掘应该删除未使用/引用的东西。

另一个项目的汇总配置如下所示:

var packageJson = require("./package.json");
import resolve from 'rollup-plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
export default {
    entry: 'src/lib/index.ts',
    targets: [
            { dest: 'dist/' + packageJson.name + '.umd.js', format: 'umd' },
            { dest: 'dist/' + packageJson.name + '.es.js', format: 'es' },
            { dest: 'dist/' + packageJson.name + '.iife.js', format: 'iife' }
    ],
    moduleName: packageJson.name,
    external: ['ts-md5/dist/md5'],
    sourceMap: true,
    globals: {
    },
    treeshake: true,
    plugins: [
        typescript({
            typescript: require('typescript')
        }),
        resolve({ module: true, jsnext: false, main: true, modulesOnly: true })
    ]
}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

为了确保正确性,Rollup将包含任何看起来可能具有副作用的代码 - 给出一个简单的例子,这个包将导致来自foo.js' hello。即使ng-include未导入foo,也要进行记录:

main.js

在goodcore的情况下,所有看起来像Rollup,好像它可能有副作用。例如,/*--- main.js ---*/ import './foo.js'; /*--- foo.js ---*/ export function unused () { // this will be omitted... } // but this will be included, because it has an effect on the world console.log('hello from foo.js'); 是调用Calc的结果,由于很难确定_Calc()是否有效果,因此无论是否有效,都必须包括_Calc()捆绑使用Calc。你可以see this in the REPL

不幸的是,使这个代码可以使用树木可能会对设计产生相当大的改变。 TypeScript也可能引入了非树木结构的构造。