This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
149 lines (143 loc) · 4.71 KB
/
webpack.common.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
const path = require('path');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const flexBugsFixes = require('postcss-flexbugs-fixes');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { BUILD_ENVS } = require('./constants');
const RESOURCES_PATH = path.resolve(__dirname);
const ENTRY_REACT = path.resolve(RESOURCES_PATH, 'src/index.js');
const ENTRY_INSTALL = path.resolve(RESOURCES_PATH, 'src/install/index.js');
const ENTRY_LOGIN = path.resolve(RESOURCES_PATH, 'src/login/index.js');
const HTML_PATH = path.resolve(RESOURCES_PATH, 'public/index.html');
const HTML_INSTALL_PATH = path.resolve(RESOURCES_PATH, 'public/install.html');
const HTML_LOGIN_PATH = path.resolve(RESOURCES_PATH, 'public/login.html');
const ASSETS_PATH = path.resolve(RESOURCES_PATH, 'public/assets');
const PUBLIC_PATH = path.resolve(__dirname, '../build/static');
const PUBLIC_ASSETS_PATH = path.resolve(PUBLIC_PATH, 'assets');
const BUILD_ENV = BUILD_ENVS[process.env.BUILD_ENV];
const isDev = BUILD_ENV === BUILD_ENVS.dev;
const config = {
mode: BUILD_ENV,
target: 'web',
context: RESOURCES_PATH,
entry: {
main: ENTRY_REACT,
install: ENTRY_INSTALL,
login: ENTRY_LOGIN,
},
output: {
path: PUBLIC_PATH,
filename: '[name].[hash].js',
},
resolve: {
modules: ['node_modules'],
alias: {
MainRoot: path.resolve(__dirname, '../'),
ClientRoot: path.resolve(__dirname, './src'),
// TODO: uncomment when v16.13.1 is released https://stackoverflow.com/a/62671689/12942752
// 'react-dom': '@hot-loader/react-dom',
},
},
module: {
rules: [
{
test: /\.ya?ml$/,
type: 'json',
use: 'yaml-loader',
},
{
test: /\.css$/i,
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDev,
},
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
flexBugsFixes,
autoprefixer({
flexbox: 'no-2009',
}),
],
},
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
},
},
{
exclude: [/\.js$/, /\.html$/, /\.json$/, /\.css$/],
use: {
loader: 'url-loader',
options: {
fallback: 'file-loader',
name: 'media/[name].[hash:8].[ext]',
limit: 10 * 1024,
},
},
},
],
},
plugins: [
new CleanWebpackPlugin({
root: PUBLIC_PATH,
verbose: false,
dry: false,
}),
new HtmlWebpackPlugin({
inject: true,
cache: false,
chunks: ['main'],
template: HTML_PATH,
}),
new HtmlWebpackPlugin({
inject: true,
cache: false,
chunks: ['install'],
filename: 'install.html',
template: HTML_INSTALL_PATH,
}),
new HtmlWebpackPlugin({
inject: true,
cache: false,
chunks: ['login'],
filename: 'login.html',
template: HTML_LOGIN_PATH,
}),
new MiniCssExtractPlugin({
filename: isDev ? '[name].css' : '[name].[hash].css',
chunkFilename: isDev ? '[id].css' : '[id].[hash].css',
}),
new CopyPlugin({
patterns: [
{
from: ASSETS_PATH,
to: PUBLIC_ASSETS_PATH,
},
],
}),
],
};
module.exports = config;