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(console, experience): optimize bundling #6326

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/console/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<head>
<meta charset="utf-8" />
<link rel="apple-touch-icon" sizes="180x180" href="./apple-touch-icon.png">
<link rel="icon" href="./favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="./src/apple-touch-icon.png">
<link rel="icon" href="./src/favicon.ico" />
</head>

<body>
Expand Down
1 change: 0 additions & 1 deletion packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@logto/react": "^3.0.12",
"@logto/schemas": "workspace:^1.18.0",
"@logto/shared": "workspace:^3.1.1",
"@mdx-js/mdx": "^3.0.1",
"@mdx-js/react": "^3.0.1",
"@mdx-js/rollup": "^3.0.1",
"@monaco-editor/react": "^4.6.0",
Expand Down
30 changes: 29 additions & 1 deletion packages/console/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { defineConfig, mergeConfig, type UserConfig } from 'vite';
import viteCompression from 'vite-plugin-compression';
import svgr from 'vite-plugin-svgr';

import { defaultConfig } from '../../vite.shared.config';
import { defaultConfig, manualChunks } from '../../vite.shared.config';

// We need to explicitly import the `.env` file and use `define` later because we do not have a
// prefix for our environment variables which it is required in Vite.
Expand Down Expand Up @@ -50,6 +50,34 @@ const buildConfig = (mode: string): UserConfig => ({
// `@withtyped/client` needs this to be defined. We can optimize this later.
'process.env': {},
},
build: {
rollupOptions: {
output: {
// Tip: You can use `pnpx vite-bundle-visualizer` to analyze the bundle size
manualChunks: (id, meta) => {
if (/\/node_modules\/(cose-base|layout-base|cytoscape|cytoscape-[^/]*)\//.test(id)) {
return 'cytoscape';
}

for (const largePackage of [
'libphonenumber-js',
'mermaid',
'elkjs',
'katex',
'refractor',
'lodash',
'lodash-es',
]) {
if (id.includes(`/node_modules/${largePackage}/`)) {
return largePackage;
}
}

return manualChunks(id, meta);
},
},
},
},
});

export default defineConfig(({ mode }) => mergeConfig(defaultConfig, buildConfig(mode)));
4 changes: 2 additions & 2 deletions packages/demo-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<head>
<meta charset="utf-8" />
<link rel="apple-touch-icon" sizes="180x180" href="./apple-touch-icon.png">
<link rel="icon" href="./favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="./src/apple-touch-icon.png">
<link rel="icon" href="./src/ffavicon.ico" />
<title>Logto Live Preview</title>
</head>

Expand Down
7 changes: 1 addition & 6 deletions packages/experience/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ const buildConfig = (mode: string): UserConfig => ({
target: browserslistToEsbuild('> 0.5%, last 2 versions, Firefox ESR, not dead'),
rollupOptions: {
output: {
// Tip: You can use `pnpx vite-bundle-visualizer` to analyze the bundle size
manualChunks: (id, meta) => {
// Caution: React-related packages should be bundled together otherwise it will cause runtime errors
// Update this list if necessary when adding new React-related packages
if (/\/node_modules\/(react|react-[^/]*|@react-spring)\//.test(id)) {
return 'react';
}

if (/\/node_modules\/i18next[^/]*\//.test(id)) {
return 'i18next';
}
Expand Down
44 changes: 31 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions vite.shared.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

import { Rollup, UserConfig } from 'vite';

export const manualChunks: Rollup.GetManualChunk = (id) => {
export const manualChunks: Rollup.GetManualChunk = (id, { getModuleInfo }) => {
const hasReactDependency = (id: string): boolean => {
return getModuleInfo(id)
?.importedIds
.some((importedId) =>
importedId.includes('react') ||
importedId.includes('react-dom')
) ?? false;
}

// Caution: React-related packages should be bundled together otherwise it will cause runtime errors
if (id.includes('/node_modules/') && hasReactDependency(id)) {
return 'react';
}

if (id.includes('/@logto/')) {
return 'logto';
return '@logto';
}

if (id.includes('/node_modules/')) {
Expand Down
Loading