【问题标题】:rollupjs - babelHelpers object not createdrollupjs - 未创建 babelHelpers 对象
【发布时间】:2016-12-05 20:26:35
【问题描述】:

我正在使用 rollup 和 babel 转译一些 ES6 代码并将结果捆绑到一个文件中,但我遇到了 babelHelpers 对象的一些问题。

rollup.config.js:

export default {
  entry: './src/js/core.js',
  dest: './dist/output.js',
  format: 'iife',
  plugins: [babel({
    externalHelpers: true,
    runtimeHelpers: true,
    presets: ["es2015-rollup"]
  })],
  treeshake: false,
  useStrict: true
};

.babelrc:

{
  "presets": ["es2015-rollup"],
  "plugins": [
    "transform-class-properties",
    "transform-es2015-classes",
    "external-helpers-2"
  ]
}

和 package.json 的一部分:

"scripts": {
    "start": "npm-run-all --parallel rollup:watch lint:watch",
    "rollup": "rollup -c",
    "rollup:watch": "rollup -c -w",
    "lint": "esw rollup.config.* src/js/** --color",
    "lint:watch": "npm run lint -- --watch"
},
"dependencies": {
    "babel-helpers": "^6.16.0",
    "babel-plugin-external-helpers": "^6.18.0",
    "babel-plugin-external-helpers-2": "^6.3.13",
    "babel-plugin-transform-class-properties": "^6.19.0",
    "babel-plugin-transform-es2015-classes": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.15.0",
    "babel-polyfill": "^6.16.0",
    "babel-preset-es2015-rollup": "^1.2.0",
    "babel-preset-latest": "^6.16.0",
    "eslint": "^3.11.1",
    "eslint-plugin-import": "^2.2.0",
    "eslint-watch": "^2.1.14",
    "npm-run-all": "^3.1.2",
    "rollup": "^0.36.4",
    "rollup-plugin-babel": "^2.6.1",
    "rollup-plugin-uglify": "^1.0.1",
    "rollup-watch": "^2.5.0"
}

core.js:

var configOptions = {
  range: [0, 5],
  arg: "speak",
  options: ["listen", "mute", "speak"]
};

function updateOptions(opt) {
  if (configOptions.range == undefined) {
    configOptions.range = opt.range;
  }
}

updateOptions({});

class Hello {
  print() {
  }
}

new Hello().print();

捆绑输出:

(function () {
'use strict';

var configOptions = {
  range: [0, 5],
  arg: "speak",
  options: ["listen", "mute", "speak"]
};

function updateOptions(opt) {
  if (configOptions.range == undefined) {
    configOptions.range = opt.range;
  }
}

updateOptions({});

var Hello = function () {
  function Hello() {
    babelHelpers.classCallCheck(this, Hello);
  }

  babelHelpers.createClass(Hello, [{
    key: "print",
    value: function print() {}
  }]);
  return Hello;
}();

new Hello().print();

}());

正如您在 core.js 中看到的,我使用的是 ES6 类。我希望 babel 只为使用过的对象添加 polyfill。由于目前我只使用类,我希望 babel 只使用 checkClassCall 和其他函数来使“类”功能正常工作。但它为我的最终包添加了整个 polyfill 功能。

所以我做了一些研究,发现了“external-helpers-2”和选项: 外部助手:真 runtimeHelpers:true

它在 babelHelpers 中打包了所有 babel 功能,但我不知道如何在我的最终 IIFE 顶部生成具有所有功能的对象。

编辑

如果我使用转换运行时,那么我会在控制台中得到它

将 'babel-runtime/helpers/classCallCheck' 视为外部依赖 将 'babel-runtime/helpers/createClass' 视为外部依赖 options.globa 中没有为外部模块“babel-runtime/helpers/classCallCheck”提供名称 ls – 猜测 '_classCallCheck' 在 options.globals 中没有为外部模块“babel-runtime/helpers/createClass”提供名称 – 猜测'_createClass'

还在最终汇总 IIFE 中将 _classCallCheck 和 _createClass 添加到我的参数中

【问题讨论】:

  • 你尝试安装 babel-plugin-transform-runtime 吗?它应该包含您需要的助手。
  • 我试过了。结果是向我的 IIFE _classCallCheck,_createClass 添加了 2 个参数,但这并不能解决我的问题。这也被添加 _classCallCheck = 'default' 在 _classCallCheck ? _classCallCheck['default'] : _classCallCheck; _createClass = 'default' 在 _createClass 中? _createClass['default'] : _createClass; ...我确实希望 Babel 在我的 IIFE 的顶部插入 classCallCheck 和 createClass 函数,而不是将它们作为外部引用

标签: javascript ecmascript-6 babeljs rollupjs


【解决方案1】:

这是我为汇总配置 babel 的方式,可能对你有用:

{
    presets        : [['es2015', {"modules": false}]], 
    runtimeHelpers : true,
    exclude        : 'node_modules/**',
    plugins        : ["external-helpers"]
}

如果非常很重要,请使用es2015 而不是es2015-rollup

【讨论】:

    【解决方案2】:

    尝试从 Rollup.js 配置中删除 externalHelpersruntimeHelpers 选项。 es2015-rollup preset 已经包含了 helpers,所以只要使用它就足够了。

    更新 Rollup.js 配置:

    export default {
      entry: './src/js/core.js',
      dest: './dist/output.js',
      format: 'iife',
      plugins: [babel({
        presets: ["es2015-rollup"]
      })],
      treeshake: false,
      useStrict: true
    };
    

    【讨论】:

    • 还是不行。包括整个 polyfill ...我只需要一些功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多