简体   繁体   English

Webpack 4在使用未定义函数时不会引发编译错误

[英]Webpack 4 doesn't throw compilation error on use of undefined functions

I recently got involved in a project which uses Webpack bundler. 我最近参与了一个使用Webpack捆绑器的项目。 While refactoring the code I noticed that the bundler doesn't throw error on use of a function which is not defined. 在重构代码时,我注意到捆绑程序在使用未定义的函数时不会引发错误。

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

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

Here baz is not imported and not defined, my expectations was that the bundler will throw error on baz as undefined, but it didn't. 这里baz未导入且未定义,我的期望是捆绑程序将baz上的错误抛出为undefined,但事实并非如此。

It would be great to identify these cases in the compile time rather than at the runtime. 最好在编译时而不是在运行时识别这些情况。

You'll need to run your code through a loader like eslint then ensure you turn on the no-undef rule. 您需要通过诸如eslint之类加载器运行代码,然后确保启用no-undef规则。 There's a sample of how to do this in the docs here: https://github.com/webpack-contrib/eslint-loader#usage . 在此处的文档中有一个有关如何执行此操作的示例: 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)
        },
      },
    ],
  },
  // ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM