-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
227 lines (201 loc) · 4.95 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Webpack Configuration
===================================================================================================================== */
// Load Core Modules:
const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
// Load Plugin Modules:
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// Configure Paths:
const PATHS = {
ADMIN: {
SRC: path.resolve(__dirname, 'admin/client/src'),
DIST: path.resolve(__dirname, 'admin/client/dist'),
BUNDLES: path.resolve(__dirname, 'admin/client/src/bundles'),
PUBLIC: '{resource-path}/admin/client/dist/'
},
MODULE: {
SRC: path.resolve(__dirname, 'client/src'),
DIST: path.resolve(__dirname, 'client/dist'),
BUNDLES: path.resolve(__dirname, 'client/src/bundles'),
PUBLIC: '{resource-path}/client/dist/',
},
MODULES: path.resolve(__dirname, 'node_modules')
};
// Configure Style Loader:
const style = (env, loaders) => {
return (env === 'production') ? ExtractTextPlugin.extract({
fallback: 'style-loader',
use: loaders
}) : [{ loader: 'style-loader' }].concat(loaders);
};
// Configure Rules:
const rules = (env) => {
return [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader'
}
],
exclude: [ PATHS.MODULES ]
},
{
test: /\.css$/,
use: style(env, [
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
plugins: [ autoprefixer ] // see "browserslist" in package.json
}
}
])
},
{
test: /\.scss$/,
use: style(env, [
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
plugins: [ autoprefixer ] // see "browserslist" in package.json
}
},
{
loader: 'sass-loader',
options: {
includePaths: [
path.resolve(process.env.PWD, '../') // allows resolving of framework paths in symlinked modules
]
}
}
])
},
{
test: /\.(gif|jpg|png)$/,
use: [
{
loader: 'url-loader',
options: {
name: 'images/[name].[ext]',
limit: 10000
}
}
]
}
];
};
// Configure Devtool:
const devtool = (env) => {
return (env === 'production') ? false : 'source-map';
};
// Configure Plugins:
const plugins = (env, src, dist) => {
// Define Common Plugins:
var common = [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
];
// Define Admin-Only Plugins:
if (src === PATHS.ADMIN.SRC) {
common.push(
new CopyWebpackPlugin([
{ from: path.resolve(src, 'images/icons'), to: 'images/icons' }
])
);
}
// Answer Common + Environment-Specific Plugins:
return common.concat((env === 'production') ? [
new CleanWebpackPlugin([ dist ], {
verbose: true
}),
new ExtractTextPlugin({
filename: 'styles/[name].css',
allChunks: true
}),
new webpack.optimize.UglifyJsPlugin({
output: {
beautify: false,
comments: false,
semicolons: false
},
compress: {
unused: false,
warnings: false
}
})
] : [
]);
};
// Define Configuration:
const config = (env) => {
return [
{
entry: {
'bundle': path.resolve(PATHS.ADMIN.BUNDLES, 'bundle.js')
},
output: {
path: PATHS.ADMIN.DIST,
filename: 'js/[name].js',
publicPath: PATHS.ADMIN.PUBLIC
},
module: {
rules: rules(env)
},
devtool: devtool(env),
plugins: plugins(env, PATHS.ADMIN.SRC, PATHS.ADMIN.DIST),
resolve: {
alias: {
'silverstripe-admin': path.resolve(process.env.PWD, '../../silverstripe/admin/client/src')
},
modules: [
PATHS.ADMIN.SRC,
PATHS.MODULES
]
},
externals: {
jquery: 'jQuery'
}
},
{
entry: {
'bundle': path.resolve(PATHS.MODULE.BUNDLES, 'bundle.js')
},
output: {
path: PATHS.MODULE.DIST,
filename: 'js/[name].js',
publicPath: PATHS.MODULE.PUBLIC
},
module: {
rules: rules(env)
},
devtool: devtool(env),
plugins: plugins(env, PATHS.MODULE.SRC, PATHS.MODULE.DIST),
resolve: {
modules: [
PATHS.MODULE.SRC,
PATHS.MODULES
]
},
externals: {
jquery: 'jQuery'
}
}
];
};
// Define Module Exports:
module.exports = (env = {development: true}) => {
process.env.NODE_ENV = (env.production ? 'production' : 'development');
console.log(`Running in ${process.env.NODE_ENV} mode...`);
return config(process.env.NODE_ENV);
};