webpack: split configs into prod/dev

This commit is contained in:
dogeystamp 2023-01-30 21:37:04 -05:00
parent 63fa42e1e3
commit f6d07eb657
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
6 changed files with 36 additions and 21 deletions

View File

@ -1,6 +1,7 @@
env: env:
browser: true browser: true
es2021: true es2021: true
node: true
extends: eslint:recommended extends: eslint:recommended
overrides: [] overrides: []
parserOptions: parserOptions:

3
package-lock.json generated
View File

@ -16,7 +16,8 @@
"style-loader": "^3.3.1", "style-loader": "^3.3.1",
"webpack": "^5.75.0", "webpack": "^5.75.0",
"webpack-cli": "^5.0.1", "webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1" "webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
} }
}, },
"node_modules/@discoveryjs/json-ext": { "node_modules/@discoveryjs/json-ext": {

View File

@ -5,9 +5,9 @@
"private": true, "private": true,
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack", "build": "webpack --config webpack.prod.js",
"lint": "eslint --ext .js,.jsx src", "lint": "eslint --ext .js,.jsx src *.js",
"start": "webpack serve --open" "start": "webpack serve --open --config webpack.dev.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -40,6 +40,7 @@
"style-loader": "^3.3.1", "style-loader": "^3.3.1",
"webpack": "^5.75.0", "webpack": "^5.75.0",
"webpack-cli": "^5.0.1", "webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1" "webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
} }
} }

View File

@ -1,19 +1,14 @@
const path = require('path'); const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const pages = ["index", "aes"]; const pages = ["index", "aes"];
module.exports = { module.exports = {
mode: 'development',
entry: pages.reduce((config, page) => { entry: pages.reduce((config, page) => {
config[page] = `./src/${page}.js`; config[page] = `./src/${page}.js`;
return config; return config;
}, {}), }, {}),
devtool: 'inline-source-map',
devServer: {
static: './dist',
},
output: { output: {
filename: "[name].js", filename: "[name].js",
path: path.resolve(__dirname, "dist"), path: path.resolve(__dirname, "dist"),
@ -30,21 +25,21 @@ module.exports = {
chunkFilename: "[id].[contenthash].css", chunkFilename: "[id].[contenthash].css",
}), }),
].concat( ].concat(
pages.map( pages.map(
(page) => (page) =>
new HtmlWebpackPlugin({ new HtmlWebpackPlugin({
inject: true, inject: true,
template: `./src/pages/${page}.html`, template: `./src/pages/${page}.html`,
filename: `${page}.html`, filename: `${page}.html`,
chunks: [page], chunks: [page],
}) })
) )
), ),
module: { module: {
rules: [ rules: [
{ {
test: /\.css$/i, test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader'], use: [MiniCssExtractPlugin.loader, "css-loader"],
}, },
], ],
}, },

10
webpack.dev.js Normal file
View File

@ -0,0 +1,10 @@
const { merge } = require("webpack-merge");
const common = require("./webpack.common.js");
module.exports = merge(common, {
mode: "development",
devtool: "inline-source-map",
devServer: {
static: "./dist",
},
});

7
webpack.prod.js Normal file
View File

@ -0,0 +1,7 @@
const { merge } = require("webpack-merge");
const common = require("./webpack.common.js");
module.exports = merge(common, {
mode: "production",
devtool: "source-map",
});