核心概念

  1. Entry
  2. | 代码的入口,打包的入口。
  3. Output
  4. | 打包文件的(bundle)所在的目录
  5. Loader
  6. | 可以处理非js文件
  7. Plugins
  8. | 可以参与打包的整个过程,打包优化压缩
  9. mode
  10. | development/production

目录结构

image.png

初始化

  1. yarn init -y //生产package.json的文件
  1. //配置package.json
  2. {
  3. "name": "webpack-vue",
  4. "version": "1.0.0",
  5. "license": "MIT",
  6. "private": true //让项目私有化
  7. }
  1. //安装依赖
  2. yarn add webpack webpack-cli
  1. 新建webpack.config.js文件,并写入
  2. const webpack = require("webpack");
  3. const path = require("path");
  4. const config = {
  5. entry:path.resolve(__dirname,'src/main.js'), //入口文件
  6. output:{ //出口文件
  7. path:path.resolve(__dirname,'dist'),
  8. filename:'bundle.js'
  9. },
  10. mode:'development' //模式
  11. }
  12. module.exports = config;
  1. //配置package.json文件
  2. "scripts": {
  3. "build": "webpack"
  4. }
  1. npm run build //自动生成dist文件夹,生成打包文件

loader

wepack自身只能处理js和json文件,加上loader后,能让wepack去处理非js(css)的文件。loader可以将所有类型的文件转换为webpack能够处理的有效模块。

  1. //安装依赖
  2. yarn add style-loader css-loader
  1. const webpack = require("webpack");
  2. const path = require("path");
  3. const config = {
  4. entry:path.resolve(__dirname,'src/main.js'), //入口文件
  5. output:{ //出口文件
  6. path:path.resolve(__dirname,'dist'),
  7. filename:'bundle.js'
  8. },
  9. + module:{
  10. + rules:[
  11. + {
  12. + test:/\.css$/i,
  13. + use:['style-loader','css-loader']
  14. + }
  15. + ]
  16. + },
  17. mode:'development' //模式
  18. }
  19. module.exports = config;

plugin

  1. yarn add html-webpack-plugin
  2. //自动生成index.html文件
  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. const config = {
  3. ....
  4. plugins:[
  5. new HtmlWebpackPlugin({
  6. title:"webpack-vue",
  7. template:path.resolve(__dirname,"public/index.html")
  8. })
  9. ],
  10. mode:'development' //模式
  11. }
  12. module.exports = config;
  1. //index.html 模板文件
  2. plugins:[
  3. new HtmlWebpackPlugin({
  4. title:"vue-webpack",
  5. template:path.resolve(__dirname,"public/index.html")//以这个路径下的html为模板
  6. })
  7. ],

清除dist文件夹

  1. yarn add clean-webpack-plugin
  1. const {CleanWebpackPlugin} = require("clean-webpack-plugin");
  2. const config = {
  3. ...
  4. plugins:[
  5. ...
  6. new CleanWebpackPlugin()
  7. ],
  8. mode:'development' //模式
  9. }

配置webpack-server

目的:为了查看代码运行时的效果。

  1. yarn add webpack-dev-server
  1. devServer:{
  2. contentBase:"./dist",
  3. port:8000, //改变端口
  4. overlay:{
  5. errors:true
  6. } //可以将报道显示到页面上
  7. }
  1. //配置package.json
  2. "scripts": {
  3. ...
  4. "serve":"webpack serve --open"
  5. }
  1. npm run serve

实现错误跟踪

  1. module.exports = {
  2. devtool: 'inline-source-map'
  3. }

安装vue-loader

  1. yarn add vue vue-loader vue-template-compiler
  1. const {VueLoaderPlugin} = require("vue-loader")
  2. module.exports = {
  3. module:{
  4. rules:[
  5. ...
  6. {
  7. test:/\.vue$/,
  8. loader:'vue-loader'
  9. }
  10. ]
  11. },
  12. plugins:[
  13. new VueLoaderPlugin()
  14. ]
  15. }
  1. //main.js
  2. import App from './App.vue';
  3. import Vue from 'vue';
  4. new Vue({
  5. render:h=>h(App)
  6. }).$mount("#app");

配置图片

  1. module.exports = {
  2. module:{
  3. rules:[
  4. {
  5. test:/\.(png|jpg|svg|gif|jpeg)$/i,
  6. type:'asset/resource'
  7. }
  8. ]
  9. }
  10. }