Skip to content

Commit 2e03e0c

Browse files
author
chenghao
committed
自定义打包分离第三方库
1 parent 085c20f commit 2e03e0c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

config/paths.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ module.exports = {
5252
appNodeModules: resolveApp('node_modules'),
5353
publicUrl: getPublicUrl(resolveApp('package.json')),
5454
servedPath: getServedPath(resolveApp('package.json')),
55+
vendorConfig: resolveApp('config/vendor.config.js')
5556
};

config/vendor.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Created by 叶子 on 2017/9/24.
3+
*/
4+
module.exports = {
5+
entry: {
6+
vendor: ['antd'], // 需要分离的库
7+
charts: ['echarts-for-react']
8+
}
9+
};

config/webpack.config.prod.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const extractTextPluginOptions = shouldUseRelativeAssetPaths
4444
{ publicPath: Array(cssFilename.split('/').length).join('../') }
4545
: {};
4646

47+
// add vendor pack
48+
const vendorEntry = require(paths.vendorConfig).entry;
49+
4750
// This is the production configuration.
4851
// It compiles slowly and is focused on producing a fast and minimal bundle.
4952
// The development configuration is different and lives in a separate file.
@@ -54,7 +57,12 @@ module.exports = {
5457
// You can exclude the *.map files from the build during deployment.
5558
devtool: 'source-map',
5659
// In production, we only want to load the polyfills and the app code.
57-
entry: [require.resolve('./polyfills'), paths.appIndexJs],
60+
entry: Object.assign( // 合并分离打包入口文件
61+
{
62+
main: [require.resolve('./polyfills'), paths.appIndexJs]
63+
},
64+
vendorEntry
65+
),
5866
output: {
5967
// The build folder.
6068
path: paths.appBuild,
@@ -264,6 +272,9 @@ module.exports = {
264272
],
265273
},
266274
plugins: [
275+
new webpack.optimize.CommonsChunkPlugin({
276+
names: ['main', 'manifest'].concat(Object.keys(vendorEntry))
277+
}),
267278
// Makes some environment variables available in index.html.
268279
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
269280
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

0 commit comments

Comments
 (0)