Webpack 不注入变量

Webpack not injecting vars

本文关键字:变量 注入 Webpack      更新时间:2023-09-26

我正在使用got作为npm包并将其导入到我的一个组件中。当我在 Safari 中通过 Webpack 运行所有内容时,它会说:

SyntaxError: Unexpected keyword 'const'. Const declarations are not supported in strict mode.

仔细观察后,绝对是got包没有将他们的const转换为var

我真的不确定要检查什么才能解决此问题。

通过做一些研究,我遇到了提出的解决方案,例如:

  • --harmony 标志添加到我的构建命令
  • 让我的.babelrc预设"presets": ["es2015", "react", "stage-0"]读取
  • 已排除并将我的node_modules包含在 webpack 中的 JavaScript 加载器中

所有这些都没有帮助。

加载器示例:

{
  test: /'.js$/,
  loaders: ['babel'],
  exclude: /node_modules/, // also tried commenting out this line
  include: path.join(__dirname, 'src')
},

由于您包含src文件夹(我相信它是node_modules文件夹的兄弟姐妹),因此添加或评论exclude: /node_modules/没有区别。

如果需要处理特定的已安装模块,可以使用以下加载器配置。github上的问题。

{
  test: /'.js$/,
  loader: 'babel',
  include: [
    path.join(__dirname, 'src'),
    path.join(__dirname, 'node_modules', 'got')
  ]
},