0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00
logto/vite.shared.config.ts
Gao Sun d7b6987b48
refactor(console, experience): optimize bundling (#6326)
* refactor(console, experience): optimize bundling

* fix: use correct favicon paths

* chore: use dynamic react dependency checking in bundling
2024-07-25 02:07:28 +00:00

52 lines
1.2 KiB
TypeScript

/** @fileoverview The common config for frontend projects. */
import { Rollup, UserConfig } from 'vite';
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';
}
if (id.includes('/node_modules/')) {
return 'vendors';
}
const match = /\/lib\/locales\/([^/]+)/.exec(id);
if (match?.[1]) {
return `phrases-${match[1]}`;
}
};
export const defaultConfig: UserConfig = {
resolve: {
alias: [
{
find: /^@\//,
replacement: '/src/',
},
],
},
optimizeDeps: {
include: ['@logto/phrases', '@logto/phrases-experience', '@logto/schemas'],
},
build: {
sourcemap: true,
rollupOptions: {
output: { manualChunks },
},
},
};