5

使用 Rollup 时,如何让它同时@babel/preset-env与 和一起使用@babel/polyfill?提到要添加的文档,useBuiltIns: 'usage'但是当我这样做时,我require is not defined在控制台中遇到错误。以下是我到目前为止所拥有的;有更推荐的设置吗?

rollup.config.js:

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';

const dist = './dist/';
const name = 'focusoverlay';

export default {
  input: './src/index.js',
  output: [
    {
      file: `${dist}${name}.cjs.js`,
      format: 'cjs'
    },
    {
      file: `${dist}${name}.esm.js`,
      format: 'esm'
    },
    {
      name: 'FocusOverlay',
      file: `${dist}${name}.js`,
      format: 'umd'
    }
  ],
  plugins: [
      resolve(),
      babel({ exclude: 'node_modules/**' }),
      terser()
  ]
};

.babelrc:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage",
        "modules": false,
        "targets": {
          "browsers": "> 0.25%, not op_mini all, not dead, IE 10-11",
          "node": 6
        }
      }
    ]
  ]
}
4

1 回答 1

11

我通过删除我的.babelrc文件并将我的 babel 配置完全移动到rollup.config.js. 然后我还包含了rollup-plugin-commonjs将 CJS 模块转换为 ES6 的插件。我的最终配置示例:

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import { terser } from 'rollup-plugin-terser';

const dist = './dist/';
const name = 'focusoverlay';

export default {
  input: './src/index.js',
  output: [
    {
      file: `${dist}${name}.cjs.js`,
      format: 'cjs'
    },
    {
      file: `${dist}${name}.esm.js`,
      format: 'esm'
    },
    {
      name: 'FocusOverlay',
      file: `${dist}${name}.js`,
      format: 'umd'
    }
  ],
  plugins: [
      resolve(),
      babel({
        exclude: 'node_modules/**',
        presets: [
          [
            '@babel/env',
            {
              modules: 'false',
              targets: {
                browsers: '> 1%, IE 11, not op_mini all, not dead',
                node: 8
              },
              useBuiltIns: 'usage'
            }
          ]
        ]
      }),
      commonjs(),
      terser()
  ]
};

完整配置在这里。当然,仍然欢迎提出改进建议。

于 2019-03-14T22:07:15.243 回答