Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.

Commit a40d1ef

Browse files
committed
refactor(webpack): removed some boilerplate code
1 parent aa5cbe3 commit a40d1ef

File tree

3 files changed

+61
-79
lines changed

3 files changed

+61
-79
lines changed

webpack/webpack.client.config.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/* eslint-disable no-undefined, object-shorthand */
22

3-
const path = require('path')
43
const config = require('../config/config')
54
const merge = require('lodash.merge')
65
const paths = require('../config/paths')
76
const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin
7+
const ExtractTextPlugin = require('extract-text-webpack-plugin')
88
const webpack = require('webpack')
9-
const webpackConfig = require('./webpack.config.js')
10-
const DEBUG = config.DEBUG
9+
const { webpackConfig, styleLoader, cssLoader, postcssLoader } = require('./webpack.config.js')
10+
const { DEBUG } = config
1111
const filename = DEBUG ? '[name].js' : '[name].[chunkhash].js'
1212

13+
const extractCSS = new ExtractTextPlugin({ filename: paths.styleSheet, allChunks: true })
14+
1315
//
1416
// Client Config
1517
// -----------------------------------------------------------------------------
@@ -25,47 +27,50 @@ const webpackClientConfig = merge({}, webpackConfig, {
2527
'react-router',
2628
'redux',
2729
'redux-actions',
28-
'redux-promise-middleware'
29-
]
30+
'redux-promise-middleware',
31+
'redux-thunk',
32+
],
3033
},
3134
output: {
3235
chunkFilename: filename,
3336
filename: filename,
34-
path: paths.publicDir
37+
path: paths.publicDir,
3538
},
3639
module: {
3740
loaders: webpackConfig.module.loaders.concat([
3841
{
3942
test: /\.css$/,
40-
loader: `style!css?modules${DEBUG ? '&localIdentName=[name]_[local]_[hash:base64:3]' : ''}!postcss`,
43+
loaders: DEBUG
44+
? [styleLoader, cssLoader, postcssLoader]
45+
: extractCSS.extract([cssLoader, postcssLoader]),
4146
exclude: /node_modules/,
42-
include: path.resolve('.')
43-
}
44-
])
47+
},
48+
]),
4549
},
4650
plugins: webpackConfig.plugins.concat(
4751
new webpack.DefinePlugin({ __BROWSER__: true }),
4852
new webpack.optimize.CommonsChunkPlugin({
4953
name: config.vendorName,
50-
filename: filename
54+
filename: filename,
5155
})
5256
).concat(DEBUG ? [] : [
57+
extractCSS,
5358
new webpack.optimize.CommonsChunkPlugin({
5459
name: config.inlineName,
55-
filename: `${config.inlineName}.js`
60+
filename: `${config.inlineName}.js`,
5661
}),
62+
new StatsWriterPlugin({ filename: `${config.statsName}.json` }),
5763
new webpack.optimize.UglifyJsPlugin({
5864
sourceMap: false,
5965
output: {
60-
comments: false
66+
comments: false,
6167
},
6268
compress: {
6369
screw_ie8: true, // eslint-disable-line camelcase
64-
warnings: false
65-
}
70+
warnings: false,
71+
},
6672
}),
67-
new StatsWriterPlugin({ filename: `${config.statsName}.json` })
68-
])
73+
]),
6974
})
7075

7176
module.exports = webpackClientConfig

webpack/webpack.config.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* eslint-disable no-undefined, object-shorthand */
22

3-
const path = require('path')
43
const webpack = require('webpack')
54
const config = require('../config/config')
65
const paths = require('../config/paths')
7-
const DEBUG = config.DEBUG
8-
const VERBOSE = false
6+
const { DEBUG } = config
97

108
const GLOBALS = {
119
'__DEV__': DEBUG,
@@ -21,61 +19,57 @@ const plugins = [
2119
options: {
2220
context: paths.contextDir,
2321
postcss: [
24-
require('autoprefixer')({ browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'IE 10'] })
25-
]
26-
}
27-
})
22+
require('autoprefixer')({ browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'IE 10'] }),
23+
],
24+
},
25+
}),
2826
]
2927

28+
exports.styleLoader = 'style-loader'
29+
exports.postcssLoader = 'postcss-loader'
30+
exports.cssLoader = {
31+
loader: 'css-loader',
32+
query: {
33+
localIdentName: DEBUG ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:5]',
34+
minimize: !DEBUG,
35+
modules: true,
36+
sourceMap: DEBUG,
37+
},
38+
}
39+
3040
const webpackConfig = {
3141
cache: DEBUG,
3242
context: paths.contextDir,
3343
bail: !DEBUG,
34-
devtool: DEBUG ? 'eval' : undefined,
44+
devtool: DEBUG ? 'cheap-eval-source-map' : undefined,
3545
output: {
3646
publicPath: '/',
37-
path: paths.publicDir
47+
path: paths.publicDir,
3848
},
3949
module: {
4050
loaders: [
4151
{
4252
test: /\.(js|jsx)$/,
43-
loader: 'babel',
53+
loader: 'babel-loader',
4454
query: {
4555
cacheDirectory: true,
46-
plugins: DEBUG ? ['react-hot-loader/babel'] : []
56+
plugins: DEBUG ? ['react-hot-loader/babel'] : [],
4757
},
4858
exclude: /node_modules/,
49-
include: path.resolve('.')
50-
}
51-
]
59+
},
60+
],
5261
},
5362
resolve: {
5463
modules: ['shared', 'node_modules'],
5564
extensions: [
5665
'.web.js',
5766
'.js',
5867
'.json',
59-
'.jsx'
60-
]
61-
},
62-
stats: {
63-
assets: false,
64-
cached: VERBOSE,
65-
cachedAssets: VERBOSE,
66-
children: VERBOSE,
67-
chunkModules: VERBOSE,
68-
chunks: VERBOSE,
69-
colors: true,
70-
context: false,
71-
hash: VERBOSE,
72-
modules: VERBOSE,
73-
reasons: true,
74-
source: VERBOSE,
75-
timings: true,
76-
version: VERBOSE,
68+
'.jsx',
69+
],
7770
},
78-
plugins: plugins
71+
stats: 'errors-only',
72+
plugins: plugins,
7973
}
8074

81-
module.exports = webpackConfig
75+
exports.webpackConfig = webpackConfig

webpack/webpack.server.config.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/* eslint-disable no-undefined, object-shorthand */
22

3-
const path = require('path')
43
const webpack = require('webpack')
5-
const config = require('../config/config')
64
const merge = require('lodash.merge')
75
const paths = require('../config/paths')
86
const ExtractTextPlugin = require('extract-text-webpack-plugin')
9-
const webpackConfig = require('./webpack.config.js')
10-
const DEBUG = config.DEBUG
7+
const { webpackConfig, styleLoader, cssLoader, postcssLoader } = require('./webpack.config.js')
118

12-
const extractCSS = new ExtractTextPlugin({ filename: paths.styleSheet, disable: false, allChunks: true })
9+
const extractCSS = new ExtractTextPlugin({ filename: paths.styleSheet, allChunks: true })
1310

1411
//
1512
// Server Config
@@ -21,42 +18,28 @@ const webpackServerConfig = merge({}, webpackConfig, {
2118
output: {
2219
filename: 'server.js',
2320
libraryTarget: 'commonjs2',
24-
path: paths.buildDir
21+
path: paths.buildDir,
2522
},
2623
module: {
27-
loaders: webpackConfig.module.loaders.concat([
28-
{
29-
test: /\.css$/,
30-
loader: extractCSS.extract({ fallbackLoader: 'style', loader: `css?modules${DEBUG ? '&localIdentName=[name]_[local]_[hash:base64:3]' : '&minimize'}!postcss` }),
31-
exclude: /node_modules/,
32-
include: path.resolve('.')
33-
}
34-
])
24+
loaders: webpackConfig.module.loaders.concat([{
25+
test: /\.css$/,
26+
loader: extractCSS.extract({ fallbackLoader: styleLoader, loader: [cssLoader, postcssLoader] }),
27+
exclude: /node_modules/,
28+
}]),
3529
},
3630
plugins: webpackConfig.plugins.concat(
3731
new webpack.DefinePlugin({ __BROWSER__: false }),
3832
extractCSS
39-
).concat(DEBUG ? [] : [
40-
new webpack.optimize.UglifyJsPlugin({
41-
sourceMap: false,
42-
output: {
43-
comments: false
44-
},
45-
compress: {
46-
screw_ie8: true, // eslint-disable-line camelcase
47-
warnings: false
48-
}
49-
}),
50-
]),
33+
),
5134
node: {
5235
__dirname: false,
5336
__filename: false,
5437
Buffer: false,
5538
console: false,
5639
global: false,
57-
process: false
40+
process: false,
5841
},
59-
externals: /^[a-z][a-z\.\-0-9]*$/
42+
externals: require('webpack-node-externals')(),
6043
})
6144

6245
module.exports = webpackServerConfig

0 commit comments

Comments
 (0)