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"
}
}