Webpack未捕获ReferenceError:未定义babelHelpers

时间:2020-01-24 13:39:26

标签: reactjs typescript react-router babeljs webpack-4

im开发一个React应用程序。对于路由,我使用react-router-dom和typesafe-react-router。我的项目是使用webpack 4和babel设置的。为了使用类型安全路由器,我定义了路由。

routes.ts

import { route } from 'typesafe-react-router';

export enum RouteNames {
  HOME = 'HOME',
  ABOUT = 'ABOUT'
}

export const Routes = {
  [RouteNames.HOME]: route(''),
  [RouteNames.ABOUT]: route('about')
};

该路由还用于其他文件,例如App.tsx

    <div className="AppContainer">
      <Router>
        <h1>My React App</h1>
        <img src={logo} />
        <div className="NavLink">
          <Link to={Routes[RouteNames.HOME].create({})}>Home</Link>
          {' | '}
          <Link to={Routes[RouteNames.ABOUT].create({})}>About</Link>
        </div>

        <Switch>
          <Route
            path={Routes[RouteNames.HOME].template()}
            exact
            component={HomePage}
          />
          <Route
            path={Routes[RouteNames.ABOUT].template()}
            component={AboutPage}
          />
          <Route component={NotFoundPage} />
        </Switch>
      </Router>
    </div>

在编译过程中一切都很好,但是出现运行时错误:

routes.ts:8 Uncaught ReferenceError: babelHelpers is not defined
    at Module../src/pages/routes.ts (routes.ts:8)
    at __webpack_require__ (bootstrap:726)
    at fn (bootstrap:100)
    at Module../src/pages/index.ts (index.ts:1)
    at __webpack_require__ (bootstrap:726)
    at fn (bootstrap:100)
    at Module../src/app/App.tsx (App.tsx:1)
    at __webpack_require__ (bootstrap:726)
    at fn (bootstrap:100)
    at Module../src/bootstrap/index.tsx (index.tsx:1)

这是我的.babelrc文件:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ],
  "plugins": [
    "@babel/plugin-external-helpers",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-object-rest-spread"
  ]
}

这是我的webpack.config.js

const path = require('path');
const { CheckerPlugin } = require('awesome-typescript-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/bootstrap/index.tsx',
  devtool: 'source-map',
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.less']
  },
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'bundle.min.js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|tsx|ts)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'less-loader',
            options: {
              javascriptEnabled: true
            }
          }
        ]
      },
      {
        test: /\.(png|jpg|gif|svg)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 10000
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new CheckerPlugin(),
    new HtmlWebpackPlugin({
      template: './src/bootstrap/index.html'
    })
  ]
};

我认为mybee的问题是babel编译器对导出的const枚举有问题。有没有办法使打字稿的语言功能与babel一起使用?

2 个答案:

答案 0 :(得分:1)

删除@babel/plugin-external-helpers,然后使用@babel/plugin-transform-runtime为您加载帮助程序。前一个插件希望您提供帮助程序,就我所见,您不是。

答案 1 :(得分:1)

您似乎正在使用@babel/plugin-external-helpers。这样可以防止Babel在每个文件的顶部添加所需的帮助程序功能。

您可以read more about the plugin in the Babel docs

要解决您的问题,我可以将插件替换为@babel/plugin-transform-runtime或手动安装所需的帮助程序。

此外,值得在GitHub上检查相关问题: