-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
134 lines (126 loc) · 3.32 KB
/
webpack.config.js
File metadata and controls
134 lines (126 loc) · 3.32 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
const { resolve } = require('path')
const {
HotModuleReplacementPlugin, NamedModulesPlugin, DefinePlugin, ProvidePlugin,
optimize: { AggressiveMergingPlugin, CommonsChunkPlugin },
} = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const merge = require('webpack-merge')
const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const MinifyPlugin = require('babel-minify-webpack-plugin')
require('dotenv').config()
const STYLE_OPTIONS = [
{
loader: 'css-loader',
options: { importLoaders: 1 },
},
'postcss-loader',
'sass-loader',
]
const BASE = {
entry: {
},
output: {
filename: '[name].js',
path: resolve(__dirname, 'dist'),
publicPath: '/',
},
context: resolve(__dirname, 'src'),
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
path.resolve(__dirname, 'src'),
],
},
{
test: /\.(scss|sass)$/,
use: process.env.NODE_ENV === 'production' ?
ExtractTextPlugin.extract({ fallback: 'style-loader', use: STYLE_OPTIONS }) :
['style-loader', ...STYLE_OPTIONS],
},
{
test: /\.(css)$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(svg)$/,
loader: 'svg-react-loader',
query: {
props: { className: 'icon' },
},
},
],
},
plugins: [
new CopyWebpackPlugin([{ from: resolve(__dirname, 'src', 'public'), to: '' }]),
new HtmlWebpackPlugin({ template: 'index.ejs' }),
new DefinePlugin(Object.keys(process.env).reduce((obj, key) => {
obj[`process.env.${key}`] = `"${process.env[key]}"`
return obj
}, {})),
new ProvidePlugin({
'URLSearchParams': 'url-search-params',
'window.fetch': 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch',
}),
],
}
if (process.env.NODE_ENV === 'production') {
module.exports = merge(BASE, {
entry: {
bundle: [
'./index.js',
],
},
output: { filename: '[name]-[chunkhash].js' },
devtool: false,
plugins: [
new CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor-[chunkhash].js',
minChunks(module) {
const context = module.context
return context && context.indexOf('node_modules') >= 0
},
}),
new ExtractTextPlugin('styles-[contenthash].css'),
new AggressiveMergingPlugin(),
new MinifyPlugin(),
],
})
} else {
module.exports = merge(BASE, {
entry: {
bundle: [
'react-hot-loader/patch',
'./index.js',
],
},
performance: { hints: false },
devtool: 'inline-source-map',
devServer: {
port: 8090,
hot: true,
contentBase: resolve(__dirname, 'dist'),
publicPath: '/',
disableHostCheck: true,
historyApiFallback: true,
},
plugins: [
new CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor.js',
minChunks(module) {
const context = module.context
return context && context.indexOf('node_modules') >= 0
},
}),
new ExtractTextPlugin('styles.css'),
new HotModuleReplacementPlugin(),
new NamedModulesPlugin(),
],
})
}