【发布时间】:2019-07-20 17:13:00
【问题描述】:
我想就如何构建一个易于使用且经过摇树优化的构建获得建议。我正在使用 rollup 来打包一个由多个组件组成的 UI 库。
我的架构是:
/src
/index.js
/components
/index.js
/component1
/index.js
/Comp.vue
...
/componentN
/index.js
/Comp.vue
/directives
/index.js
/directive1
/index.js
...
/directiveN
/index.js
src/components/index.js 看起来像
export { default as Comp1 } from './component1
...
export { default as CompN } from './componentN
src/directives/index.js 看起来像
export { default as Directive1 } from './directive1
...
export { default as DirectiveN } from './directiveN
每个内部index.js只是为了方便的绑定,比如
import Comp from './Comp.vue'
export default Comp`
最后src/index.js 将收集所有内容:
import { * as components } from './components'
import { * as directives } from './directives'
export { components, directives }
构建时,汇总配置如下所示:
{
input: 'src/index.js',
output: {
file: 'dist/lib.esm.js',
format: 'esm',
}
(当然我要避免所有编译丑化插件,我认为它们会成为这个问题的噪音)
所以这个构建看起来不错,并且有效,但是......
- 使用非常不方便:
import { components } from 'lib'
const { Comp1 } = components
- 这种结构在使用时也可能会破坏树抖动,因为我们导入了完整的
components对象,而只需要Comp1。
我知道我不应该是关心摇树的人,而是提供一个支持摇树的库,这就是它的意义所在。在使用最简单的@vue/cli 模板测试我的构建时,完整的库被导入,甚至@vue/cli 声称已经启用了开箱即用的 webpack-treeshaking 功能。
我不介意构建单独的文件而不是一个大的 esm 构建,但据我所知,一个文件构建与树摇动是可能的。我对构建单独文件的恐惧是CompA 可能在内部需要CompB,如果用户还需要CompB,在这种情况下它可能会在构建中复制(例如,一个外部使用版本和一个内部使用版本)。
我不知道如何进行优化。任何指针都非常受欢迎。
【问题讨论】:
标签: javascript vue.js rollup