Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mc-scripts): manually group some vendor chunks #3615

Merged
merged 12 commits into from
Oct 14, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/gentle-icons-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-frontend/mc-scripts': patch
---

Manually group some vendor chunks.
4 changes: 3 additions & 1 deletion packages/mc-scripts/src/commands/build-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs-extra';
import { build, type Plugin } from 'vite';
import { packageLocation as applicationStaticAssetsPath } from '@commercetools-frontend/assets';
import { generateTemplate } from '@commercetools-frontend/mc-html-template';
import { manualChunks } from '../config/optimizations';
import paths from '../config/paths';
import pluginDynamicBaseAssetsGlobals from '../vite-plugins/vite-plugin-dynamic-base-assets-globals';
import pluginI18nMessageCompilation from '../vite-plugins/vite-plugin-i18n-message-compilation';
Expand Down Expand Up @@ -41,7 +42,8 @@ async function run() {
// NOTE that after the build, Vite will write the `index.html` (template)
// at the `/public/public/index.html` location. See `fs.renameSync` below.
input: paths.appIndexHtml,
// Reduce the memory footpring when building sourcemaps.
output: { manualChunks },
// Reduce the memory footprint when building sourcemaps.
// https://github.com/vitejs/vite/issues/2433#issuecomment-1361094727
cache: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import createPostcssConfig from './create-postcss-config';
import hasJsxRuntime from './has-jsx-runtime';
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
import momentLocalesToKeep from /* preval */ './moment-locales';
import { webpackCacheGroups } from './optimizations';
import paths from './paths';
import vendorsToTranspile from './vendors-to-transpile';

Expand Down Expand Up @@ -71,17 +72,15 @@ function createWebpackConfigForDevelopment(
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
// https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a
optimization: {
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
splitChunks: {
chunks: 'all',
},
// Keep the runtime chunk separated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
// https://github.com/facebook/create-react-app/issues/5358
runtimeChunk: {
name: 'runtime',
},
splitChunks: {
cacheGroups: webpackCacheGroups,
},
moduleIds: 'named',
chunkIds: 'deterministic',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import createPostcssConfig from './create-postcss-config';
import hasJsxRuntime from './has-jsx-runtime';
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
import momentLocalesToKeep from /* preval */ './moment-locales';
import { webpackCacheGroups } from './optimizations';
import paths from './paths';
import vendorsToTranspile from './vendors-to-transpile';

Expand Down Expand Up @@ -128,6 +129,9 @@ function createWebpackConfigForProduction(
runtimeChunk: {
name: 'runtime',
},
splitChunks: {
cacheGroups: webpackCacheGroups,
},
moduleIds: 'named',
chunkIds: 'deterministic',
},
Expand Down
40 changes: 40 additions & 0 deletions packages/mc-scripts/src/config/optimizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Dependencies to be split/grouped into separate chunks.
// This is useful to reduce the main bundle size and have more
// dedicated caching strategy for specific chunks.
// https://webpack.js.org/plugins/split-chunks-plugin/
// https://rollupjs.org/configuration-options/#output-manualchunks
const manualChunks = {
'apollo-client': ['@apollo/client'],
'core-js-pure': ['core-js-pure'],
'commercetools-uikit-icons': ['@commercetools-uikit/icons'],
moment: ['moment', 'moment-timezone'],
react: [
'react',
'react-dom',
'react-router',
'react-router-dom',
'react-intl',
'@reduxjs/toolkit',
'redux',
'react-redux',
'redux-logger',
'redux-thunk',
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmenko the only change I made is to lump redux and react together; since redux is fairly small this seems fine to do.

'sentry-browser': ['@sentry/browser'],
};

const webpackCacheGroups = Object.entries(manualChunks).reduce(
(previousChunks, [chunkName, vendors]) => {
return {
...previousChunks,
[chunkName]: {
test: new RegExp(`[\\/]node_modules[\\/](${vendors.join('|')})[\\/]`),
name: chunkName,
chunks: 'all',
},
};
},
{}
);

export { manualChunks, webpackCacheGroups };
Loading