春风得意马蹄疾
一日看尽长安花

Webpack构建vue单页面项目配置

安装依赖

  "devDependencies": {//开发依赖
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.1",
    "babel-loader": "^8.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^5.0.0",
    "html-webpack-plugin": "^4.5.0",
    "mini-css-extract-plugin": "^1.2.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^10.0.4",
    "style-loader": "^2.0.0",
    "vue-loader": "^15.9.3",
    "vue-template-compiler": "^2.6.12",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-merge": "^5.2.0"
  },
  "dependencies": { //生产依赖
    "vue": "^2.6.12",
    "vue-router": "^3.4.7",
    "vuex": "^3.5.1"
  }

由于开发环境配置与生产环境配置有所不同,比如css代码是否压缩,所以需要两个环境的配置文件,而这两个文件又有一些重复的部分,所以将重复部分抽离出来为基础配置文件作为两个环境的公共代码

解析vue文件

  1. 安装vue-loader, 进行配置
  2. 安装vue-template-compiler, 不用配置
  3. 使用vue的plugin

基础配置(webpack.base.config.js)

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');//html模板
const VueLoaderPlugin = require('vue-loader/lib/plugin');//支持vue组件的热加载
module.exports = {
  entry: './src/main.js',//入口
  output: {
    filename: 'js/bundle.js',
    path: path.join(__dirname, '../dist'),
    publicPath: '/'
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, '../public/index.html'),
      filename: 'index.html',
      title: '首页'
    }),
    new VueLoaderPlugin()
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          plugins: [],
          presets: [
            '@babel/preset-env'
          ]
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        exclude: /node_modules/
      }
    ]
  },

  resolve: {
    // 可以省略的文件后缀
    extensions: ['.js', '.json', '.vue'],
  }
}

开发环境配置(webpack.dev.config.js)

const path = require("path");
const baseConfig = require("./webpack.base.config");//引入基础配置
const {merge} = require('webpack-merge');//对象合并(此方法不会覆盖)

module.exports = merge(baseConfig, {
  mode: "development",
  module: {
    rules: [
      {
        test: /\.css$/,
          //解析css,将css解析并添加至header的style标签中
        use: ['style-loader', 'css-loader']
      },
      {
           //解析scss,将scss解析并添加至header的style标签中
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.(jpg|jpeg|png|gif|svg|webp)$/,
        loader: 'file-loader'
      }
    ]
  },

  devServer: {//开启webpack服务
    open: true,
    contentBase: path.join(__dirname, "./dist"),
  },

});

生产环境配置(webpack.pro.config.js)

const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.config");
const {CleanWebpackPlugin} = require('clean-webpack-plugin');//清除上一次打包出的文件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');//css代码抽离
module.exports = merge(baseConfig, {
  mode: "production",
  plugins: [
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: 'css/main.css' //抽离后放置的的文件
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
          //解析css后,不在放置header中,而是抽离css为单独文件,即main.css
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.scss$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
      },
      {
        test: /\.(jpg|jpeg|png|gif|svg|webp)$/,
        loader: 'url-loader',
        options: {
          limit: 1024*1024*1
        }
      }
    ]
  }
});
Like
Like Love Haha Wow Sad Angry
赞(0) 打赏
未经允许不得转载:栗子纪blog » Webpack构建vue单页面项目配置
分享到: 更多 (0)
0 0 vote
Article Rating
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments

创作不易,打赏一下作者买瓶洗发露

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x