webpack 用jquery 提示$未定义

发布于 2022-09-05 05:54:10 字数 2938 浏览 17 评论 0

webpack 用jquery 提示$未定义 但是window.jquery有值
配置文件如下
var path = require('path');
var webpack = require('webpack');
//用来抽离css的插件
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {

entry: './src/main.js',
output: {
    libraryTarget: 'umd',
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
},
//打包去除外部依赖
externals: {
    'vue': {
        root: 'Vue',
        commonjs: 'vue',
        commonjs2: 'vue',
        amd: 'vue'
    },
    'lodash': {
        commonjs: 'lodash',
        commonjs2: 'lodash',
        amd: 'lodash',
        root: '_'
    },
    'jquery': 'window.jQuery',

},
module: {
    rules: [
        {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: {
                loaders: {
                    css: ExtractTextPlugin.extract({
                        use: 'css-loader',
                        fallback: 'vue-style-loader' // <- this is a dep of vue-loader, so no need to explicitly install if using npm3
                    })
                }
                // other vue-loader options go here
            }
        },
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        },
        {
            test: /\.(png|jpg|gif|svg)$/,
            loader: 'url-loader',
            options: {
                name: "image/[name].[ext]?[hash]",
                limit: 8192
            }
        },
        {
            test: /\.(less|css)$/,
            exclude: /node_modules/,
            use: ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: [
                    {loader: "css-loader"},
                    {loader: "less-loader"}
                ],
            })
        },

    ]
},
plugins: [
    new ExtractTextPlugin("css/styles.css"),
    // new webpack.ProvidePlugin({
    //     jQuery:'jquery',
    //     'window.jQuery': 'jquery',
    //     'window.$': 'jquery',
    //     $: 'jquery',
    // }),
],
resolve: {
    alias: {
        'vue$': 'vue/dist/vue.esm.js',
    }
},
devServer: {
    historyApiFallback: true,
    noInfo: true
},
performance: {
    hints: false
},
devtool: '#eval-source-map'

};

if (process.env.NODE_ENV === 'production') {

module.exports.devtool = '#source-map';
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
        'process.env': {
            NODE_ENV: '"production"'
        }
    }),
    new webpack.optimize.UglifyJsPlugin({
        sourceMap: true,
        compress: {
            warnings: false
        }
    }),
    new webpack.LoaderOptionsPlugin({
        minimize: true
    })
])

}
页面中通过<script src="js/jquery-3.2.1.js"></script>引用

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2022-09-12 05:54:10
// webpack.config.js
module.exports = {
    ...
    plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       })
    ]
};

参考:Webpack using bootstrap - jquery is not defined

岁月染过的梦 2022-09-12 05:54:10

看看是不是jQ的版本问题,还有可能是你的jq没有引入

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文