webpack环境配置分类结合vue使用

文件目录结构

 

按照目录结构创建好文件

控制台执行: npm install

/config/webpack.common.js

const path = require('path')
const {merge} = require('webpack-merge')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader');
const HtmlWebpcakPlugin = require("html-webpack-plugin")
module.exports = {
    entry:'./src/main.js',
    output:{
        path:path.resolve(__dirname,'../dist'),
        filename:'[name].bundle.js'
    },
    plugins:[
        new CleanWebpackPlugin(),
        new HtmlWebpcakPlugin({
            template: path.resolve(__dirname,'../src/index.html')
        }),
        new VueLoaderPlugin()
    ],
    module:{
        rules:[
            {
                test: /\.vue$/,
                loader: 'vue-loader'
              },
              {
                test: /\.css$/,
                use: ['vue-style-loader', 'css-loader','style-loader']
              }
        ]
    },
    resolve: {
        extensions: ['.js', '.vue'],
        alias: {
          'vue$': 'vue/dist/vue.esm.js'
        }
      }
}

开发环境

 ./config/webpack.dev.js
/*
 * @description: 
 * @param: 
 * @return: 
 * @Date: 2024-03-06 14:07:54
 */
const path = require('path')
const {merge} = require('webpack-merge')
const common = require('./webpack.common.js')

module.exports = merge(common,{
    mode:'development',
    devtool:'inline-source-map',
    devServer:{
        static:path.join(__dirname,'../dist'),
        host:'localhost',
        port:8080,
        open:true
    }
})

生产环境

./config/webpack.prod.js
/*
 * @description: 
 * @param: 
 * @return: 
 * @Date: 2024-03-06 14:08:25
 */
const path = require('path')
const {merge} = require('webpack-merge')
const common = require('./webpack.common.js')
const TerserWebpackPlugin = require('terser-webpack-plugin')
module.exports = merge(common,{
    mode:"production",
    output:{
        filename:'[name].[hash:7].js' //例如 wang.4678946.js
    },
    optimization:{
        minimize:true,
        minimizer:[new TerserWebpackPlugin()]
    }
})
src/main.js

/*
 * @description: 
 * @param: 
 * @return: 
 * @Date: 2024-04-07 17:50:43
 */
import Vue, { h } from 'vue'
import App from './App.vue'
new Vue({
    render:h=>h(App)
}).$mount('#app')
src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
    <script src="../dist/build.js"></script>
</body>
</html>

src/App.vue

<!--
 * @description: 
 * @param: 
 * @return: 
 * @Date: 2024-04-07 18:47:25
-->
<template>
    <div>
        {{ message }}
    </div>
</template>

<script>
export default {
    data() {
        return {
            message:'你打的'
        }
    }

}
</script>

<style></style>

#package.json
{
  "name": "test-vue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack serve --config ./config/webpack.dev.js ",
    "build": "webpack --config ./config/webpack.dev.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^4.0.0",
    "html-webpack-plugin": "^5.6.0",
    "terser-webpack-plugin": "^5.3.10",
    "vue": "^2.7.16",
    "webpack": "^5.91.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^5.0.4",
    "webpack-merge": "^5.10.0",
    "css-loader": "^7.0.0",
    "vue-loader": "^15.4.2",
    "vue-style-loader": "^4.1.3",
    "vue-template-compiler": "^2.7.16"
  },
  "dependencies": {
    
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值