Files
lifeforge/client/vite.config.ts

187 lines
4.6 KiB
TypeScript

// import MillionLint from '@million/lint'
import federation from '@originjs/vite-plugin-federation'
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import { globSync } from 'glob'
import path from 'node:path'
import { type Alias, defineConfig } from 'vite'
const ReactCompilerConfig = {
sources: filename => {
return true
}
}
export const alias: Alias[] = [
{
find: '@providers',
replacement: path.resolve(__dirname, './src/providers')
},
{ find: '@utils', replacement: path.resolve(__dirname, './src/utils') },
{ find: '@core', replacement: path.resolve(__dirname, './src/core') },
{
find: '@server',
replacement: '@server',
customResolver: (id, importer) => {
// Check if importing from a module (apps/**/client/)
const moduleMatch = importer?.match(/\/apps\/([^/]+)\/client\//)
if (moduleMatch) {
// Resolve to the module's server directory
const moduleName = moduleMatch[1]
const serverPath = path.resolve(
__dirname,
'../apps',
moduleName,
'server',
id.replace('@server/', '').replace('@server', '')
)
const matched = globSync([
serverPath + '.ts',
serverPath + '/index.ts',
serverPath
])
return matched[0] || serverPath
}
// Default: resolve to core server
const corePath = path.resolve(
__dirname,
'../server/src',
id.replace('@server/', '').replace('@server', '')
)
const matched = globSync([
corePath + '.ts',
corePath + '/index.ts',
corePath
])
return matched[0] || corePath
}
},
{ find: '@modules', replacement: path.resolve(__dirname, '../apps') },
{
find: '@',
replacement: '@',
customResolver: (id, importer, options) => {
let rootDir = ''
const isAppModule =
importer?.includes('/apps/') &&
importer?.includes('/client/') &&
!importer?.includes('/client/src/')
if (importer?.endsWith('manifest.ts')) {
rootDir = importer.replace('manifest.ts', 'src/')
} else if (isAppModule) {
const clientMatch = importer?.match(/(.+\/client)\//)
rootDir = clientMatch?.[1] || ''
} else {
rootDir = importer?.split('/src/')[0] + '/src' || ''
}
const matched = globSync([
path.resolve(rootDir, id.slice(2) + '.{tsx,ts,json}'),
path.resolve(rootDir, id.slice(2), 'index.{tsx,ts}'),
path.resolve(rootDir, id.slice(2))
])
if (!matched[0]) {
console.log([
path.resolve(rootDir, id.slice(2) + '.{tsx,ts,json}'),
path.resolve(rootDir, id.slice(2), 'index.{tsx,ts}'),
path.resolve(rootDir, id.slice(2))
])
console.log(
`[vite] failed to resolve import "${id}" from "${importer}"`
)
return null
}
return matched[0]
}
}
]
export default defineConfig({
envDir: path.resolve(__dirname, '../env'),
plugins: [
// MillionLint.vite({}),
react({
babel: {
plugins: [['babel-plugin-react-compiler', ReactCompilerConfig]]
}
}),
tailwindcss(),
federation({
name: 'host',
remotes: {
None: ''
},
shared: [
'react',
'react-dom',
{
shared: {
packagePath: './node_modules/shared'
},
'lifeforge-ui': {
packagePath: './node_modules/lifeforge-ui'
}
},
'@tanstack/react-query',
'i18next',
'react-i18next'
]
})
],
server: {
fs: {
strict: true
},
watch: {
ignored: ['**/node_modules/**', '**/.git/**']
}
},
resolve: {
alias
},
build: {
commonjsOptions: {
transformMixedEsModules: true
},
target: 'esnext',
sourcemap: false,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
return id
.toString()
.split('node_modules/')
.pop()!
.split('/')[0]
.toString()
}
}
}
}
},
optimizeDeps: {
esbuildOptions: {
sourcemap: false,
target: 'esnext'
}
},
css: {
postcss: {
plugins: [
{
postcssPlugin: 'internal:charset-removal',
AtRule: {
charset: atRule => {
if (atRule.name === 'charset') {
atRule.remove()
}
}
}
}
]
}
}
})