原生js项目中webpack.config.js的简单配置
const { resolve } = require("path");
const autoprefixer = require("autoprefixer");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const miniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
mode: "development",
entry: {
index: resolve(__dirname, "./src/js/index.js"),
detail: resolve(__dirname, "./src/js/detail.js"),
collections: resolve(__dirname, "./src/js/collections.js"),
},
output: {
path: resolve(__dirname + "./dist"),
filename: "js/[name].js",
},
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: resolve(__dirname, "node_modules"),
query: {
presets: ["latest"],
},
},
{
test: /\.tpl$/,
loader: "ejs-loader",
},
{
test: /\.scss$/,
use: [
{
loader: miniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV === "development",
},
},
"css-loader",
{
loader: "postcss-loader",
options: {
plugins: function () {
return [autoprefixer("last 5 version")];
},
},
},
"sass-loader",
],
},
{
test: /\.(png|jpg|jpeg|gif|ico|woff|eot|svg|ttf)$/i,
loader: [
"url-loader?limit=1024&name=img/[name]-[hash:16].[ext]",
"image-webpack-loader",
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
filename: "index.html",
template: resolve(__dirname, "src/index.html"),
title: "新闻头条",
chunks: ["index"],
chunksSortMode: "manual",
excludeChunks: ["node_modules"],
hash: true,
minify: {
removeComments: true,
collapseWhitespace: true,
},
}),
new HtmlWebpackPlugin({
filename: "detail.html",
template: resolve(__dirname, "src/detail.html"),
title: "新闻详情",
chunks: ["detail"],
chunksSortMode: "manual",
excludeChunks: ["node_modules"],
hash: true,
minify: {
removeComments: true,
collapseWhitespace: true,
},
}),
new HtmlWebpackPlugin({
filename: "collections.html",
template: resolve(__dirname, "src/collections.html"),
title: "我的新闻",
chunks: ["collections"],
chunksSortMode: "manual",
excludeChunks: ["node_modules"],
hash: true,
minify: {
removeComments: true,
collapseWhitespace: true,
},
}),
new miniCssExtractPlugin({
filename: "css/[name].css",
}),
],
devServer: {
watchOptions: {
ignored: /node_modules/,
},
open: true,
host: "localhost",
port: 3000,
},
};