-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
173 lines (147 loc) · 4.13 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
172
173
// We need to 'use strict' here because this file isn't compiled with babel
/* eslint strict:0 */
'use strict';
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
// default the environment to development
const NODE_ENV = process.env.NODE_ENV || 'development';
const IS_PRODUCTION = NODE_ENV === 'production';
const appPath = path.join(__dirname, 'app');
const assetsPath = path.join(__dirname, 'public');
const publicPath = '/';
function getPlugins() {
// These plugins are used in all environments
const plugins = [
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
template: 'app/index.tpl.html',
inject: 'body',
filename: 'index.html',
}),
// http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
];
// add plugins that should be used only in certain environments
if (IS_PRODUCTION) {
// http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
plugins.push(new webpack.optimize.UglifyJsPlugin({
minimize: true,
sourceMap: true,
}));
} else {
// http://webpack.github.io/docs/list-of-plugins.html#hotmodulereplacementplugin
plugins.push(new webpack.HotModuleReplacementPlugin());
}
return plugins;
}
function getLoaders() {
const loaders = [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
// Don't use .babelrc. Use the specified config below with webpack
babelrc: false,
// This disables babel's transformation of ES2015 module syntax.
// Doing so allows us to use Webpack 2's import system instead.
// https://webpack.js.org/guides/migrating/
presets: [['env', { modules: false }], 'stage-2', 'react'],
plugins: ['transform-strict-mode', 'react-hot-loader/babel'],
},
},
}, {
test: /\.svg/,
use: [{
loader: 'svg-url-loader',
}],
}, {
test: /(\.scss)$/,
exclude: /node_modules/,
use: [
{
loader: 'style-loader',
}, {
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
importLoaders: 1,
localIdentName: '[local]___[hash:base64:5]',
minimize: true,
},
}, {
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
}, {
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'eslint-loader',
}],
},
];
return loaders;
}
function getEntry() {
const entry = [];
// hot reload only when in non-production environment
if (!IS_PRODUCTION) {
// https://github.com/glenjamin/webpack-hot-middleware#config
entry.push('webpack-hot-middleware/client?reload=true');
}
// if TEST_APP then we build the test application, else the main one
if (process.env.TEST_APP) {
entry.push(path.join(appPath, 'test-app-index.js'));
} else {
entry.push(path.join(appPath, 'index.js'));
}
return entry;
}
function getOutput() {
let output;
// in production, we need a special output object
if (IS_PRODUCTION) {
output = {
path: assetsPath,
publicPath,
filename: '[name]-[hash].min.js',
};
} else {
output = {
path: assetsPath,
publicPath,
filename: '[name].js',
};
}
return output;
}
module.exports = {
// necessary per https://webpack.github.io/docs/testing.html#compile-and-test
target: NODE_ENV === 'test' ? 'node' : 'web',
// enable cache in non-production environments
cache: !IS_PRODUCTION,
// anything but 'source-map breaks Chrome dev tools so we use source-map'
// more info: https://webpack.github.io/docs/build-performance.html#sourcemaps
devtool: 'source-map',
resolve: {
modules: [
appPath,
'node_modules',
],
},
plugins: getPlugins(),
module: {
rules: getLoaders(),
},
entry: getEntry(),
output: getOutput(),
};