-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.prod.config.js
135 lines (123 loc) · 3.65 KB
/
webpack.prod.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
/* eslint-disable import/no-extraneous-dependencies */
// Initialization
const webpack = require('webpack');
// File ops
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// Folder ops
const CleanPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
// PostCSS support
const postcssImport = require('postcss-easy-import');
const precss = require('precss');
const autoprefixer = require('autoprefixer');
/* eslint-enable import/no-extraneous-dependencies */
// Constants
const APP = path.join(__dirname, 'app');
const BUILD = path.join(__dirname, 'build');
const STYLE = path.join(__dirname, 'app/style.css');
const PUBLIC = path.join(__dirname, 'app/public');
const TEMPLATE = path.join(__dirname, 'app/templates/index.html');
const NODE_MODULES = path.join(__dirname, 'node_modules');
const PACKAGE = Object.keys(
require('./package.json').dependencies
);
module.exports = {
entry: {
app: APP,
style: STYLE,
vendor: PACKAGE
},
resolve: {
extensions: ['', '.js', '.jsx', '.css']
},
output: {
path: BUILD,
filename: '[name].[chunkhash].js',
chunkFilename: '[chunkhash].js',
publicPath: '/'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loaders: ['babel?cacheDirectory'],
include: APP
},
// Extract CSS during build
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css!postcss'),
include: [APP, NODE_MODULES]
},
// Process JSON data fixtures
{
test: /\.json$/,
loader: 'json',
include: [APP, NODE_MODULES]
}
]
},
// Configure PostCSS plugins
postcss: function processPostcss(webpack) { // eslint-disable-line no-shadow
return [
postcssImport({
addDependencyTo: webpack
}),
precss,
autoprefixer({ browsers: ['last 2 versions'] })
];
},
// Remove comment if you require sourcemaps for your production code
// devtool: 'cheap-module-source-map',
plugins: [
// Required to inject NODE_ENV within React app.
// Reduntant package.json script entry does not do that, but required for .babelrc
// Optimizes React for use in production mode
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production') // eslint-disable-line quote-props
}
}),
// Clean build directory
new CleanPlugin([BUILD]),
new CopyWebpackPlugin([
{ from: PUBLIC, to: BUILD }
],
{
ignore: [
// Doesn't copy Mac storage system files
'.DS_Store'
]
}
),
// Auto generate index.html
new HtmlWebpackPlugin({
template: TEMPLATE,
// JS placed at the bottom of the body element
inject: 'body',
// Use html-minifier
minify: {
collapseWhitespace: true
}
}),
// Extract CSS to a separate file
new ExtractTextPlugin('[name].[chunkhash].css'),
// Remove comment to dedupe duplicating dependencies for larger projects
// new webpack.optimize.DedupePlugin(),
// Separate vendor and manifest files
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
// Minify JavaScript
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
// Error: vendor.450f275….js:16 Uncaught TypeError: Cannot read property 'shape' of undefined
// Error: manifest.798b47f….js:1 Uncaught TypeError: Cannot read property 'call' of undefined
// new webpack.optimize.OccurrenceOrderPlugin()
]
};