Cannot read property 'helpers' of undefined with babel and webapck

痞子三分冷 提交于 2020-01-14 13:34:27

问题


I'm trying to build a Safari extension. Since I can't use require or import in the browser, I am trying to set up Webpack and Babel. I replied heavily on some other examples I found, but no joy. Any help appreciated.

webpack.conf.js

const path = require('path')

module.exports = {
    target: 'web',
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, "ReduxDevTools.safariextension"),
        filename: 'index.js'
    },
    module: {
      rules: [
        {
          test: /src\/.+\.js$/,
          exclude: /(node_modules|ReduxDevTools\.safariextension)/,
          use: {
            loader: 'babel-loader',
            options: {
              "presets": [
                ["@babel/preset-env", {
                  "targets": {
                    "safari": 10
                  }
                }],
                "@babel/preset-stage-0"
              ],
              "plugins": [
                ["@babel/plugin-transform-runtime", {
                  "helpers": false,
                  "polyfill": false,
                  "regenerator": true,
                  "moduleName": "@babel/runtime"
                }]
              ]
            }
          }
        }
      ]
    }
}

package.json

"scripts": {
    "build": "./node_modules/.bin/webpack",
    "dev": "nodemon --watch ./src --exec yarn run build"
  },

Directory

├── CertificateSigningRequest.certSigningRequest
├── ReduxDevTools.safariextension
│   ├── AppIcon.appiconset
│   │   ├── Contents.json
│   │   ├── logo-2-1024.png
│   │   ├── logo-2-128.png
│   │   ├── logo-2-16.png
│   │   ├── logo-2-256.png
│   │   ├── logo-2-32.png
│   │   ├── logo-2-512.png
│   │   └── logo-2-64.png
│   ├── Info.plist
│   ├── Settings.plist
│   ├── index.html
│   └── index.js
├── dist
├── package.json
├── src
│   └── index.js
├── webpack.config.js
├── yarn-error.log
└── yarn.lock

Error

[nodemon] starting `yarn run build`
warning ../package.json: No license field
$ ./node_modules/.bin/webpack
Hash: e42a920b430f30cdd9f4
Version: webpack 3.10.0
Time: 1637ms
   Asset     Size  Chunks             Chunk Names
index.js  4.08 kB       0  [emitted]  main
   [0] ./src/index.js 1.61 kB {0} [built] [failed] [1 error]

ERROR in ./src/index.js
Module build failed: TypeError: Cannot read property 'helpers' of undefined
    at _default (/Users/noah/Projects/ReduxDevtoolsExtension/node_modules/@babel/plugin-transform-runtime/lib/index.js:17:25)
    at Function.memoisePluginContainer (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)
    at Function.normalisePlugin (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
    at /Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
    at Array.map (<anonymous>)
    at Function.normalisePlugins (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
    at OptionManager.mergeOptions (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
    at OptionManager.init (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/noah/Projects/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/noah/Projects/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/noah/Projects/ReduxDevtoolsExtension/node_modules/babel-loader/lib/index.js:50:20)
    at Object.module.exports (/Users/noah/Projects/ReduxDevtoolsExtension/node_modules/babel-loader/lib/index.js:175:20)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[nodemon] app crashed - waiting for file changes before starting...

回答1:


If you're using babel-loader@7, which is what is automatically installed, along with @babel/core beta, try use babel-loader@^8.0.0-beta.0.

yarn add --dev babel-loader@^8.0.0-beta.0



回答2:


You will still receive this with the babel-loader@8, but it's a necessity regardless, since @7 points to babel-core@6. Since the helper error is specific to the transform-runtime plugin running through babel-register, you do need to make sure babel-register is updated as well:

$ yarn add @babel/register

If babel-register doesn't fix your issue, you most likely have another dependency that's pointing to an older version. Without dependencies this will be a trivial process.

Others (including myself) have found that this will also happen when passing babel-register@7 in your scripts. (e.g. node -r babel-register webpack-dev-serv --config webpack.config)

Last but not least you can remove the transform runtime from your babelrc's plugins. I know this isn't ideal, but it is a temporary way to get around this error until Babel@7 is stable with documentation.

I hope that helps you or someone else with this issue, and here's the official issue.



来源:https://stackoverflow.com/questions/47820724/cannot-read-property-helpers-of-undefined-with-babel-and-webapck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!