forked from cncjs/cncjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.webconfig.production.js
135 lines (130 loc) · 5.15 KB
/
webpack.webconfig.production.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
process.env.NODE_ENV = 'production';
/* eslint prefer-arrow-callback: 0 */
const without = require('lodash/without');
const crypto = require('crypto');
const path = require('path');
const findImports = require('find-imports');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CSSSplitWebpackPlugin = require('css-split-webpack-plugin').default;
const ManifestPlugin = require('webpack-manifest-plugin');
const InlineChunkWebpackPlugin = require('html-webpack-inline-chunk-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginAddons = require('html-webpack-plugin-addons');
const nib = require('nib');
const stylusLoader = require('stylus-loader');
const baseConfig = require('./webpack.webconfig.base');
const buildConfig = require('./build.config');
const pkg = require('./package.json');
// Use publicPath for production
const payload = pkg.version;
const publicPath = (function(payload) {
const algorithm = 'sha1';
const buf = String(payload);
const hash = crypto.createHash(algorithm).update(buf).digest('hex');
return '/' + hash.substr(0, 8) + '/'; // 8 digits
}(payload));
const timestamp = new Date().getTime();
const webpackConfig = Object.assign({}, baseConfig, {
devtool: 'cheap-module-source-map',
entry: {
polyfill: [
path.resolve(__dirname, 'src/web/polyfill/index.js')
],
vendor: findImports([
'src/web/**/*.{js,jsx}',
'!src/web/polyfill/**/*.js',
'!src/web/containers/DevTools.js', // redux-devtools
'!src/web/**/*.development.js'
], { flatten: true }),
app: [
path.resolve(__dirname, 'src/web/index.jsx')
]
},
output: {
path: path.resolve(__dirname, 'dist/cnc/web'),
chunkFilename: `[name].[chunkhash].bundle.js?_=${timestamp}`,
filename: `[name].[chunkhash].bundle.js?_=${timestamp}`,
publicPath: publicPath
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: JSON.stringify('production'),
LANGUAGES: JSON.stringify(buildConfig.languages),
TRACKING_ID: JSON.stringify(buildConfig.analytics.trackingId)
}
}),
new webpack.NoEmitOnErrorsPlugin(),
new stylusLoader.OptionsPlugin({
default: {
// nib - CSS3 extensions for Stylus
use: [nib()],
// no need to have a '@import "nib"' in the stylesheet
import: ['~nib/lib/nib/index.styl']
}
}),
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale$/,
new RegExp('^\./(' + without(buildConfig.languages, 'en').join('|') + ')$')
),
new webpack.optimize.CommonsChunkPlugin({
// The order matters, the order should be reversed just like loader chain.
// https://github.com/webpack/webpack/issues/1016
names: ['vendor', 'polyfill', 'manifest'],
filename: `[name].[chunkhash].js?_=${timestamp}`,
minChunks: Infinity
}),
// Generates a manifest.json file in your root output directory with a mapping of all source file names to their corresponding output file.
new ManifestPlugin({
fileName: 'manifest.json'
}),
new ExtractTextPlugin({
filename: '[name].css',
allChunks: false
}),
new CSSSplitWebpackPlugin({
size: 4000,
imports: '[name].[ext]?[hash]',
filename: '[name]-[part].[ext]?[hash]',
preserve: false
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
screw_ie8: true, // React doesn't support IE8
warnings: false
},
mangle: {
screw_ie8: true
},
output: {
comments: false,
screw_ie8: true
}
}),
new HtmlWebpackPlugin({
filename: 'index.hbs',
template: path.resolve(__dirname, 'src/web/assets/index.hbs'),
chunksSortMode: 'dependency' // Sort chunks by dependency
}),
new HtmlWebpackPluginAddons({
/**
* Do not insert "[name]-[part].css" to the html. For example:
* <link href="/9b80ca13/[name]-1.css?0584938f631ef1dd3e93d8d8169648a0" rel="stylesheet">
* <link href="/9b80ca13/[name]-2.css?0584938f631ef1dd3e93d8d8169648a0" rel="stylesheet">
* <link href="/9b80ca13/[name].css?ff4bb41b7b5e61a63da54dff2e59581d" rel="stylesheet">
*/
afterHTMLProcessing: function(htmlPluginData, next) {
const re = new RegExp(/<link.* href="[^"]+\w+\-\d+\.css[^>]+>/);
htmlPluginData.html = htmlPluginData.html.replace(re, '');
next(null, htmlPluginData);
}
}),
new InlineChunkWebpackPlugin({
inlineChunks: ['manifest']
})
]
});
module.exports = webpackConfig;