-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
161 lines (155 loc) · 5.25 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
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
var autoprefixer = require('autoprefixer');
var cssExtractor = new ExtractTextWebpackPlugin('styles/[name].css');
var lifecycleEvent = process.env.npm_lifecycle_event;
require('dotenv').config();
/*
INITIAL CONFIG
*/
var devConfig = {
entry: './src/app.jsx',
output: {
publicPath: '/',
path: __dirname + '/public',
filename: 'js/app.js'
},
devtool: 'source-map',
module: {
// Pre Loaders run before the transpiler. They're great for unit testing, code linting, etc.
preLoaders: [
{
test: /\.jsx?$/,
loaders: ['eslint'],
exclude: /node_modules/
}
],
// Loaders are what manage all of your actual code and assets.
loaders: [
{ // CSS/Sass loader config
test: /\.s?css$/,
loaders: ['style', 'css', 'postcss', 'sass']
},
{ // ES6 loader config
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: ['babel']
},
{ // Import fonts
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loaders: ['file?name=fonts/[name].[ext]']
}
]
},
// Post CSS is a nifty plugin for normalizing your styles. Often times, 3rd-party plugins like this one will require their own config block.
postcss: [
autoprefixer({ browsers: ['last 3 versions'] }) // Automatically adds vendor prefixes for x browser versions (and all vendors). :D
],
plugins: [
// HtmlWebpackPlugin is what Automatically injects your styles and javascript into your index.html file.
new HtmlWebpackPlugin({
title: 'Webpack Build',
template: './src/index.html'
}),
// We're letting webpack know that we're in a development environment
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"development"'
})
],
devServer: {
historyApiFallback: true,
contentBase: './public',
proxy: {
// Proxy the url /api to an external API. This way you don't have to install the server on your computer and can get coding faster.
'/api': {
target: process.env.API_HOST || 'localhost',
xfwd: true,
changeOrigin: true
}
}
}
}
var buildConfig = {
entry: './src/app.jsx',
output: {
publicPath: '/',
path: __dirname + '/public',
filename: 'js/app.js'
},
devtool: 'source-map',
module: {
// Pre Loaders run before the transpiler. They're great for unit testing, code linting, etc.
preLoaders: [
{
test: /\.jsx?$/,
loaders: ['eslint'],
exclude: /node_modules/
}
],
// Loaders are what manage all of your actual code and assets.
loaders: [
{ // CSS/Sass loader config
test: /\.s?css$/,
loader: cssExtractor.extract(['css', 'postcss', 'sass'])
},
{ // ES6 loader config
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: ['babel']
},
{ // Import fonts
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loaders: ['file?name=fonts/[name].[ext]']
}
]
},
// Post CSS is a nifty plugin for normalizing your styles. Often times, 3rd-party plugins like this one will require their own config block.
postcss: [
autoprefixer({ browsers: ['last 3 versions'] }) // Automatically adds vendor prefixes for x browser versions (and all vendors). :D
],
plugins: [
// HtmlWebpackPlugin is what Automatically injects your styles and javascript into your index.html file.
new HtmlWebpackPlugin({
title: 'Webpack Build',
template: './src/index.html'
}),
// We're letting webpack know that we're in a development environment
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new CleanWebpackPlugin(['public/fonts', 'public/js', 'public/styles', 'public/index.html']),
cssExtractor,
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true,
minimize: true
})
],
devServer: {
historyApiFallback: true,
contentBase: './public',
proxy: {
// Proxy the url /api to an external API. This way you don't have to install the server on your computer and can get coding faster.
'/api': {
target: process.env.API_HOST || 'localhost',
xfwd: true,
changeOrigin: true
}
}
}
}
/*
SELECT WHICH CONFIG TO USE
*/
switch (lifecycleEvent) {
case 'build':
module.exports = buildConfig;
break;
default:
module.exports = devConfig;
break;
}