forked from linuswillner/react-console-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
144 lines (124 loc) Β· 3.6 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
require('colors')
const path = require('path')
const webpack = require('webpack')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const ExtractCSSWebpackPlugin = require('mini-css-extract-plugin')
const OptimizeCSSWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const OptimizeJSWebpackPlugin = require('terser-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const ProgressBarWebpackPlugin = require('progress-bar-webpack-plugin')
const ErrorOverlayWebpackPlugin = require('error-overlay-webpack-plugin')
const dev = process.env.NODE_ENV !== 'production' || process.argv.indexOf('-p') === -1
const HTMLInjecterConfig = new HTMLWebpackPlugin({
template: path.join(__dirname, '/demo/index.html'),
filename: 'index.html',
inject: 'body'
})
const CSSExtracterConfig = new ExtractCSSWebpackPlugin({
filename: dev ? '[name].css' : '[name].[hash].css',
chunkFilename: dev ? '[id].css' : '[id].[hash].css'
})
const JSOptimizerConfig = new OptimizeJSWebpackPlugin({
cache: true,
parallel: true,
sourceMap: true
})
const CSSOptimizerConfig = new OptimizeCSSWebpackPlugin({})
const ProgressBarConfig = new ProgressBarWebpackPlugin({
format: `${':msg'.cyan} [:bar] ${':percent'.green} (${':elapsed'.green} seconds)`,
clear: false
})
const EnvironmentConfig = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
const devPlugins = [
HTMLInjecterConfig,
CSSExtracterConfig,
new ErrorOverlayWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
]
const prodPlugins = [
HTMLInjecterConfig,
CSSExtracterConfig,
EnvironmentConfig,
new webpack.ProgressPlugin()
]
// If clean build is desired, add CleanWebpackPlugin
if (process.argv.indexOf('-c') !== -1) prodPlugins.push(new CleanWebpackPlugin())
// If in CI, don't output progress to stdout to reduce log clutter
if (!process.env.CI) prodPlugins.push(ProgressBarConfig)
// Helper for easier alias creation
// const createAlias = modulePath => path.resolve(__dirname, modulePath)
// Webpack config
module.exports = {
// Development server
devServer: {
host: 'localhost',
port: 8000,
hot: true,
open: (!process.env.TEST && !process.env.CI) && true,
headers: {
'Access-Control-Allow-Origin': '*' // Allow CORS
}
},
// Production optimisers
optimization: {
minimizer: dev ? [] : [JSOptimizerConfig, CSSOptimizerConfig]
},
// Entry point
entry: [
'react-hot-loader/patch',
path.join(__dirname, '/demo/index.jsx')
],
// Dummies for native Node modules not present in browser scope
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
crypto: 'empty'
},
// Loaders
module: {
rules: [
{ // JSX
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{ // SCSS
test: /\.scss$/,
use: [
dev ? 'style-loader' : ExtractCSSWebpackPlugin.loader,
'css-loader',
'sass-loader'
]
},
{ // Images
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'url-loader',
options: {
limit: 10000
}
}
]
},
// Extension config
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'react-dom': '@hot-loader/react-dom' // DOM patches for react-hot-loader
// Internal shortcuts
}
},
// Production build
output: {
filename: 'index.js',
path: path.join(__dirname, '/build')
},
devtool: 'cheap-module-source-map',
mode: dev ? 'development' : 'production',
plugins: dev ? devPlugins : prodPlugins
}