-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpackssr.config.js
executable file
·352 lines (318 loc) · 11.1 KB
/
webpackssr.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
require('dotenv').config();
const cssnano = require('cssnano')
const extend = require('extend')
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const AssetsPlugin = require('assets-webpack-plugin');
const pkg = require(path.resolve(process.cwd(), './package.json'));
const debug = process.env.NODE_ENV === 'development';
const verbose = process.env.VERBOSE === 'true';
const hmr = process.env.HMR === 'true';
const babelConfig = Object.assign({}, pkg.babel, {
babelrc: false,
cacheDirectory: hmr,
});
const AUTOPREFIXER_BROWSERS = [
'Android >= 4',
'Chrome >= 35',
'Firefox >= 34',
'Explorer >= 9',
'iOS >= 7',
'Opera >= 12',
'Safari >= 7.1',
];
const exposevar = {
'process': {
'env': {
'BROWSER': true,
'WEB_HOST': JSON.stringify(process.env['WEB_HOST']),
'WEB_PORT': JSON.stringify(process.env['WEB_PORT']),
'API_HOST': JSON.stringify(process.env['API_HOST']),
'API_PORT': JSON.stringify(process.env['API_PORT']),
'IO_HOST': JSON.stringify(process.env['IO_HOST']),
'IO_PORT': JSON.stringify(process.env['IO_PORT'])
}
}
}
console.log(`babel-loader?${JSON.stringify(babelConfig)}`)
let BASE_CSS_LOADER;
if (!debug)
BASE_CSS_LOADER = 'css?-minimize'
else
BASE_CSS_LOADER = 'css?sourceMap&-minimize'
// Add any packge names here whose styles need to be treated as CSS modules.
// These paths will be combined into a single regex.
const PATHS_TO_TREAT_AS_CSS_MODULES = [
// 'react-toolbox', (example)
]
// If config has CSS modules enabled, treat this project's styles as CSS modules.
PATHS_TO_TREAT_AS_CSS_MODULES.push(
process.cwd().replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g, '\\$&') // eslint-disable-line
)
const localIdentName= debug ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]';
const cssModulesLoader = [
BASE_CSS_LOADER,
'modules',
'importLoaders=1',
'localIdentName='+localIdentName
// 'localIdentName=[name]__[local]___[hash:base64:5]'
].join('&')
const isUsingCSSModules = !!PATHS_TO_TREAT_AS_CSS_MODULES.length
const cssModulesRegex = new RegExp(`(${PATHS_TO_TREAT_AS_CSS_MODULES.join('|')})`)
const configvar={
'process.env.NODE_ENV': debug ? '"development"' : '"production"',
__DEV__: debug,
}
// Webpack configuration (main.js => public/dist/main.{hash}.js)
// http://webpack.github.io/docs/configuration.html
const webpackConfig = {
plugins: [],
context: process.cwd(),
// Switch loaders to debug or release mode
debug: debug,
// Developer tool to enhance debugging, source maps
// http://webpack.github.io/docs/configuration.html#devtool
devtool: debug ? 'source-map' : false,
resolve: {
},
cache: false,
// Options affecting the normal modules
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
include: [
path.resolve(process.cwd(), './actions'),
path.resolve(process.cwd(), './components'),
path.resolve(process.cwd(), './core'),
path.resolve(process.cwd(), './pages'),
path.resolve(process.cwd(), './routes'),
path.resolve(process.cwd(), ''),
path.resolve(process.cwd(), './main.js'),
],
loader: `babel-loader?${JSON.stringify(babelConfig)}`,
},
{
test: /\.css/,
include: cssModulesRegex,
loaders: [
'simple-universal-style',
cssModulesLoader,
'postcss-loader',
],
},
{
test: /\.json$/,
exclude: [
path.resolve(process.cwd(), './routes.json'),
],
loader: 'json-loader',
},
{
test: /\.json$/,
include: [
path.resolve(process.cwd(), './routes.json'),
],
loaders: [
`babel-loader?${JSON.stringify(babelConfig)}`,
path.resolve(process.cwd(), './utils/routes-loader.js'),
],
},
{
test: /\.md$/,
loader: path.resolve(process.cwd(), './utils/markdown-loader.js'),
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: 'url-loader?limit=10000',
},
{
test: /\.woff(\?.*)?$/,
loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff'
},
{
test: /\.woff2(\?.*)?$/,
loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff2'
},
{test: /\.otf(\?.*)?$/, loader: 'file?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=font/opentype'},
{
test: /\.ttf(\?.*)?$/,
loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/octet-stream'
},
{test: /\.eot(\?.*)?$/, loader: 'file?prefix=fonts/&name=[path][name].[ext]'},
{test: /\.svg(\?.*)?$/, loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=image/svg+xml'},
{
test: /\.(wav|mp3)$/,
loader: 'file-loader',
},
],
},
// The list of plugins for PostCSS
// https://github.com/postcss/postcss
postcss(bundler) {
return [
// Transfer @import rule by inlining content, e.g. @import 'normalize.css'
// https://github.com/postcss/postcss-import
require('postcss-import')({ addDependencyTo: bundler }),
// W3C variables, e.g. :root { --color: red; } div { background: var(--color); }
// https://github.com/postcss/postcss-custom-properties
require('postcss-custom-properties')(),
// W3C CSS Custom Media Queries, e.g. @custom-media --small-viewport (max-width: 30em);
// https://github.com/postcss/postcss-custom-media
require('postcss-custom-media')(),
// CSS4 Media Queries, e.g. @media screen and (width >= 500px) and (width <= 1200px) { }
// https://github.com/postcss/postcss-media-minmax
require('postcss-media-minmax')(),
// W3C CSS Custom Selectors, e.g. @custom-selector :--heading h1, h2, h3, h4, h5, h6;
// https://github.com/postcss/postcss-custom-selectors
require('postcss-custom-selectors')(),
// W3C calc() function, e.g. div { height: calc(100px - 2em); }
// https://github.com/postcss/postcss-calc
require('postcss-calc')(),
// Allows you to nest one style rule inside another
// https://github.com/jonathantneal/postcss-nesting
require('postcss-nesting')(),
// W3C color() function, e.g. div { background: color(red alpha(90%)); }
// https://github.com/postcss/postcss-color-function
require('postcss-color-function')(),
// Convert CSS shorthand filters to SVG equivalent, e.g. .blur { filter: blur(4px); }
// https://github.com/iamvdo/pleeease-filters
require('pleeease-filters')(),
// Generate pixel fallback for "rem" units, e.g. div { margin: 2.5rem 2px 3em 100%; }
// https://github.com/robwierzbowski/node-pixrem
require('pixrem')(),
// W3C CSS Level4 :matches() pseudo class, e.g. p:matches(:first-child, .special) { }
// https://github.com/postcss/postcss-selector-matches
require('postcss-selector-matches')(),
// Transforms :not() W3C CSS Level 4 pseudo class to :not() CSS Level 3 selectors
// https://github.com/postcss/postcss-selector-not
require('postcss-selector-not')(),
// Add vendor prefixes to CSS rules using values from caniuse.com
// https://github.com/postcss/autoprefixer
cssnano({
autoprefixer: {
add: true,
remove: true,
browsers: ['last 2 versions']
},
discardComments: {
removeAll: true
},
discardUnused: false,
mergeIdents: false,
reduceIdents: false,
safe: true,
sourcemap: true
})
];
},
}
const clientConfig = extend(true, {}, webpackConfig, {
// 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(process.cwd(), './public/dist'),
publicPath: '/dist/',
filename: debug ? '[name].js?[hash]' : '[name].[hash].js',
// chunkFilename: debug ? '[id].js?[chunkhash]' : '[id].[chunkhash].js',
sourcePrefix: ' ',
},
// What information should be printed to the console
stats: {
colors: true,
reasons: debug,
hash: verbose,
version: verbose,
timings: true,
chunks: verbose,
chunkModules: verbose,
cached: verbose,
cachedAssets: verbose,
},
// The list of plugins for Webpack compiler
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin(extend(true, {}, configvar, exposevar)),
new CopyWebpackPlugin([{from: 'assets', to: ''}]),
// Emit a JSON file with assets paths
// https://github.com/sporto/assets-webpack-plugin#options
new AssetsPlugin({
path: path.resolve(process.cwd(), './public/dist'),
filename: 'assets.json',
prettyPrint: true,
}),
],
});
// Optimize the bundle in release (production) mode
if (!debug) {
clientConfig.plugins.push(new webpack.optimize.DedupePlugin());
clientConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
screw_ie8: true,
warnings: verbose,
},
mangle: {
screw_ie8: true,
},
output: {
comments: false,
screw_ie8: true,
},
}));
clientConfig.plugins.push(new webpack.optimize.AggressiveMergingPlugin());
}
// Hot Module Replacement (HMR) + React Hot Reload
if (hmr) {
babelConfig.plugins.unshift('react-hot-loader/babel');
clientConfig.entry.unshift('react-hot-loader/patch', 'webpack-hot-middleware/client');
clientConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
clientConfig.plugins.push(new webpack.NoErrorsPlugin());
}
exposevar.process.env.BROWSER = false;
const serverConfig = extend(true, {}, webpackConfig, {
output: {
path: path.resolve(process.cwd(), './public/dist'),
filename: 'server.js',
libraryTarget: 'commonjs2',
//path: path.resolve(__dirname, '../dist/assets')
},
entry: [
/* The main entry point of your JavaScript application */
'./server.js',
],
target: 'node',
externals: [
/^\.\/assets$/,
/ajv/,
function filter(context, request, cb) {
const isExternal =
request.match(/^[@a-z][a-z\/\.\-0-9]*$/i) && !request.match(/^react-routing/) && !context.match(/[\\/]react-routing/);
cb(null, Boolean(isExternal));
},
],
plugins: [
new webpack.ProvidePlugin({
"React": "react"
}),
// new HappyPack({ id: 'js' }),
// new HappyPack({ id: 'css' }),
// Define free variables
// https://webpack.github.io/docs/list-of-plugins.html#defineplugin
new webpack.DefinePlugin(extend(true, {}, configvar, exposevar)),
],
node: {
console: false,
global: false,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
},
});
module.exports = [clientConfig, serverConfig];