This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
webpack.config.js
201 lines (192 loc) · 5.77 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint-disable no-console */
const path = require('path');
const { readdirSync } = require('fs');
const autoPrefixer = require('autoprefixer');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const I18nextVersioningPlugin = require('i18next-versioning-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ImageMinPlugin = require('imagemin-webpack-plugin').default;
const { DefinePlugin } = require('webpack');
const packageVersion = require('./package.json').version;
const getClientEnvironment = require('./config/env');
const env = getClientEnvironment();
const production = env.raw.NODE_ENV === 'production';
const useDevServer = !production && process.env.WEBPACK_DEV_SERVER === 'true';
const buildVersion = `${packageVersion}-dev`;
const devServerUrl = new URL(env.raw.BASE_HREF || 'http://127.0.0.1:9000');
const devServerConfig = {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: devServerUrl.port,
host: devServerUrl.hostname,
publicPath: devServerUrl.pathname
};
const copies = [{
from: path.resolve(__dirname, 'src', 'manifest.json'),
to: path.resolve(__dirname, 'dist', 'manifest.json'),
}, {
from: path.resolve(__dirname, 'src', 'img'),
to: path.resolve(__dirname, 'dist', 'img'),
toType: 'dir',
}, {
from: path.resolve(__dirname, 'src', 'favicon'),
to: path.resolve(__dirname, 'dist', 'favicon'),
toType: 'dir',
}, {
from: path.resolve(__dirname, 'src', 'i18n'),
to: path.resolve(__dirname, 'dist', 'i18n'),
toType: 'dir',
}];
// include /src/.api/ in development builds; i.e. include API response mocks
// Files in /src/.api/ will be served as responses to test requests,
// according to the file's name.
// When you test e.g. a username `ghost`, the contents of
// /src/.api/ghost will be served.
if (!production) {
copies.push({
from: path.resolve(__dirname, 'src', '.api'),
to: path.resolve(__dirname, 'dist', '.api'),
toType: 'dir',
});
}
const config = {
mode: env.raw.NODE_ENV,
entry: {
app: './src/js/main.js',
worker: './src/js/worker.js'
},
output: {
filename: (chunkData) => {
return chunkData.chunk.name === 'app' ? 'js/[name].[hash:7].js' : './[name].js';
},
path: path.resolve(__dirname, 'dist'),
publicPath: ''
},
infrastructureLogging: {
level: 'info'
},
module: {
rules: [
{
test: /\.scss$/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
{ loader: 'postcss-loader', options: { plugins: [autoPrefixer] } },
'sass-loader'
],
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: 'last 2 versions'
}
}
]
],
plugins: [
[
'@babel/plugin-transform-runtime',
{
regenerator: true
}
],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread'
]
}
}
},
{
test: /\.(png|gif|jpg|jpeg)$/,
use: [
{
loader: 'url-loader',
options: { name: 'images/design/[name].[hash:6].[ext]', publicPath: '../', limit: 8192 },
},
],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [
{
loader: 'url-loader',
options: { name: 'fonts/[name].[hash:6].[ext]', publicPath: '../', limit: 8192 },
},
],
},
],
},
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
}),
new OptimizeCssAssetsPlugin({}),
],
},
plugins: [
new I18nextVersioningPlugin({
langsRoot: './src/i18n',
hashFileName: './i18nVersionHashes.json'
}),
new HtmlWebpackPlugin({
inject: true,
hash: production,
filename: 'index.html',
template: path.resolve(__dirname, 'src', 'index.html'),
favicon: path.resolve(__dirname, 'src', 'img', 'favicon.png'),
templateParameters: {
baseHref: useDevServer
? `http://${devServerConfig.host}:${devServerConfig.port}${devServerConfig.publicPath}`
: env.raw.BASE_HREF,
production,
buildVersion,
testUsers: !production ? readdirSync('./src/.api').join(', ') : undefined
},
minify: production ? {
collapseWhitespace: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
} : false
}),
new MiniCssExtractPlugin({
filename: 'css/[name].[hash:7].css',
}),
new ImageMinPlugin({ test: /\.(jpg|jpeg|png|gif|svg)$/i }),
new CleanWebpackPlugin({
/**
* Some plugins used do not correctly save to webpack's asset list.
* Disable automatic asset cleaning until resolved
*/
cleanStaleWebpackAssets: false,
verbose: true,
}),
new CopyWebpackPlugin(copies),
new DefinePlugin({
...env.stringified,
// i18nVersions - added by I18nextVersioningPlugin
})
],
};
if (useDevServer) {
config.devServer = devServerConfig;
}
console.log(`Mode: ${config.mode}`);
console.log(`useDevServer: ${useDevServer}`);
module.exports = config;