-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebpack.config.js
More file actions
167 lines (147 loc) · 3.58 KB
/
webpack.config.js
File metadata and controls
167 lines (147 loc) · 3.58 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
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
// Strict mode.
'use strict'
// Dependencies.
const autoprefixer = require('autoprefixer')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const webpack = require('webpack')
// ================ //
// ================ //
// Default plugins. //
// ================ //
// ================ //
const plugins = [
// Generate "index.html" file.
new HtmlWebpackPlugin({
template: './source/index.html',
inject: 'body',
minify: {
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
removeCDATASectionsFromCDATA: true,
removeComments: true,
removeCommentsFromCDATA: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}
}),
/*
This adds the `window.fetch` Ajax helper.
Documentation here:
https://github.com/github/fetch
*/
new webpack.ProvidePlugin({
Promise: 'imports?this=>global!exports?global.Promise!es6-promise',
'window.Promise': 'imports?this=>global!exports?global.Promise!es6-promise',
fetch: 'imports?this=>global!exports?global.fetch!whatwg-fetch',
'window.fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
}),
// Bundle ".scss" in one ".css" file.
new ExtractTextPlugin('bundle.css', {
allChunks: true
})
]
// ============================= //
// ============================= //
// Plugins for production build. //
// ============================= //
// ============================= //
if (process.env.BABEL_ENV === 'production') {
/*
Set NODE_ENV, because some modules like
React/Redux look for this in their code.
Without adding this, the build process
will just minify their "dev" versions.
*/
plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
)
// Minify JS.
plugins.push(
new webpack.optimize.UglifyJsPlugin({
minimize: true
})
)
// Copy over "static" folder.
plugins.push(
new CopyWebpackPlugin([
{
// Input: "/source/static".
from: './source/static',
// Output: "/build/static".
to: './static'
}
])
)
}
// =============================== //
// =============================== //
// Loaders for various file types. //
// =============================== //
// =============================== //
// Determine *.scss loader.
function sassLoader () {
let x = 'style!css?-url!postcss!sass'
if (process.env.BABEL_ENV === 'production') {
x = ExtractTextPlugin.extract('style', 'css?-url&minimize!postcss!sass')
}
return x
}
const loaders = [
// JSON.
{
test: /\.json$/,
loader: 'json-loader',
exclude: /node_modules/
},
// JavaScript.
{
test: /\.js$/,
loaders: ['react-hot', 'babel'],
exclude: /node_modules/
},
// CSS.
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(
'style-loader',
'css-loader?minimize',
'postcss-loader'
)
},
// Sass.
{
test: /\.scss$/,
loader: sassLoader()
}
]
// ======================== //
// ======================== //
// Export Webpack settings. //
// ======================== //
// ======================== //
module.exports = {
// Development code.
entry: [
'./source'
],
// Production build.
output: {
path: path.join(__dirname, 'build'),
filename: 'bundle.js'
},
module: {
loaders: loaders
},
plugins: plugins,
postcss: [
autoprefixer
]
}