-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathwebpack.config.js
More file actions
161 lines (152 loc) · 5.8 KB
/
Copy pathwebpack.config.js
File metadata and controls
161 lines (152 loc) · 5.8 KB
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
// webpack.config.js
const webpack = require('webpack');
const path = require('path');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LiveReloadPlugin = require('webpack-livereload-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const NODE_ENV = process.env.NODE_ENV || 'production';
const isProductionMode = NODE_ENV === 'production';
const isWebpackDevServerMode = process.env.WEBPACK_DEV_SERVER_MODE === 'true';
console.log('\n================ webpack.config.js ================');
console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`);
console.log(`isProductionMode: ${isProductionMode}`);
console.log(`isWebpackDevServerMode: ${isWebpackDevServerMode}`);
console.log('===================================================\n');
// Load .env file
if (isProductionMode) {
const envFilePath = '.env.production';
const envLoadResult = require('dotenv').config({ path: envFilePath });
if (envLoadResult.error) {
console.log(`[WARNING] ${envLoadResult.error.message}\n`);
} else {
console.log(`env file '${envFilePath}' loaded successfully.\n`);
}
}
// Load StickyBoard config
const stickyboardConfig = require('./stickyboard.config');
console.log(`stickyboard.config env variables are loaded successfully.`);
if (!isProductionMode) {
console.log('[FRONT-END]', stickyboardConfig.env, '\n');
}
const config = {
mode: NODE_ENV,
entry: path.join(__dirname, 'src', 'index.js'),
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[contenthash].js',
publicPath: isWebpackDevServerMode ? '/' : '/dist/',
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-runtime',
[
'babel-plugin-transform-imports',
{
'@material-ui/core': {
transform:
'@material-ui/core/esm/${member}',
preventFullImport: true,
},
'@material-ui/icons': {
transform:
'@material-ui/icons/esm/${member}',
preventFullImport: true,
},
},
],
],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(ico|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
use: {
loader: 'file-loader',
options: {
name(file) {
if (process.env.NODE_ENV === 'development') {
return '[path][name].[ext]';
}
return '[contenthash].[ext]';
},
},
},
},
],
},
resolve: {
modules: ['src', 'node_modules'],
alias: {
react: path.resolve('./node_modules/react'),
},
symlinks: false,
},
plugins: [
new NodePolyfillPlugin(),
new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),
new webpack.DefinePlugin(
Object.keys(stickyboardConfig.env).reduce((acc, envKey) => {
const value = stickyboardConfig.env[envKey];
if (typeof value === 'number') {
acc[`process.env.${envKey}`] = value;
} else {
acc[`process.env.${envKey}`] = JSON.stringify(value);
}
return acc;
}, {})
),
// Ignore all locale files of moment.js
new webpack.WatchIgnorePlugin({ paths: [/^\.\/locale$/, /moment$/] }),
new HtmlWebpackPlugin({
...stickyboardConfig,
template: 'src/view/index.ejs',
}),
new LiveReloadPlugin({}),
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
// If you want to run bundle analyzer,
// release below comments with require statements above
// new BundleAnalyzerPlugin({
// analyzerHost: '127.0.0.1',
// analyzerPort: 9000
// })
]
};
if (!isProductionMode) {
config['devtool'] = 'inline-source-map';
}
// Add devServer config if the mode is webpack dev server mode
if (isWebpackDevServerMode) {
config['devServer'] = {
contentBase: path.join(__dirname, 'dist'),
hot: true,
inline: true,
compress: true,
public: 'localhost:8080',
};
}
module.exports = config;