Webpack: expressing module dependency

后端 未结 2 1589
长发绾君心
长发绾君心 2021-01-31 18:09

I\'m trying to require the bootstrap-webpack module in my webpacked application.

It appears to need jQuery, since the bundled javascript then throws the fol

2条回答
  •  渐次进展
    2021-01-31 18:48

    There are two possible solutions:

    Use the ProvidePlugin: It scans the source code for the given identifier and replaces it with a reference to the given module, just like it has been required.

    // webpack.config.js
    module.exports = {
        ...
        plugins: [
            new webpack.ProvidePlugin({
               $: "jquery",
               jQuery: "jquery"
           })
        ]
    };
    

    Use the imports-loader: It provides the possibility to prepend preparations like require() statements.

    // webpack.config.js
    {
        ...
        module: {
            loaders: [
                { test: require.resolve("jquery"), loader: "imports?jQuery=jquery" }
            ]
        }
    }
    

    In that case you need to run npm install imports-loader --save before.

提交回复
热议问题