This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
171 lines (161 loc) · 4.23 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
168
169
170
171
const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');
const childProcess = require('child_process');
// Webpack plugins
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// Read `package.json` file
const pkg = require('./package.json');
// Define some constants
const TARGET = process.env.npm_lifecycle_event;
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build')
};
const VERSION = (() => {
var v;
try {
v = process.env.SOURCE_VERSION || process.env.SHA || childProcess.execSync('git rev-parse HEAD').toString();
} catch (e) {
// occurs with Heroku deploy button for instance
v = 'unknown';
}
return v;
})();
const PORT = process.env.PORT || 3000;
// Common config, shared by all "targets"
const common = {
// Entry points are used to define "bundles"
entry: {
app: PATHS.app
},
// Extensions that should be used to resolve module
//
// - `''` is needed to allow imports without an extension
// - note the `.` before extensions as it will fail to match without!
resolve: {
extensions: ['.js', '.jsx']
},
// Tells Webpack how to write the compiled files to disk
// Note, that while there can be multiple entry points, only one output
// configuration is specified
output: {
path: PATHS.build,
// `[name]` is replaced by the name of the chunk
filename: '[name].js'
},
module: {
rules: [
{
test: /\.jsx?$/,
use: [
{ loader: 'babel-loader' },
],
include: [PATHS.app],
},
{
test: /\.(ttf|eot|svg|woff(2)?)(\?v=.+)?$/,
use: [
'file-loader?name=fonts/[name].[ext]',
],
include: [
path.join(__dirname, 'node_modules/font-awesome/fonts/'),
]
},
{
test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
use: [
'file-loader?name=[path][name].[ext]&context=./app',
],
include: [PATHS.app],
}
]
},
// Plugins do not operate on individual source files: they influence the
// build process as a whole
plugins: [
new HtmlWebpackPlugin({
template: 'index.ejs',
title: pkg.name,
version: VERSION.substring(0, 7),
favicon: 'app/favicon.ico',
appMountId: 'root'
})
]
};
// Default configuration
if (TARGET === 'dev' || !TARGET) {
module.exports = merge(common, {
devtool: 'eval-source-map',
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:' + PORT,
'webpack/hot/only-dev-server',
PATHS.app
],
module: {
rules: [
{
test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader',
include: [PATHS.app],
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
]
});
}
// Build for production
if (TARGET === 'build') {
module.exports = merge(common, {
output: {
path: PATHS.build,
// Set up caching by adding cache busting hashes to filenames
// `[chunkhash]` returns a chunk specific hash
filename: '[name].[chunkhash].js',
// The filename of non-entry chunks
chunkFilename: '[chunkhash].js'
},
module: {
rules: [
// Extract CSS during build
{
test: /\.(css|scss)$/,
use: ExtractTextPlugin.extract({
use: [
'css-loader',
'sass-loader',
],
fallback: 'style-loader',
}),
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new CleanPlugin([ PATHS.build ]),
new ExtractTextPlugin({
filename: '[name].[chunkhash].css',
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.LoaderOptionsPlugin({
minimize: true,
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
compress: {
// Ignore warning messages are they are pretty useless
warnings: false
}
})
]
});
}