汇总+ @ babel / preset-env + @ babel / polyfill

时间:2019-03-11 23:48:45

标签: javascript babel rollup babel-polyfill babel-preset-env

使用汇总时,如何使它与@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
        }
      }
    ]
  ]
}

1 个答案:

答案 0 :(得分:0)

我通过删除.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()
  ]
};

完整配置here。仍然欢迎您提出改进建议。