vue3+webpack打包

文章详细描述了一个使用Webpack构建Vue3应用的配置文件,包括entry点、输出路径、模块规则以及各种插件的使用,同时展示了package.json中的开发和生产环境构建脚本。
摘要由CSDN通过智能技术生成

1. 这里只列举了基本的打包配置,如需其他插件可自行配置

// webpack.config.js

const path = require('path')
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const Dotenv = require('dotenv-webpack')

module.exports = {
  mode: process.env.NODE_ENV,
  entry: {
    main: './src/main.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/[name].[contenthash:8].js'
  },
  resolve: {
    alias: {
      '@': path.resolve('src')
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [ '@babel/preset-env' ]
          }
        }
      }
    ]
  },
  plugins: [
    new Dotenv({
      path: `./.env.${process.env.NODE_ENV}`,
    }),
    new NodePolyfillPlugin(),
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: "Webpack App",
      url: './public',
      template: "./public/index.html",
      filename: "index.html"
    }),
  ]
}

2. package.json

{
  "name": "vue3-webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack serve --config webpack.config.js",
    "build:test": "cross-env NODE_ENV=test webpack --config webpack.config.js",
    "build:prod": "cross-env NODE_ENV=production webpack --config webpack.config.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.23.7",
    "@babel/preset-env": "^7.23.8",
    "babel-loader": "^9.1.3",
    "css-loader": "^6.9.0",
    "html-webpack-plugin": "^5.6.0",
    "sass": "^1.69.7",
    "sass-loader": "^13.3.3",
    "style-loader": "^3.3.4",
    "vue-loader": "^17.4.2",
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  },
  "dependencies": {
    "cross-env": "^7.0.3",
    "dotenv-webpack": "^8.0.1",
    "node-polyfill-webpack-plugin": "^3.0.0",
    "vue": "^3.4.8"
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值