Files
twenty/packages/twenty-ui/vite.config.ts
T
Raphaël Bosi 8553c574db Improve twenty-ui packaging for standalone publishing (#21946)
Quick packaging wins to move twenty-ui closer to a standalone
publishable library.

- Move `react`/`react-dom` to `peerDependencies` (`^19.0.0`) so
consumers provide a single React and we avoid duplicate-React bugs. They
stay in `devDependencies` for the in-repo build, and `vite.config.ts`
now derives the Rollup `external` list from peer deps too so React stays
externalized instead of bundled.
- Declare `type-fest` in `dependencies`. It was a phantom dep (resolved
only via root hoisting) and its types are referenced by the emitted
json-visualizer `.d.ts`, so standalone consumers need it.
- Move build-only `glob` to `devDependencies` and add `typescript` (both
used only by `generateBarrels.ts`).
- Make `tsconfig.json` self-contained by inlining the base compiler
options, and point the Vite `cacheDir`/`optimizeDeps.exclude` at
package-local paths.

Verified: typecheck, build (React confirmed externalized in `dist`, not
inlined), dts emission, and unit tests all pass.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/21946?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-06-22 15:09:25 +00:00

181 lines
5.3 KiB
TypeScript

import react from '@vitejs/plugin-react-swc';
import * as fs from 'fs';
import * as path from 'path';
import { defineConfig } from 'vite';
import checker from 'vite-plugin-checker';
import dts, { type PluginOptions } from 'vite-plugin-dts';
import sassDts from 'vite-plugin-sass-dts';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
type Checkers = Parameters<typeof checker>[0];
import packageJson from './package.json';
const entries = Object.keys(packageJson.exports)
.filter((el) => !el.endsWith('.css'))
.map((module) => `src/${module}/index.ts`);
const entryFileNames = (chunk: any, extension: 'cjs' | 'mjs') => {
if (!chunk.isEntry) {
throw new Error(
`Should never occurs, encountered a non entry chunk ${chunk.facadeModuleId}`,
);
}
const splitFaceModuleId = chunk.facadeModuleId?.split('/');
if (splitFaceModuleId === undefined) {
throw new Error(
`Should never occurs splitFaceModuleId is undefined ${chunk.facadeModuleId}`,
);
}
const moduleDirectory = splitFaceModuleId[splitFaceModuleId?.length - 2];
if (moduleDirectory === 'src') {
return `${chunk.name}.${extension}`;
}
return `${moduleDirectory}.${extension}`;
};
export default defineConfig(({ command }) => {
const isBuildCommand = command === 'build';
const tsConfigPath = isBuildCommand
? path.resolve(__dirname, './tsconfig.lib.json')
: path.resolve(__dirname, './tsconfig.json');
const checkersConfig: Checkers = {
typescript: {
tsconfigPath: tsConfigPath,
},
};
const dtsConfig: PluginOptions = {
entryRoot: 'src',
tsconfigPath: tsConfigPath,
};
const BUNDLED_DEPS = ['@tabler/icons-react'];
const externalDeps = Object.keys({
...(packageJson.dependencies || {}),
...(packageJson.peerDependencies || {}),
}).filter((dep) => !BUNDLED_DEPS.includes(dep));
return {
resolve: {
alias: {
'@ui/': path.resolve(__dirname, 'src') + '/',
'@assets/': path.resolve(__dirname, 'src/assets') + '/',
'@styles/': path.resolve(__dirname, 'src/styles') + '/',
'@tabler/icons-react': '@tabler/icons-react/dist/esm/icons/index.mjs',
},
},
css: {
modules: {
localsConvention: 'camelCaseOnly',
},
preprocessorOptions: {
scss: {
api: 'modern-compiler',
loadPaths: [path.resolve(__dirname, 'src/styles')],
additionalData: [
`@use 'abstracts/functions' as *;`,
`@use 'abstracts/mixins' as *;`,
`@use 'abstracts/breakpoints' as *;`,
'',
].join('\n'),
},
},
},
optimizeDeps: {
// Pre-bundle React up front so Vite's dep optimizer doesn't re-bundle it
// mid-run during browser-mode Storybook tests — re-bundling rotates the
// optimized chunk hash and 404s in-flight dynamic imports (vite 8 / rolldown).
include: [
'react',
'react-dom',
'react-dom/client',
'react/jsx-runtime',
'react/jsx-dev-runtime',
],
},
root: __dirname,
cacheDir: 'node_modules/.vite',
assetsInclude: ['src/**/*.svg'],
plugins: [
react(),
tsconfigPaths({
root: __dirname,
projects: ['tsconfig.json'],
}),
svgr(),
// Generates typed *.module.scss.d.ts siblings (dev mode only — backed by
// sass-embedded). CI/build relies on the ambient src/scss-modules.d.ts.
sassDts({ esmExport: true, legacyFileFormat: true }),
dts(dtsConfig),
checker(checkersConfig),
{
name: 'copy-theme-css',
closeBundle() {
const distDir = path.resolve(__dirname, 'dist');
fs.mkdirSync(distDir, { recursive: true });
const themeCssFiles = ['theme-light.css', 'theme-dark.css'];
for (const file of themeCssFiles) {
fs.copyFileSync(
path.resolve(__dirname, `src/theme-constants/${file}`),
path.resolve(distDir, file),
);
}
},
},
],
build: {
cssCodeSplit: false,
minify: 'esbuild',
sourcemap: false,
emptyOutDir: false,
outDir: './dist',
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
interopDefault: true,
defaultIsModuleExports: true,
requireReturnsDefault: 'auto',
},
lib: {
entry: ['src/index.ts', ...entries],
name: 'twenty-ui',
},
rollupOptions: {
external: (id: string) =>
externalDeps.some((dep) => id === dep || id.startsWith(dep + '/')),
output: [
{
assetFileNames: 'style.css',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
format: 'es',
entryFileNames: (chunk) => entryFileNames(chunk, 'mjs'),
},
{
assetFileNames: 'style.css',
format: 'cjs',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
interop: 'auto',
esModule: true,
exports: 'named',
entryFileNames: (chunk) => entryFileNames(chunk, 'cjs'),
},
],
},
},
logLevel: 'error',
};
});