需安装插件:add-asset-html-webpack-plugin

webpack.dll.js

  1. /**
  2. * 使用过dll技术,对某些库(第三方库:jQuery,vue,react)进行单独打包
  3. * 当你运行webpack时 默认查找 webpack.config.js 配置文件
  4. * 需求:需要运行webpack.dll.js 文件
  5. * --> webpack --config webpack.dll.js
  6. */
  7. const {resolve} = require('path')
  8. const webapck = require('webpack')
  9. const webpack = require('webpack')
  10. module.exports = {
  11. entry:{
  12. //最终打包生成的[name] --> jquery
  13. jQuery:['jquery']
  14. },
  15. output:{
  16. filename:'[name].js',
  17. path:resolve(__dirname,'dll'),
  18. library:'[name]_[hash]',//打包的库里面向外暴露出去的内容叫什么名字
  19. },
  20. plugins:[
  21. //打包生成一个 manifest.json -->提供和jQuery映射
  22. new webpack.DllPlugin({
  23. name:'[name]_[hash]',//映射库的暴露的内容名称
  24. path:resolve(__dirname,'dll/manifest.json')
  25. })
  26. ],
  27. mode:'production'
  28. }

webpack.config.js

  1. const{resolve} = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. const webpack = require('webpack')
  4. const AddAssetHtmlWebpackPlugin = require('add-asset-html-webpack-plugin')
  5. module.exports = {
  6. entry:'./src/index.js',
  7. output:{
  8. filename:'built.js',
  9. path:resolve(__dirname,'build')
  10. },
  11. module:{
  12. rules:[
  13. //loader的配置
  14. ]
  15. },
  16. plugins:[
  17. new HtmlWebpackPlugin({
  18. template:'./src/index.html'
  19. }),
  20. //告诉webpack哪些库不参与打包,同时使用的名称也得改
  21. new webpack.DllReferencePlugin({
  22. manifest:resolve(__dirname,'dll/manifest.json')
  23. }),
  24. //将某个文件打包输出去,并在html中自动引入该资源
  25. new AddAssetHtmlWebpackPlugin({
  26. filepath:resolve(__dirname,'dll/jquery.js')
  27. })
  28. ],
  29. mode:'development'
  30. }