const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 设置nodejs环境变量 设置开发环境的配置,默认看生产环境
// process.env.NODE_ENV = 'development'
module.exports = {
entry: ['./src/js/index.js', './src/index.html'],
output: {
filename: 'js/main.js',
path: resolve(__dirname, 'build'),
// 将某些资源发送到指定目录,
assetModuleFilename: 'images/[hash:10][ext][query]',
clean: true //每次构建前清理 /build 文件夹
},
module: {
rules: [
// 加载css
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
// 加载less
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
// 加载images url中的图片资源
{
test: /\.(png|svg|jpg|jpeg|gif)$/,
type: 'asset/resource'
},
// 将 HTML 导出为字符串,需要传入静态资源的引用路径
{
test: /\.html$/,
loader: 'html-loader'
},
// 加载其他文件资源
{
exclude: /\.(js|css|less|html|jpg|png|gif)$/,
loader: 'file-loader',
options: {
outputPath: 'static',
name: '[hash:10].[ext]'
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
],
mode: 'development',
devServer: {
// 未配置端口,默认localhost:8080
static: './build',
// 启动gzip压缩
compress: true,
open: true,
hot: true
},
devtool: 'source-map'
}