-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.cjs
159 lines (155 loc) · 4.33 KB
/
webpack.config.cjs
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
// webpack.config.cjs for JS and JSON (default), TS, HTML, CSS, SCSS, TailwindCSS
// https://webpack.js.org
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExecaPlugin = require('execa-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const glob = require('glob');
const path = require('path');
const components = glob
.globSync('./public/lib/components/**.ts')
.reduce(function (obj, el) {
obj[path.parse(el).name] = `./${el}`;
return obj;
}, {});
const DEV_MODE = process.env.NODE_ENV !== 'production';
const BUILD_DOCS = process.env.BUILD_DOCS === 'true' || false;
module.exports = {
stats: { children: true, errorDetails: true },
mode: DEV_MODE ? 'development' : 'production', // development, production(default) or none
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
proxy: {
'/api': `http://localhost:8080`,
},
historyApiFallback: true,
compress: true,
port: 9000,
},
devtool: DEV_MODE ? 'inline-source-map' : false,
entry: {
// start bundling from here
index: './public/lib/script/pages/index.js',
...components,
},
output: {
// bundle to this location
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.[name].js',
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.ts', '.tsx', '.js'],
// Add support for TypeScripts fully qualified ESM imports.
extensionAlias: {
'.js': ['.js', '.ts'],
'.cjs': ['.cjs', '.cts'],
'.mjs': ['.mjs', '.mts'],
},
},
plugins: [
// https://webpack.js.org/plugins/
new HtmlWebpackPlugin({
// minifies html and adds imports
filename: 'index.html',
template: 'public/routes/index.html',
chunks: ['index'], // only include the 'index' chunk
}),
new HtmlWebpackPlugin({
// minifies html and adds imports
filename: 'sub/index.html',
template: 'public/routes/sub/index.html',
chunks: ['index'], // only include the 'index' chunk
}),
new MiniCssExtractPlugin({
// minifies css and splits it
filename: DEV_MODE ? '[name].css' : '[name].[contenthash].css',
chunkFilename: DEV_MODE ? '[id].css' : '[id].[contenthash].css',
}),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'public/static'),
to: path.resolve(__dirname, 'dist'),
},
],
}),
new CopyPlugin({
patterns: [
{
from: path.resolve(
__dirname,
'node_modules/@shoelace-style/shoelace/dist/assets',
),
to: path.resolve(__dirname, 'dist/shoelace/assets'),
},
],
}),
BUILD_DOCS &&
new ExecaPlugin({
onBeforeRun: [
{
args: ['run', 'build-docs'],
cmd: 'npm',
options: {
cwd: process.cwd(),
},
},
],
}),
],
module: {
// loaders, so that webpack understands more than JavaScript and JSON
rules: [
{
// static assets (Images, Fonts, etc.)
test: /\.(png|jpg|gif|svg|eot|ttf|woff)$/,
type: 'asset/resource',
},
{
// static assets (Images, Fonts, etc.)
test: /\.(png|jpg|gif|svg|eot|ttf|woff)$/,
use: [
{
loader: 'file-loader',
},
],
},
{
// TS (`.ts`, `.cts`, `.mts` or `.tsx`)
test: /\.([cm]?ts|tsx)$/,
loader: 'ts-loader',
},
{
// SCSS/SASS
test: /\.s[ac]ss$/i,
use: [
DEV_MODE ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
{
// CSS
test: /\.css$/i, // type to transform
use: [
DEV_MODE ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader', // TailwindCSS, Autoprefixer, etc. (postcss.config.cjs)
options: {
postcssOptions: {
plugins: [
// bundle optimization, asset management, injection of env variables...
['postcss-preset-env', {}],
],
},
},
},
],
},
],
},
};