首页 > 解决方案 > 未捕获的 ReferenceError:jquery 未在 Object.766 中定义(外部“jquery.js”:1)

问题描述

我正在使用DLL方法打包jQuery库但浏览器一直报错这是我的webpack.config.js文件和webpack.dll.js文件和index.js文件

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const AddAssetHtmlWebpackPlugin = require('add-asset-html-webpack-plugin')

module.exports = {
  entry: './src/js/index.js',
  output: {
    filename: 'js/bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean:true
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      scriptLoading: 'blocking'
    }),
    new webpack.DllReferencePlugin({
      manifest:path.resolve(__dirname,'dll/manifest.json')
    }),
    new AddAssetHtmlWebpackPlugin({
      filepath: path.resolve(__dirname, 'dll/jquery.js'),
      publicPath: './',
    })
  ],
  mode: 'production',
  devtool:'source-map'
  
};

const { resolve } = require('path');
const webpack = require('webpack');

module.exports = {
    entry: {
        
        jquery:['jquery']
    },
    output: {
        filename: '[name].js',
        path: resolve(__dirname, 'dll'),
        library: '[name]_[hash]',
        
    },
    plugins: [
        new webpack.DllPlugin({
            name: '[name].js',
            path: resolve(__dirname, 'dll/manifest.json')
        })
    ],
    mode:'production'
}

import $ from 'jquery'

console.log($);

在此处输入图像描述

在此处输入图像描述

webpack打包过程没有错,文件应该是在正确的目录下生成的,引用过程没有错,但是就是不知道为什么浏览器一直报错,谁能帮我解决这个问题

标签: javascriptwebpackdllproduction

解决方案


推荐阅读