ProvidePlugin not working with jquery - webpack

a 夏天 提交于 2019-12-10 14:54:25

问题


I am trying to access jquery in my application according to information on webpack website you are able to create global variable using webpack https://webpack.js.org/plugins/provide-plugin/

here is my webpack set up

module.exports = {
    entry: {
        index: "./scripts/index.js",
        vendorJS: ["jquery", "jquery-validation", "jquery-validation-unobtrusive"]
    }
    ,
    output: {
        path: path.join(__dirname, '/wwwroot/'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css', '.scss', '.sass']
    },
    module: {
        rules: [
            {
                test: /\.scss$/i,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'sass-loader']
                })
            },
            {
                test: /\.css$/i,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader']
                })
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: ['url-loader?limit=10000&mimetype=application/font-woff&name=/fonts/[name].[ext]'],
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: ['file-loader?name=/fonts/[name].[ext]'],
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['./wwwroot']),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        }),
        css
    ],

}

using thius set up i am unable to access my $ - jquery in console menu.

any idea why this is happening?


回答1:


I know it's late, but for Googlers (or Duck Duck goers if you care about privacy), I had the same problem:

if I used $: 'jquery' It threw

Can't resolve 'jquery'

If I used

const jquery = require('jquery');
$: jquery

It threw

Can't import module "undefined"

This finally worked for me:

new webpack.ProvidePlugin({
    $: require.resolve('jquery'),
    jQuery: require.resolve('jquery')
})


来源:https://stackoverflow.com/questions/45899466/provideplugin-not-working-with-jquery-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!