-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Expand file tree
/
Copy pathvite.config.ts
More file actions
174 lines (161 loc) · 5.02 KB
/
Copy pathvite.config.ts
File metadata and controls
174 lines (161 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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';
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: string[] = [];
const externalDeps = Object.keys({
...(packageJson.dependencies || {}),
...(packageJson.peerDependencies || {}),
}).filter((dep) => !BUNDLED_DEPS.includes(dep));
return {
resolve: {
tsconfigPaths: true,
alias: {
'@ui/': path.resolve(__dirname, 'src') + '/',
'@assets/': path.resolve(__dirname, 'src/assets') + '/',
'@styles/': path.resolve(__dirname, 'src/styles') + '/',
},
},
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(),
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',
},
esModule: true,
exports: 'named',
entryFileNames: (chunk) => entryFileNames(chunk, 'cjs'),
},
],
},
},
logLevel: 'error',
};
});