【问题标题】:Webpack 4 doesn't throw compilation error on use of undefined functionsWebpack 4 在使用未定义函数时不会抛出编译错误
【发布时间】:2020-01-04 15:50:35
【问题描述】:

我最近参与了一个使用 Webpack bundler 的项目。在重构代码时,我注意到捆绑器不会在使用未定义的函数时抛出错误。

import { foo } from './foo.js';

foo('hi');
baz('test');

这里的 baz 没有导入也没有定义,我的期望是捆绑器会在 baz 上抛出错误为未定义,但它没有。

最好在编译时而不是在运行时识别这些情况。

【问题讨论】:

    标签: javascript node.js webpack babeljs webpack-4


    【解决方案1】:

    您需要通过loader like eslint 运行您的代码,然后确保启用no-undef 规则。此处的文档中有一个如何执行此操作的示例:https://github.com/webpack-contrib/eslint-loader#usage

    module.exports = {
      // ...
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'eslint-loader',
            options: {
              // eslint options (if necessary)
            },
          },
        ],
      },
      // ...
    };
    

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 2019-10-25
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多