-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
167 lines (149 loc) · 3.61 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
import path from 'path';
import webpack from 'webpack';
import envHelper from './utils/envHelper';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import HtmlWebpackHarddiskPlugin from 'html-webpack-harddisk-plugin';
const isProduction = process.argv.indexOf('-p') !== -1;
const parsedEnvs = envHelper('./.env');
const getEntries = () => {
if (!isProduction) {
return [
'react-hot-loader/patch',
// activate HMR for React
'webpack-dev-server/client?http://localhost:8080',
// bundle the client for webpack-dev-server
// and connect to the provided endpoint
'webpack/hot/only-dev-server',
// bundle the client for hot reloading
// only- means to only hot reload for successful updates
'./index.js',
];
} else {
return ['./index.js'];
}
};
let plugins = [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': (isProduction) ? '"production"' : '""',
...parsedEnvs
}
}),
new HtmlWebpackPlugin({
title: 'React BKND',
template: 'index.html',
filename: '../index.html',
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin(),
];
if (!isProduction) {
plugins = [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
...plugins,
];
} else {
plugins = [
new ExtractTextPlugin({
filename: 'style.css',
allChunks: true
}),
...plugins,
]
}
const sassProdConfig = {
test: /\.(sass|scss|css)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
// modules: true,
// localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
},
'postcss-loader',
'sass-loader',
],
}),
};
const sassDevConfig = {
test: /\.(sass|scss|css)$/,
use: [
'style-loader',
'css-loader?importLoaders=1',
'postcss-loader',
'sass-loader'
],
}
const config = {
context: path.resolve(__dirname, 'client'),
devtool: (isProduction) ? false : 'source-map',
entry: getEntries(),
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist/assets'),
publicPath: '/assets',
},
resolve: {
alias: {
node_modules: path.resolve(__dirname, 'node_modules'),
Utils: path.resolve(__dirname, 'client/utils/'),
Styles: path.resolve(__dirname, 'client/styles/')
}
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
publicPath: '/assets',
hot: true,
historyApiFallback: true
},
performance: {
hints: false,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
}],
},
{
test: /\.(jpe?g|png|gif)$/i,
loaders: ['file-loader?context=src/images&name=images/[path][name].[ext]', {
loader: 'image-webpack-loader',
query: {
mozjpeg: {
progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 4,
},
pngquant: {
quality: '75-90',
speed: 3,
},
},
}],
exclude: /node_modules/,
include: __dirname,
},
{
test: /\.(svg)$/,
use: ['svg-url-loader'],
}
],
},
plugins: plugins,
};
const sassConfig = (isProduction) ? sassProdConfig : sassDevConfig;
config.module.rules.push(sassConfig);
module.exports = config;