const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isDev = process.env.NODE_ENV !== 'production';
const { VueLoaderPlugin } = require('vue-loader');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
entry: {
index: path.join(__dirname, '../src/main.js'),
},
output: {
filename: isDev ? '[name].bundle.[chunkhash].js' : '[name].js',
path: path.resolve(__dirname, '../dist'),
clean: true, // 每一次打包前清空dist文件夹
},
module: {
rules: [
{
test: /\.(png|svg|jpeg|gif)$/i,
include: path.join(__dirname, '../src'),
type: 'asset/resource',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
include: [path.join(__dirname, '../src')],
type: 'asset/resource',
},
{
test: /\.(le|c)ss$/i,
include: [path.join(__dirname, '../src')],
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
},
},
{
loader: 'css-loader',
options: {
esModule: true,
modules: {
namedExport: true,
localIdentName: '[local]',
},
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [['postcss-preset-env']],
},
},
},
'less-loader',
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'@': path.join(__dirname, '../src'),
},
symlinks: false,
},
plugins: [
new HtmlWebpackPlugin({
title: 'Vue App',
favicon: path.join(__dirname, '../public/favicon.ico'),
template: path.join(__dirname, '../public/index.html'),
}),
new MiniCssExtractPlugin({
filename: isDev ? '[name].[contenthash].css' : '[name].css',
chunkFilename: isDev ? '[id].[contenthash].css' : '[id].css',
}),
// 请确保引入这个插件!
new VueLoaderPlugin(),
new CopyPlugin({
patterns: [
{
from: path.join(__dirname, '../public'),
to: path.join(__dirname, '../dist', 'public'),
},
],
}),
],
};