forked from nurikk/zigbee2mqtt-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
175 lines (166 loc) · 5.02 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
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const webpack = require("webpack");
const path = require("path");
const glob = require("glob");
const proxyTo = process.env.Z2M_API_URI ? process.env.Z2M_API_URI : "ws://localhost:8579";
const getPlugins = (production) => {
const plugins = [
new webpack.DefinePlugin({
FRONTEND_VERSION: JSON.stringify(process.env.npm_package_version)
}),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css",
chunkFilename: "[id].[contenthash].css",
}),
new CopyPlugin({
patterns: [{
from: 'src/images/favicon.ico',
}, {
from: 'src/bootstrap-theme'
}],
}),
];
const basePath = "src/templates";
glob.sync(`${basePath}/**/*.html`).forEach((item) => {
plugins.push(
new HtmlWebpackPlugin({
filename: path.relative(basePath, item),
template: item,
})
);
});
if (production) {
if (process.env['CI'] !== 'true') {
plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: "static",
})
);
}
} else {
plugins.push(new ForkTsCheckerWebpackPlugin());
}
return plugins;
};
const config = {
entry: "./src/index.tsx",
output: {
filename: "[name].[contenthash].js",
path: path.resolve("./dist"),
},
target: "web",
devtool: "source-map",
optimization: {
minimize: false,
usedExports: true,
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
defaultVendors: {
test: /node_modules/,
name: "scripts/vendor",
chunks: "all",
enforce: true,
},
},
},
},
resolve: {
// mainFields: ["module", "main"],
extensions: [".ts", ".tsx", ".js", ".html", ".txt"]
},
module: {
rules: [{
test: /\.txt$/i,
type: 'asset/source'
},
{
test: /\.(png|jpe?g|gif)$/i,
type: 'asset/resource',
},
{
test: /\.tsx?$/,
use: [{
loader: "ts-loader",
}],
},
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
"@teamsupercell/typings-for-css-modules-loader",
{
loader: "css-loader",
options: {
modules: true,
},
},
],
exclude: /node_modules/,
},
{
test: /\.css$/i,
include: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.scss$/,
include: [/\.global\./, /node_modules/],
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
sideEffects: true,
},
{
// Preprocess our own .css files
// This is the place to add your own loaders (e.g. sass/less etc.)
// for a list of loaders, see https://webpack.js.org/loaders/#styling
test: /\.scss$/,
exclude: [/\.global\./, /node_modules/],
use: [
MiniCssExtractPlugin.loader,
'@teamsupercell/typings-for-css-modules-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
importLoaders: 1,
},
},
'sass-loader',
],
sideEffects: true,
},
],
},
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
},
contentBase: "./dist",
compress: true,
host: "0.0.0.0",
port: 3030,
proxy: {
"/api": {
target: proxyTo,
ws: true,
},
},
},
stats: "errors-only",
externals: {}
};
module.exports = (env, args) => {
let production = false;
if (args && args.mode === "production") {
production = true;
} else {
console.log("== Development mode");
}
const plugins = getPlugins(production);
return {...config, plugins}
};