rollup 配置

561 阅读1分钟
// 压缩js代码
import { terser } from 'rollup-plugin-terser';
// 将es6编译为es5
import { babel } from '@rollup/plugin-babel';
// 可以告诉 Rollup 如何查找外部模块
import { nodeResolve } from '@rollup/plugin-node-resolve';
// 允许 Rollup 从 JSON 文件中导入数据
import json from 'rollup-plugin-json';
// rollup不支持commonJS,使用该插件可以解析commonJS,若跟plugin-babel一起使用,需要把commonJS放在前面
import commonjs from '@rollup/plugin-commonjs';

const configurePlugins = ({ module }) => {
  return [
    nodeResolve(),
    commonjs(),
    babel({
      presets: [['@babel/preset-env', {
        targets: {
          browsers: ['ie 11'],
        },
      }]],
    }),
    terser({
      module,
      mangle: true,
      compress: true,
    }),
    json()
  ]
}

const configs = [
  {
    input: 'lib/index.js',
    output: {
      format: 'esm',
      file: './dist/npm.esm.js',
    },
    plugins: configurePlugins({ module: false }),
    // 指出哪些模块需要被视为外部引入
    external:['react', 'react-dom']
  },
  {
    input: 'lib/index.js',
    output: {
      format: 'cjs',
      file: './dist/npm.cjs.js',
    },
    plugins: configurePlugins({ module: false }),
    external:['react', 'react-dom']
  }];

export default configs;