【发布时间】:2020-02-14 19:15:04
【问题描述】:
使用 lodash-es,我们可以像这样挑选模块:import drop from 'lodash/drop'; 这样只有 drop 被使用,没有别的。
如何使用 Rollup 实现相同的目标?
我正在配置一个 react 和 typescript ui 库以用于两个 create-react-app 项目。我了解到使用 esm 最适合我的用例,它是使用 Rollup 实现 treeshaking 的最有效方式。但是,当我使用我的库时,我是这样导入的:import { someComponent }from 'my-ui-library'; 是不是像上面的cherry-pick 方法一样挑出someComponent。语法重要吗?
这是我的rollup.config.js:
import babel from 'rollup-plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import svgr from '@svgr/rollup';
import url from '@rollup/plugin-url';
import json from '@rollup/plugin-json';
import external from 'rollup-plugin-peer-deps-external';
import includePaths from 'rollup-plugin-includepaths';
const pkg = require('./package.json');
export default {
cache: false,
input: 'src/index.ts',
output: [{ file: pkg.module, format: 'esm' }],
treeshake: true,
plugins: [
external({
preferBuiltins: false,
}),
babel({
exclude: /node_modules/,
plugins: ['external-helpers'],
externalHelpers: true, // Exclude babel helpers.
}),
json(),
resolve(),
svgr({
ref: true,
svgo: false,
}),
typescript({
clean: true,
typescript: require('typescript'),
}),
url(),
includePaths({
paths: ['src'],
}),
],
};
【问题讨论】:
标签: reactjs typescript configuration rollup rollupjs