forked from soaple/corona-board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.app.js
119 lines (112 loc) · 3.98 KB
/
webpack.app.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
// webpack.dev.js
const webpack = require('webpack')
const path = require('path')
const NODE_ENV = process.env.NODE_ENV || 'production';
const isProductionMode = NODE_ENV === 'production';
const isWebpackDevServerMode = process.env.WEBPACK_DEV_SERVER_MODE === 'true';
console.log('================ webpack.app.js ================');
console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`)
console.log(`isProductionMode: ${isProductionMode}`);
console.log(`isWebpackDevServerMode: ${isWebpackDevServerMode}`);
console.log('================================================');
// Load .env configuration
const envFilePath = isProductionMode ? '.env' : '.env.development';
const envLoadResult = require('dotenv').config({ path: envFilePath }).parsed;
if (envLoadResult.error) {
throw envLoadResult.error;
}
// reduce it to an object
const envKeys = Object.keys(envLoadResult).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(envLoadResult[next]);
return prev;
}, {});
const config = {
mode: NODE_ENV,
entry: path.join(__dirname, 'src', 'index.js'),
output: {
path: path.join(__dirname, 'dist'),
filename: 'app.bundle.js',
publicPath: isWebpackDevServerMode ? 'http://localhost:8080/' : '/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$/,
loaders: [
'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 webpack.DllReferencePlugin({
context: '.',
manifest: require(path.join(__dirname, 'dist', 'lib-manifest.json'))
}),
new webpack.DefinePlugin(envKeys),
// Ignore all locale files of moment.js
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
],
node: {
fs: 'empty'
}
}
// 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