简体   繁体   中英

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

I recently got involved in a project which uses Webpack bundler. 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.

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. There's a sample of how to do this in the docs here: 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)
        },
      },
    ],
  },
  // ...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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