Skip to content

Commit 59522c8

Browse files
committed
refactor(mc-scripts): manually group some vendor chunks
1 parent 976a59f commit 59522c8

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

.changeset/lazy-donuts-share.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-frontend/mc-scripts': patch
3+
---
4+
5+
Manually group some vendor chunks

packages/mc-scripts/src/commands/build-vite.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs-extra';
55
import { build, type Plugin } from 'vite';
66
import { packageLocation as applicationStaticAssetsPath } from '@commercetools-frontend/assets';
77
import { generateTemplate } from '@commercetools-frontend/mc-html-template';
8+
import { manualChunks } from '../config/optimizations';
89
import paths from '../config/paths';
910
import pluginDynamicBaseAssetsGlobals from '../vite-plugins/vite-plugin-dynamic-base-assets-globals';
1011
import pluginSvgr from '../vite-plugins/vite-plugin-svgr';
@@ -40,6 +41,7 @@ async function run() {
4041
// NOTE that after the build, Vite will write the `index.html` (template)
4142
// at the `/public/public/index.html` location. See `fs.renameSync` below.
4243
input: paths.appIndexHtml,
44+
output: { manualChunks },
4345
},
4446
},
4547
server: {

packages/mc-scripts/src/config/create-webpack-config-for-development.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import type {
1111
import LocalHtmlWebpackPlugin from '../webpack-plugins/local-html-webpack-plugin';
1212
import createPostcssConfig from './create-postcss-config';
1313
import hasJsxRuntime from './has-jsx-runtime';
14+
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
1415
import momentLocalesToKeep from /* preval */ './moment-locales';
16+
import { webpackCacheGroups } from './optimizations';
1517
import paths from './paths';
1618
import vendorsToTranspile from './vendors-to-transpile';
17-
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
1819

1920
const defaultToggleFlags: TWebpackConfigToggleFlagsForDevelopment = {
2021
generateIndexHtml: true,
@@ -69,17 +70,15 @@ function createWebpackConfigForDevelopment(
6970
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
7071
// https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a
7172
optimization: {
72-
// Automatically split vendor and commons
73-
// https://twitter.com/wSokra/status/969633336732905474
74-
splitChunks: {
75-
chunks: 'all',
76-
},
7773
// Keep the runtime chunk separated to enable long term caching
7874
// https://twitter.com/wSokra/status/969679223278505985
7975
// https://github.com/facebook/create-react-app/issues/5358
8076
runtimeChunk: {
8177
name: 'runtime',
8278
},
79+
splitChunks: {
80+
cacheGroups: webpackCacheGroups,
81+
},
8382
moduleIds: 'named',
8483
chunkIds: 'deterministic',
8584
},

packages/mc-scripts/src/config/create-webpack-config-for-production.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import createPostcssConfig from './create-postcss-config';
1818
import hasJsxRuntime from './has-jsx-runtime';
1919
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
2020
import momentLocalesToKeep from /* preval */ './moment-locales';
21+
import { webpackCacheGroups } from './optimizations';
2122
import paths from './paths';
2223
import vendorsToTranspile from './vendors-to-transpile';
2324

@@ -126,6 +127,9 @@ function createWebpackConfigForProduction(
126127
runtimeChunk: {
127128
name: 'runtime',
128129
},
130+
splitChunks: {
131+
cacheGroups: webpackCacheGroups,
132+
},
129133
moduleIds: 'named',
130134
chunkIds: 'deterministic',
131135
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 };

0 commit comments

Comments
 (0)