Webpack: Throw error on missing member import

后端 未结 2 1192
猫巷女王i
猫巷女王i 2021-02-12 18:08

I have an import something like this:

import { foo } from \'bar\';

Is there a way to get Webpack to throw an error if foo is not d

相关标签:
2条回答
  • 2021-02-12 18:15

    It is possible to configure webpack 2 to throw an error when an import fails by using output.strictModuleExceptionHandling. The functionality was added by this pull request https://github.com/webpack/webpack/pull/3302 but it has not been documented yet. Here's how to use it:

    module.exports = {
        entry: {
            main: "./main.js",
        },
        output: {
            filename: "[name].bundle.js",
            strictModuleExceptionHandling: true
        }
    }
    

    Now if I try to import from a file which dosn't exist, or I make an import which resolves to undefined I would get error and warning messages in the webpack console:

    WARNING in ./js/pedigree.js
    32:35-49 "export 'default' (imported as 'DisorderLegend') was not found in './disorderLegend'
    
    ERROR in ./js/pedigree.js
    Module not found: Error: Can't resolve './OkCancelDialogue' in '/home/tim/workspace/projects/public/js/ext-lib/panogram/js'
     @ ./js/pedigree.js 5:0-54
     @ ./js/viewerPedigree.js
     @ ./main.js
     @ multi (webpack)-dev-server/client?http://localhost:8080 ./main.js
    webpack: Failed to compile.
    

    In the chrome console you'll get a warning like this:

    0 讨论(0)
  • 2021-02-12 18:31

    As Tobias K pointed out in the comments, the other answer is incorrect. Configuring strictModuleExceptionHandling: true will not produce a compile time error if you try to import a module which does not exist.

    The correct configuration is strictExportPresence: true, which is only available in webpack v2.3.0 and later. (Earlier versions can only show a warning, not an error.)

    0 讨论(0)
提交回复
热议问题