无法通过babel和webapck读取未定义的属性'helpers'

时间:2017-12-14 19:24:47

标签: webpack babel safari-extension

我正在尝试构建Safari扩展。由于我无法在浏览器中使用requireimport,因此我尝试设置Webpack和Babel。我对我发现的其他一些例子做了大量的回答,但没有快乐。任何帮助表示赞赏。

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"
  },

目录

├── 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

错误

[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...

2 个答案:

答案 0 :(得分:3)

如果您使用babel-loader@7(自动安装的内容)以及@babel/core测试版,请尝试使用babel-loader@^8.0.0-beta.0

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

答案 1 :(得分:2)

你仍然可以通过babel-loader @ 8获得这个,但无论如何它都是必需的,因为@ 7指向babel-core @ 6。由于辅助错误特定于通过babel-register运行的转换运行时插件,因此您还需要确保更新babel-register:

$ yarn add @babel/register

如果babel-register没有解决您的问题,那么您很可能有另一个指向旧版本的依赖项。如果没有依赖关系,这将是一个微不足道的过程。

其他人(包括我自己)发现在脚本中传递babel-register @ 7时也会发生这种情况。 (例如node -r babel-register webpack-dev-serv --config webpack.config)

最后但并非最不重要的是,您可以从babelrc的插件中删除转换运行时。我知道这不是理想的,但这是一种解决这个错误的临时方法,直到Babel @ 7在文档中保持稳定。

我希望这可以帮助您或其他人解决此问题,而official issue就是这样。