Webpack: Throw error on missing member import

后端 未结 2 1191
猫巷女王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:

提交回复
热议问题