This repository has been archived by the owner on Dec 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwebpack.config.js
157 lines (139 loc) · 4.68 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
/**
* React Static Boilerplate
* https://github.com/kriasoft/react-static-boilerplate
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* eslint-disable global-require */
const path = require('path');
const webpack = require('webpack');
const ReactStaticPlugin = require('react-static-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const pkg = require('./package.json');
const isDebug = global.DEBUG === false ? false : !process.argv.includes('--release');
const isVerbose = process.argv.includes('--verbose') || process.argv.includes('-v');
const useHMR = !!global.HMR; // Hot Module Replacement (HMR)
const babelConfig = Object.assign({}, pkg.babel, {
babelrc: false,
cacheDirectory: useHMR,
});
const version = require('./version');
const appConfig = require('./config');
// Webpack configuration (main.js => public/dist/main.{hash}.js)
// http://webpack.github.io/docs/configuration.html
const config = {
// The base directory for resolving the entry option
context: __dirname,
// The entry point for the bundle
entry: [
/* The main entry point of your JavaScript application */
'./main.js',
],
// Options affecting the output of the compilation
output: {
path: path.resolve(__dirname, './build'),
publicPath: '/',
filename: '[name].js?[hash]',
chunkFilename: '[id].js?[chunkhash]',
sourcePrefix: ' ',
},
// Developer tool to enhance debugging, source maps
// http://webpack.github.io/docs/configuration.html#devtool
devtool: isDebug ? 'source-map' : false,
// What information should be printed to the console
stats: {
colors: true,
reasons: isDebug,
hash: isVerbose,
version: isVerbose,
timings: true,
chunks: isVerbose,
chunkModules: isVerbose,
cached: isVerbose,
cachedAssets: isVerbose,
},
// The list of plugins for Webpack compiler
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': isDebug ? '"development"' : '"production"',
__DEV__: isDebug,
}),
],
// Options affecting the normal modules
module: {
rules: [
{
test: /config\.js/,
loader: `preprocess-loader?version=${version}`,
},
{
test: /\.jsx?$/,
include: [
path.resolve(__dirname, './components'),
path.resolve(__dirname, './pages'),
path.resolve(__dirname, './utils'),
path.resolve(__dirname, './content.index.js'),
path.resolve(__dirname, './main.js'),
path.resolve(__dirname, './routes.js'),
],
loader: isDebug ?
`babel-loader?${JSON.stringify(babelConfig)}` :
`babel-loader?${JSON.stringify(babelConfig)}!strip-loader?strip[]=console.log`,
},
{
test: [/\.scss/, /\.css/],
loader: isDebug ?
'style-loader!css-loader!postcss-loader!sass-loader' :
ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader', 'sass-loader'],
}),
},
{
test: /\.json$/,
loader: 'json-loader',
},
{
test: /\.md$/,
loader: path.resolve(__dirname, './utils/markdown-loader.js'),
},
{
test: /\.(ttf|png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: 'url-loader?limit=10000',
},
{
test: /\.(eot|wav|mp3)$/,
loader: 'file-loader',
},
],
},
};
// Optimize the bundle in release (production) mode
if (!isDebug) {
config.plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: { warnings: isVerbose } })); // eslint-disable-line
config.plugins.push(new webpack.optimize.AggressiveMergingPlugin());
config.plugins.push(new ExtractTextPlugin({ filename: 'styles.css', allChunks: true }));
config.plugins.push(
new ReactStaticPlugin({
routes: './routes.js',
template: './components/HtmlTemplate/HtmlTemplate.js',
// custom props
stylesheet: '/styles.css',
gaTrackingID: appConfig.gaTrackingID,
mouseFlowProjectID: appConfig.mouseFlowProjectID,
googleTagManagerID: appConfig.googleTagManagerID,
})
);
}
// Hot Module Replacement (HMR) + React Hot Reload
if (isDebug && useHMR) {
config.entry.unshift('webpack-hot-middleware/client?path=/__webpack_hmr');
config.plugins.push(new webpack.HotModuleReplacementPlugin());
config.plugins.push(new webpack.NoEmitOnErrorsPlugin());
config.plugins.push(new CopyWebpackPlugin([{ from: 'content/assets', to: 'build/assets' }]));
}
module.exports = config;