refactor: improve folder structure for @lifeforge/configs

This commit is contained in:
melvinchia3636
2026-06-08 20:12:46 +08:00
parent 57a24a52a7
commit e1494d9559
5 changed files with 71 additions and 63 deletions

View File

@@ -0,0 +1,14 @@
export const SHARED_PACKAGES = {
'@lifeforge/shared': {
aliasRegex: /^@lifeforge\/shared$/,
entryPoint: '../shared/src/index.ts'
},
'@lifeforge/federation': {
aliasRegex: /^@lifeforge\/federation$/,
entryPoint: '../packages/federation/src/index.ts'
},
'@lifeforge/ui': {
aliasRegex: /^@lifeforge\/ui$/,
entryPoint: '../packages/ui/src/index.ts'
}
}

View File

@@ -1,3 +1,7 @@
export { defineModuleConfig } from './module-vite.config'
export { defineClientConfig, SHARED_PACKAGES } from './client-vite.config'
export { customResolver } from './resolver'
export { defineModuleConfig } from './vite/module-vite.config'
export { defineClientConfig } from './vite/client-vite.config'
export { SHARED_PACKAGES } from './constants/shared-packages'
export { aliasResolver as customResolver } from './resolvers/alias-resolver'

View File

@@ -6,21 +6,28 @@ import path from 'node:path'
* Supports resolving from the client src directory, client root for manifest files,
* and falls back to package src directories.
*/
export function customResolver(id: string, importer: string | undefined): string | null {
export function aliasResolver(
id: string,
importer: string | undefined
): string | null {
if (!importer) {
return null
}
let rootDir: string
const clientMatch = importer.match(/(.+\/client)/)
if (clientMatch) {
const clientDir = clientMatch[1]
rootDir =
id === '@/manifest' || id === '@/manifest.ts'
? clientDir
: `${clientDir}/src`
} else {
const srcMatch = importer.match(/(.+\/src)/)
if (!srcMatch) {
return null
}
@@ -28,6 +35,7 @@ export function customResolver(id: string, importer: string | undefined): string
}
const basePath = path.resolve(rootDir, id.slice(2))
const candidates = [
`${basePath}.tsx`,
`${basePath}.ts`,
@@ -43,8 +51,7 @@ export function customResolver(id: string, importer: string | undefined): string
}
}
console.log(
`[vite] failed to resolve import "${id}" from "${importer}"`
)
console.log(`[vite] failed to resolve import "${id}" from "${importer}"`)
return null
}

View File

@@ -3,44 +3,27 @@ import federation from '@originjs/vite-plugin-federation'
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin'
import react from '@vitejs/plugin-react'
import path from 'node:path'
import { type UserConfig, defineConfig } from 'vite'
import { customResolver } from './resolver'
import { defineConfig } from 'vite'
const ReactCompilerConfig = {
sources: () => {
return true
}
}
export const SHARED_PACKAGES = {
'@lifeforge/shared': {
aliasRegex: /^@lifeforge\/shared$/,
entryPoint: '../shared/src/index.ts'
},
'@lifeforge/federation': {
aliasRegex: /^@lifeforge\/federation$/,
entryPoint: '../packages/federation/src/index.ts'
},
'@lifeforge/ui': {
aliasRegex: /^@lifeforge\/ui$/,
entryPoint: '../packages/ui/src/index.ts'
}
}
import { SHARED_PACKAGES } from '../constants/shared-packages'
import { aliasResolver } from '../resolvers/alias-resolver'
/**
* Creates the standard client/host Vite configuration for LifeForge.
*/
export function defineClientConfig(dirname: string) {
const aliasList = [
...Object.entries(SHARED_PACKAGES).map(([, { aliasRegex, entryPoint }]) => ({
find: aliasRegex,
replacement: path.resolve(dirname, entryPoint)
})),
...Object.entries(SHARED_PACKAGES).map(
([, { aliasRegex, entryPoint }]) => ({
find: aliasRegex,
replacement: path.resolve(dirname, entryPoint)
})
),
{ find: '@modules', replacement: path.resolve(dirname, '../apps') },
{
find: '@',
replacement: '@',
customResolver
customResolver: aliasResolver
}
]
@@ -61,7 +44,16 @@ export function defineClientConfig(dirname: string) {
// MillionLint.vite({}),
react({
babel: {
plugins: [['babel-plugin-react-compiler', ReactCompilerConfig]]
plugins: [
[
'babel-plugin-react-compiler',
{
sources: () => {
return true
}
}
]
]
}
}),
vanillaExtractPlugin(),

View File

@@ -3,6 +3,8 @@ import react from '@vitejs/plugin-react'
import path from 'node:path'
import { type UserConfig, defineConfig, loadEnv, mergeConfig } from 'vite'
import { SHARED_PACKAGES } from '../constants/shared-packages'
interface ModuleConfigOptions {
dirname: string
pkg: {
@@ -20,15 +22,19 @@ export function defineModuleConfig(
overrides: UserConfig | ((env: any) => UserConfig) = {}
) {
const isDocker = process.env.DOCKER_BUILD === 'true'
const outDir = isDocker ? 'dist-docker' : 'dist'
const moduleName = pkg.name.replace('@lifeforge/', '')
return defineConfig((configEnv) => {
return defineConfig(configEnv => {
const isDev = configEnv.command === 'serve'
// 1. Resolve environment variables natively from the root env folder
const envDir = path.resolve(dirname, '../../../env')
const env = loadEnv(configEnv.mode, envDir, '')
const apiHost = isDocker ? '/api' : env.VITE_API_HOST
if (!apiHost) {
@@ -50,38 +56,24 @@ export function defineModuleConfig(
shared: {
react: { generate: false },
'react-dom': { generate: false },
'@lifeforge/shared': { generate: false },
'@lifeforge/federation': { generate: false },
'@lifeforge/ui': { generate: false },
'react-i18next': { generate: false },
i18next: { generate: false },
'@tanstack/react-query': { generate: false }
'@tanstack/react-query': { generate: false },
...Object.fromEntries(
Object.keys(SHARED_PACKAGES).map(e => [e, { generate: false }])
)
}
})
],
resolve: {
alias: [
...(isDev
? [
{
find: /^@lifeforge\/shared$/,
replacement: path.resolve(dirname, '../../../shared/src/index.ts')
},
{
find: /^@lifeforge\/federation$/,
replacement: path.resolve(
dirname,
'../../../packages/federation/src/index.ts'
)
},
{
find: /^@lifeforge\/ui$/,
replacement: path.resolve(
dirname,
'../../../packages/ui/src/index.ts'
)
}
]
? Object.entries(SHARED_PACKAGES).map(
([, { aliasRegex, entryPoint }]) => ({
find: aliasRegex,
replacement: path.resolve(dirname, '/../../', entryPoint)
})
)
: []),
{
find: /^@\/manifest$/,
@@ -99,9 +91,8 @@ export function defineModuleConfig(
}
// 3. Resolve custom overrides if they are passed as a function
const resolvedOverrides = typeof overrides === 'function'
? overrides(configEnv)
: overrides
const resolvedOverrides =
typeof overrides === 'function' ? overrides(configEnv) : overrides
// 4. Merge base configuration with the module-specific overrides
return mergeConfig(baseConfig, resolvedOverrides)