-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
182 lines (173 loc) · 5.04 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -------------------------------------------------------------------------- */
/* ----------------------- SPBuild Sytem Verison 1.3.1 ---------------------- */
/* --------------- (C) Grant Ramsay 2023 under the MIT Licence -------------- */
/* ------------------ https://github.com/seapagan/sp-build ------------------ */
/* -------------------------------------------------------------------------- */
const path = require("path");
const fs = require("fs");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const Dotenv = require("dotenv-webpack");
// const ESLintPlugin = require("eslint-webpack-plugin");
const HtmlValidatePlugin = require("html-validate-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const StylelintPlugin = require("stylelint-webpack-plugin");
const WarningsToErrorsPlugin = require("warnings-to-errors-webpack-plugin");
const chooseEntry = () => {
// this will use an index.ts file if exists, otherwise uses index.js
// if both exist, the .ts is preferred
const indexTs = path.join(__dirname, "src/index.ts");
const indexJs = path.join(__dirname, "src/index.js");
return fs.existsSync(indexTs) ? indexTs : indexJs;
};
const haveFavicon = () => {
// if there is a favicon.ico file, use it
const favicon = path.join(__dirname, "src/favicon.ico");
return fs.existsSync(favicon);
};
module.exports = (_env, argv) => {
const devMode = argv.mode !== "production";
const config = {
mode: argv.mode ? argv.mode : "development",
entry: {
bundle: chooseEntry(),
},
devtool: devMode ? "eval" : "source-map",
resolve: {
extensions: [".tsx", ".ts", ".js", ".jsx"],
},
output: {
path: __dirname + "/dist",
filename: "[contenthash].js",
clean: true,
},
plugins: [
new Dotenv({ systemvars: true, expand: true }),
new HtmlWebpackPlugin({
template: "src/index.html",
favicon: haveFavicon() ? "src/favicon.ico" : "",
}),
// new ESLintPlugin(),
// new StylelintPlugin({
// configFile: ".stylelintrc.json",
// }),
new HtmlValidatePlugin(),
].concat(
devMode
? []
: [
// we only want these ones for a production build
new WarningsToErrorsPlugin(),
new MiniCssExtractPlugin({
filename: "[contenthash].css",
}),
]
),
devServer: {
client: {
logging: "warn",
overlay: {
warnings: false,
errors: true,
},
},
static: {
directory: path.join(__dirname, "dist"),
},
watchFiles: ["./src/*"],
hot: true,
},
stats: "minimal",
module: {
rules: [
{
// for CSS Modules
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: {
auto: true,
localIdentName: devMode
? "[path][name]__[local]"
: "[hash:base64]",
},
},
},
{
loader: "postcss-loader",
options: {
sourceMap: true,
postcssOptions: {
config: "postcss.config.js",
},
},
},
{
loader: "sass-loader",
options: {
api: "modern-compiler",
sassOptions: {
// Your sass options
},
},
},
],
include: /\.module\.(sa|sc|c)ss$/,
},
{
// for Standard CSS (non-module) files.
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? "style-loader" : MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "postcss-loader",
options: {
sourceMap: true,
postcssOptions: {
config: "postcss.config.js",
},
},
},
"sass-loader",
],
exclude: /\.module\.(sa|sc|c)ss$/,
},
{
// JS and JSX
test: /\.js?$/,
exclude: /node_modules/,
include: path.resolve(__dirname, "src"),
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
{
// TS and TSX
test: /\.ts?$/,
exclude: /node_modules/,
use: {
loader: "ts-loader",
},
},
{
// inline small svg files
test: /\.svg$/,
type: "asset",
},
],
},
optimization: {
minimizer: ["...", new CssMinimizerPlugin()],
},
};
console.log("Mode : ", config.mode);
return config;
};