File tree 5 files changed +58
-6
lines changed
5 files changed +58
-6
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @commercetools-frontend/mc-scripts ' : patch
3
+ ---
4
+
5
+ Manually group some vendor chunks
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import fs from 'fs-extra';
5
5
import { build , type Plugin } from 'vite' ;
6
6
import { packageLocation as applicationStaticAssetsPath } from '@commercetools-frontend/assets' ;
7
7
import { generateTemplate } from '@commercetools-frontend/mc-html-template' ;
8
+ import { manualChunks } from '../config/optimizations' ;
8
9
import paths from '../config/paths' ;
9
10
import pluginDynamicBaseAssetsGlobals from '../vite-plugins/vite-plugin-dynamic-base-assets-globals' ;
10
11
import pluginSvgr from '../vite-plugins/vite-plugin-svgr' ;
@@ -40,6 +41,7 @@ async function run() {
40
41
// NOTE that after the build, Vite will write the `index.html` (template)
41
42
// at the `/public/public/index.html` location. See `fs.renameSync` below.
42
43
input : paths . appIndexHtml ,
44
+ output : { manualChunks } ,
43
45
} ,
44
46
} ,
45
47
server : {
Original file line number Diff line number Diff line change @@ -11,10 +11,11 @@ import type {
11
11
import LocalHtmlWebpackPlugin from '../webpack-plugins/local-html-webpack-plugin' ;
12
12
import createPostcssConfig from './create-postcss-config' ;
13
13
import hasJsxRuntime from './has-jsx-runtime' ;
14
+ // https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
14
15
import momentLocalesToKeep from /* preval */ './moment-locales' ;
16
+ import { webpackCacheGroups } from './optimizations' ;
15
17
import paths from './paths' ;
16
18
import vendorsToTranspile from './vendors-to-transpile' ;
17
- // https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
18
19
19
20
const defaultToggleFlags : TWebpackConfigToggleFlagsForDevelopment = {
20
21
generateIndexHtml : true ,
@@ -69,17 +70,15 @@ function createWebpackConfigForDevelopment(
69
70
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
70
71
// https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a
71
72
optimization : {
72
- // Automatically split vendor and commons
73
- // https://twitter.com/wSokra/status/969633336732905474
74
- splitChunks : {
75
- chunks : 'all' ,
76
- } ,
77
73
// Keep the runtime chunk separated to enable long term caching
78
74
// https://twitter.com/wSokra/status/969679223278505985
79
75
// https://github.com/facebook/create-react-app/issues/5358
80
76
runtimeChunk : {
81
77
name : 'runtime' ,
82
78
} ,
79
+ splitChunks : {
80
+ cacheGroups : webpackCacheGroups ,
81
+ } ,
83
82
moduleIds : 'named' ,
84
83
chunkIds : 'deterministic' ,
85
84
} ,
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import createPostcssConfig from './create-postcss-config';
18
18
import hasJsxRuntime from './has-jsx-runtime' ;
19
19
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
20
20
import momentLocalesToKeep from /* preval */ './moment-locales' ;
21
+ import { webpackCacheGroups } from './optimizations' ;
21
22
import paths from './paths' ;
22
23
import vendorsToTranspile from './vendors-to-transpile' ;
23
24
@@ -126,6 +127,9 @@ function createWebpackConfigForProduction(
126
127
runtimeChunk : {
127
128
name : 'runtime' ,
128
129
} ,
130
+ splitChunks : {
131
+ cacheGroups : webpackCacheGroups ,
132
+ } ,
129
133
moduleIds : 'named' ,
130
134
chunkIds : 'deterministic' ,
131
135
} ,
Original file line number Diff line number Diff line change
1
+ // Dependencies to be split/grouped into separate chunks.
2
+ // This is useful to reduce the main bundle size and have more
3
+ // dedicated caching strategy for specific chunks.
4
+ // https://webpack.js.org/plugins/split-chunks-plugin/
5
+ // https://rollupjs.org/configuration-options/#output-manualchunks
6
+ const manualChunks = {
7
+ 'apollo-client' : [ '@apollo/client' ] ,
8
+ 'core-js-pure' : [ 'core-js-pure' ] ,
9
+ 'commercetools-uikit-icons' : [ '@commercetools-uikit/icons' ] ,
10
+ moment : [ 'moment' , 'moment-timezone' ] ,
11
+ react : [
12
+ 'react' ,
13
+ 'react-dom' ,
14
+ 'react-router' ,
15
+ 'react-router-dom' ,
16
+ 'react-intl' ,
17
+ ] ,
18
+ redux : [
19
+ '@reduxjs/toolkit' ,
20
+ 'redux' ,
21
+ 'react-redux' ,
22
+ 'redux-logger' ,
23
+ 'redux-thunk' ,
24
+ ] ,
25
+ 'sentry-browser' : [ '@sentry/browser' ] ,
26
+ } ;
27
+
28
+ const webpackCacheGroups = Object . entries ( manualChunks ) . reduce (
29
+ ( previousChunks , [ chunkName , vendors ] ) => {
30
+ return {
31
+ ...previousChunks ,
32
+ [ chunkName ] : {
33
+ test : new RegExp ( `[\\/]node_modules[\\/](${ vendors . join ( '|' ) } )[\\/]` ) ,
34
+ name : chunkName ,
35
+ chunks : 'all' ,
36
+ } ,
37
+ } ;
38
+ } ,
39
+ { }
40
+ ) ;
41
+
42
+ export { manualChunks , webpackCacheGroups } ;
You can’t perform that action at this time.
0 commit comments