-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
150 lines (140 loc) · 4.85 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
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const {InjectManifest} = require('workbox-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const {DefinePlugin} = require('webpack');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const packageJSON = require('./package.json');
const crypto = require('crypto');
const fs = require('fs');
/**
* Высчитывает hash от содержимого файла
* @param {String} filePath путь до файла
* @return {String}
*/
function getFileHash(filePath) {
const fileBuffer = fs.readFileSync(filePath);
const hashSum = crypto.createHash('sha256');
hashSum.update(fileBuffer);
return hashSum.digest('hex');
}
const DEPLOY_DIR = 'dist';
const SERVER_IP = 'brrrello.ru';
const confConst = {
LOCAL_HOST: 'localhost',
BACKEND_RELEASE: process.env.BACKEND_RELEASE || SERVER_IP,
BACKEND_PORT_RELEASE: 443,
BACKEND_PORT_DEBUG: 8000,
FRONTEND_RELEASE: process.env.FRONTEND_RELEASE || SERVER_IP,
FRONTEND_PORT: 3001,
DEBUG: process.env.NODE_ENV !== 'production',
HTTP: 'http',
HTTPS: 'https',
};
const confDefs = {
FRONTEND_ADDRESS: JSON.stringify(confConst.DEBUG ?
confConst.LOCAL_HOST : confConst.FRONTEND_RELEASE),
FRONTEND_PORT: confConst.FRONTEND_PORT,
BACKEND_ADDRESS: JSON.stringify(confConst.DEBUG ? confConst.LOCAL_HOST : confConst.BACKEND_RELEASE),
BACKEND_PORT: confConst.DEBUG ? confConst.BACKEND_PORT_DEBUG : confConst.BACKEND_PORT_RELEASE,
DEBUG: confConst.DEBUG,
SCHEME: JSON.stringify(confConst.DEBUG ? confConst.HTTP : confConst.HTTPS),
APP_VERSION: JSON.stringify(packageJSON.version),
SW_FILE_NAME: JSON.stringify(`sw.${confConst.DEBUG ? '' : getFileHash('./src/sw.js')}.js`),
};
const devServer = {
port: confDefs.FRONTEND_PORT,
hot: false,
static: [DEPLOY_DIR],
historyApiFallback: true, // Для работы роута на 404
};
const config = {
entry: './src/index.js',
output: {
publicPath: '/',
path: path.resolve(__dirname, DEPLOY_DIR),
filename: `bundle${confConst.DEBUG ? '' : '.[contenthash]'}.js`,
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: path.resolve(__dirname, 'node_modules/'),
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
{
include: [
path.resolve(__dirname, 'src/'),
],
test: /\.(s*)css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
{
include: [
path.resolve(__dirname, 'src/components/'),
path.resolve(__dirname, 'src/views/'),
path.resolve(__dirname, 'src/popups/'),
],
test: /\.hbs$/,
loader: 'handlebars-loader',
options: {
helperDirs: path.resolve(__dirname, 'src/modules/Helpers'),
},
},
],
},
optimization: {
minimizer: [
new CssMinimizerPlugin({
parallel: true,
}),
],
minimize: true,
},
plugins: [
new MiniCssExtractPlugin({filename: `style${confConst.DEBUG ? '' : '.[contenthash]'}.css`}),
new HtmlWebpackPlugin({
backend: confDefs.DEBUG ?
`${JSON.parse(confDefs.SCHEME)}://${JSON.parse(confDefs.BACKEND_ADDRESS)}` +
`:${confDefs.BACKEND_PORT}` : null,
scriptLoading: 'module',
filename: 'index.html',
template: 'src/index_template.html',
}),
new DefinePlugin(confDefs),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'public', 'assets'),
to: path.resolve(__dirname, DEPLOY_DIR, 'assets'),
},
],
}),
],
mode: confConst.DEBUG ? 'development' : 'production',
devtool: confConst.DEBUG ? 'source-map' : undefined,
devServer: confConst.DEBUG ? devServer : devServer,
};
if (!confConst.DEBUG) {
config.optimization.minimizer.push(new TerserPlugin());
config.plugins.push(new InjectManifest({
swSrc: './src/sw.js',
swDest: JSON.parse(confDefs.SW_FILE_NAME),
exclude: [
/\.m?js$/,
],
}));
}
module.exports = config;