-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
148 lines (130 loc) · 3.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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const SETTINGS = require('./settings');
const production = process.env.NODE_ENV === 'production';
const analyzer = process.env.ANALYZE === '1';
const stylesLoaders = [
{
loader: 'css-loader',
options: {
modules: {
mode: 'local',
localIdentName: production ? '[hash:base64:7]' : '[path]__[local]--[hash:base64:5]',
},
},
},
'postcss-loader',
{
loader: 'sass-loader',
options: {
additionalData: '@import "styles/globals";',
sassOptions: {
includePaths: [path.join(__dirname, 'src')],
},
},
},
];
const rules = [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.(css|scss)$/,
include: path.join(__dirname, './src'),
loader: production
? [MiniCssExtractPlugin.loader, ...stylesLoaders]
: ['style-loader', ...stylesLoaders],
},
{
// do not load styles as css modules from other directories (e.g. node_modules) but src
test: /\.(css)$/,
loaders: production
? [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
: ['style-loader', 'css-loader', 'postcss-loader'],
exclude: path.resolve(__dirname, '../src'),
},
{
test: /\.(svg|png|jpg|gif|woff|woff2|otf|ttf|eot)$/,
loader: 'file-loader',
},
];
const pluginsBase = [
new HtmlWebpackPlugin({ template: 'template.ejs' }),
new FaviconsWebpackPlugin(SETTINGS.FAVICONS),
new BundleAnalyzerPlugin({ analyzerMode: analyzer ? 'server' : 'disabled' }),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || ''),
},
}),
new CopyWebpackPlugin({
patterns: [{ from: 'static/' }, { from: 'node_modules/pdfjs-dist/cmaps/', to: 'cmaps/' }],
}),
];
const developmentPlugins = [
...pluginsBase,
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new BrowserSyncPlugin(
{
host: 'localhost',
port: SETTINGS.PORT,
proxy: `http://localhost:${SETTINGS.PORT}`,
notify: false,
},
{
reload: false,
},
),
];
const productionPlugins = [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [SETTINGS.PUBLIC_PATH],
}),
...pluginsBase,
new LodashModuleReplacementPlugin(),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new webpack.optimize.OccurrenceOrderPlugin(),
];
module.exports = {
devtool: production ? false : 'cheap-module-source-map',
mode: production ? 'production' : 'development',
optimization: {
minimize: production,
},
entry: production
? path.join(__dirname, './src/index')
: [
`webpack-dev-server/client?http://localhost:${SETTINGS.PORT}`,
'webpack/hot/only-dev-server',
path.join(__dirname, './src/index'),
],
output: {
path: SETTINGS.PUBLIC_PATH,
filename: 'bundle.js',
publicPath: '/',
globalObject: 'this',
},
resolve: {
modules: [path.join(__dirname, 'src'), 'node_modules'],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'react-dom': '@hot-loader/react-dom',
},
},
module: { rules },
plugins: production ? productionPlugins : developmentPlugins,
};