-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
173 lines (155 loc) · 4.49 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
/* eslint-disable compat/compat */
const nodePath = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const getGitInfo = require('git-repo-info');
const pkg = require('./package.json');
const git = getGitInfo();
const base = __dirname;
const src = nodePath.resolve(base, 'src');
const dist = nodePath.resolve(base, 'dist');
const build = nodePath.resolve(base, 'build');
const content = nodePath.resolve(base, 'public');
const path = { base, src, dist, content, build };
const image = [/\.(bmp|gif|jpe?g|png|svg)$/];
const font = [/\.(woff|woff2|ttf|eot|svg)(\?t=[0-9]+)?$/];
const html = [/\.html$/];
const json = [/\.json$/];
const source = [/\.(js|jsx)$/];
const style = [/\.css$/];
const type = { image, font, html, json, source, style };
const protocol = 'http:';
const host = '0.0.0.0';
const port = 3000;
const config = {
protocol,
host,
port,
};
function createDevprod(env = 'production') {
return function devprod(dev, prod) {
return env === 'production' ? prod : dev;
};
}
module.exports = function webpackConfig(env = {}) {
const devprod = createDevprod(env.NODE_ENV);
return {
mode: devprod('development', 'production'),
// mode: 'development',
context: path.base,
devtool: devprod(
'cheap-module-source-map',
'hidden-cheap-module-source-map'
),
entry: {
[pkg.name]: ['normalize.css', './src/index.js'],
},
output: {
path: devprod(path.dist, path.build),
filename: `[name].${git.sha}.js`,
chunkFilename: `[name].[contenthash].js`,
publicPath: devprod('/', '/static/survey/'),
},
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
runtimeChunk: devprod(undefined, 'single'),
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
devServer: devprod({
contentBase: path.content,
historyApiFallback: true,
host: config.host,
hot: true,
overlay: true,
port: config.port,
useLocalIp: true,
}),
module: {
rules: [
{
exclude: [
...type.html,
...type.json,
...type.image,
...type.source,
...type.style,
],
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
test: type.image,
loader: 'url-loader',
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
test: type.source,
include: [
nodePath.join(base, 'node_modules/react-intl'),
nodePath.join(base, 'node_modules/intl-messageformat'),
nodePath.join(base, 'node_modules/intl-messageformat-parser'),
],
loader: 'babel-loader',
// options: {
// rootMode: 'upward',
// },
},
{
test: type.source,
exclude: [/node_modules/],
loader: 'babel-loader',
// options: {
// rootMode: 'upward',
// },
},
{
test: type.style,
use: [
devprod('style-loader', { loader: MiniCssExtractPlugin.loader }),
'css-loader',
],
},
],
},
plugins: [
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(pkg.version),
__BASE_PATH__: JSON.stringify(pkg.basePath),
__DEVELOPMENT__: devprod(true, false),
__GIT_BRANCH__: JSON.stringify(git.branch),
__GIT_REVISION__: JSON.stringify(git.sha),
}),
new webpack.HashedModuleIdsPlugin(),
new MiniCssExtractPlugin({
filename: `[name].[contenthash].css`,
chunkFilename: `[name].[contenthash].css`,
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
inject: true,
template: nodePath.resolve(path.content, 'index.html'),
}),
],
resolve: {
modules: [path.src, 'node_modules'],
extensions: ['.js', '.jsx', '.json'],
},
};
};