-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
299 lines (268 loc) · 8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Created by arey on 4/28/17.
*/
const { isDevelopment, getEnvironment } = require('./helpers/environmentHelper');
const config = require('config');
const webpack = require('webpack');
const fs = require('fs');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const port = config.get('app.port');
const isAdaptive = config.get('adaptive');
const baseDirectory = __dirname;
const pagesDirectory = path.join(baseDirectory, 'app', 'pages');
// List of extract text plugins
const extractTextPlugins = [];
// Creating extract text plugins for CSS on specific devices
if (isAdaptive) {
extractTextPlugins.push(new ExtractTextPlugin({
filename: getOuputName('css', 'desktop'),
}));
extractTextPlugins.push(new ExtractTextPlugin({
filename: getOuputName('css', 'mobile'),
}));
} else {
extractTextPlugins.push(new ExtractTextPlugin({
filename: getOuputName('css'),
}));
}
module.exports = {
entry: Object.assign({}, pageEntryPoint({
index: [
path.join(baseDirectory, '/app/client/index.js'),
],
adaptive: [
path.join(baseDirectory, '/app/client/adaptive.js'),
],
error: [
path.join(baseDirectory, '/app/client/error.js'),
]
}), {
vendor: [
'react', 'react-dom', 'react-declarative-head'
]
}),
// the bundle file we will get in the result
output: {
publicPath: 'http://localhost:' + port + '/',
path: path.join(baseDirectory, 'bundles'),
filename: getOuputName('js'),
},
module: {
loaders: getLoaders(),
},
plugins: pluginsToLoad(),
target: 'web'
};
function getLoaders() {
const loaders = [];
// Adding babel common loader
loaders.push({
loader: 'babel-loader',
test: /\.js?$/,
include: path.join(baseDirectory),
exclude: /\/node_modules\//,
query: {
env: {
development: {
presets: ['react-hmre'],
plugins: [
[
'react-transform', {
transforms: [
{
transform: 'react-transform-hmr',
imports: ['react'],
locals: ['module']
}
]
}
]
]
}
},
}
});
// If it is adaptive get the loaders for desktop and mobile style loaders
if (isAdaptive) {
loaders.push(getStyleLoader('desktop'));
loaders.push(getStyleLoader('mobile'));
} else {
loaders.push(getStyleLoader());
}
return loaders;
}
/**
* Add modules to entry point only when its development
* @param entry
* @returns {*}
*/
function pageEntryPoint(entry) {
const devEntries = ['react-hot-loader/patch', 'webpack-hot-middleware/client'];
if (typeof entry !== 'object') {
throw new Error('pageEntryPoint must receive an Object and the name of the entry needs to match the page name (folder)');
} else if (typeof entry === 'object') {
Object.keys(entry).forEach((pageName) => {
// Get styles entries
const stylesEntries = getStyles(pageName);
// Convert to array if is not an array
entry[pageName] = Array.isArray(entry[pageName]) ? entry[pageName] : [entry[pageName]];
if (isDevelopment()) {
entry[pageName] = devEntries.concat(entry[pageName]);
}
// If the page have styles add them to the entry point list
if (stylesEntries.length > 0) {
entry[pageName] = entry[pageName].concat(stylesEntries);
}
});
return entry;
} else {
throw new Error(`Expected entry point to be object, array or string. Instead got: ${entry}`);
}
}
/**
* Get styles depending on if it is adaptive or not
* @param pageId
* @returns {Array}
*/
function getStyles(pageId) {
const stylesEntries = [];
const pageDirectory = path.join(pagesDirectory, pageId);
if (isAdaptive) {
const desktopPath = path.join(pageDirectory, 'styles', 'index.desktop.scss');
const mobilePath = path.join(pageDirectory, 'styles', 'index.mobile.scss');
if (fs.existsSync(desktopPath) && fs.existsSync(mobilePath)) {
stylesEntries.push(desktopPath, mobilePath);
} else {
console.log(`You have adaptive configuration enable, you must add a index.desktop.scss and index.mobile.scss for "${pageId}" on your page directory. This is just a warning, no css were build`);
}
} else {
const stylePath = path.join(pageDirectory, 'styles', 'index.scss');
if (fs.existsSync(stylePath)) {
stylesEntries.push(stylePath);
} else {
console.log('You dont have any styles file. Remember to add index.scss on your page directory');
}
}
return stylesEntries;
}
/**
* Get styles loaders depending on if it is adaptive or not
* @param device
* @returns {{test: RegExp, use: Array.<string>}}
*/
function getStyleLoader(device) {
let extractPlugin;
// If a device is sent add the extract plugin for the specific device
if (device) {
// Fint the correct extract text depending on the device
const pattern = new RegExp(`\.${device}\.`);
extractPlugin = extractTextPlugins.filter(extract => pattern.test(extract.filename))[0];
// Generate the device pattern for the extract text plugin
const loaderPattern = new RegExp(`${device}\.scss$`);
return {
test: loaderPattern,
use: ['css-hot-loader'].concat(
extractPlugin.extract({
use: [{
loader: 'css-loader',
options: {
minimize: !isDevelopment()
}
}, {
loader: 'autoprefixer-loader'
}, {
loader: 'sass-loader',
}],
fallback: 'style-loader'
})
)
};
}
const pattern = new RegExp(`[\.scss$|\.css$]`);
extractPlugin = extractTextPlugins.filter(extract => pattern.test(extract.filename))[0];
return {
test: /\.scss$/,
use: ['css-hot-loader'].concat(
extractPlugin.extract({
use: [{
loader: 'css-loader',
options: {
minimize: !isDevelopment()
}
}, {
loader: 'autoprefixer-loader'
}, {
loader: 'sass-loader',
}],
fallback: 'style-loader'
})
)
};
}
/**
* Get the plugins to load
* @returns {[*,*]}
*/
function pluginsToLoad() {
let plugins = [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(getEnvironment())
}
})
];
// Add all the extract text plugins
plugins = plugins.concat(extractTextPlugins);
if (isDevelopment()) {
plugins = plugins.concat([
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]);
} else {
// This must be the first plugin
plugins.unshift(
new CleanWebpackPlugin([path.join(baseDirectory, 'bundles')], {
verbose: true,
allowExternal: true
})
);
plugins = plugins.concat([
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false
},
screwIe8: true,
sourceMap: false
}),
new ManifestPlugin({
map: (el) => {
// If it is CSS and adaptive mode is enable. Rename the chuck name to generate two different files on manifest
if (isAdaptive && /\.css$/.test(el.path)) {
const isMobile = /\.(mobile)\./.test(el.path);
const isDesktop = /\.(desktop)\./.test(el.path);
// Change the chunk name to generate a different entry on manifest
el.name = `${el.chunk.name}.${isDesktop ? 'desktop' : isMobile ? 'mobile' : ''}.css`;
}
return el;
}
}),
]);
}
return plugins;
}
function getOuputName(extension, device) {
if (device) {
return `[name].${device}${!isDevelopment() ? '.[hash]' : ''}.${extension}`;
}
return `[name]${!isDevelopment() ? '.[hash]' : ''}.${extension}`;
}