Captainlonate
Captainlonate

Reputation: 5008

Webpack 4.x does not tell me when an import is undefined

Using Webpack 4.12

If I attempt to import something from a module, where the module exists, but the thing I'm trying import doesn't, webpack makes no mention of it.

For instance, if I try to do either one of these (either with harmony or commonjs):

import {shouldnotwork} from 'core-js';
const {shouldnotwork} = require('core-js');

Then, Webpack just carries on like there is no issue. In these cases, shouldnotwork will be undefined.

Meanwhile, in my huge site with tons of files, all of the pages and features that use that component will simply not work, at runtime. Naturally I won't realize this unless I actually try to use each feature of my site.

So, I've attempted to use strictModuleExceptionHandling, which someone suggested online.

In webpack.config.js
config.output = {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js', 
    strictModuleExceptionHandling: true
};

This did absolutely nothing. Did I use it wrong?

I also considered trying to use strictExportPresence, which someone had suggested. But according to their documentation, it's deprecated and will be removed.

What I want, is for Webpack to print something to the console that alerts me when I attempt to import something that does not exist (and results in undefined).

I'm used to this functionality, because my current Gulp.js + Browserify build system takes no prisoners. I want to switch to Webpack, but this is a massive show stopper until I can achieve the same level of quality control I already have.

So, what can I do to make this possible?

Thanks,

Upvotes: 2

Views: 682

Answers (1)

Avaq
Avaq

Reputation: 3031

First and foremost I had to tell Babel to error on missing imports, by setting modules: false in the webpack-preset-env configuration. This caused Webpack to raise warnings.

Secondly, setting config.module.strictExportPresence = true turned those warnings into compilation errors.

Upvotes: 2

Related Questions