Skip to content

Commit

Permalink
feat: 新增nest模板、新增server类型的vite配置
Browse files Browse the repository at this point in the history
  • Loading branch information
MZ-Dlovely committed Oct 26, 2024
1 parent 6688a6b commit bfd9cbb
Show file tree
Hide file tree
Showing 26 changed files with 1,770 additions and 125 deletions.
15 changes: 15 additions & 0 deletions apps/backend-nest/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 适配器
VITE_ADAPTER=nest

# 应用名称
VITE_APP_NAME=Vben Server

# 应用入口
VITE_APP_PATH=src

# 应用入口的导出变量
VITE_EXPORT_NAME=default

# 立即启动
VITE_IMMEDIATE=true

1 change: 1 addition & 0 deletions apps/backend-nest/.env.analyze
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_VISUALIZER=true
2 changes: 2 additions & 0 deletions apps/backend-nest/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 端口号
VITE_PORT=5777
2 changes: 2 additions & 0 deletions apps/backend-nest/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 端口号
VITE_PORT=3000
7 changes: 7 additions & 0 deletions apps/backend-nest/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"explorer.fileNesting.patterns": {
"*.ts": "${capture}.module.ts,${capture}.service.ts,${capture}.dto.ts,${capture}.controller.ts",
"*.module.ts": "${capture}.module.ts,${capture}.service.ts,${capture}.dto.ts",
"*.service.ts": "${capture}.dto.ts"
}
}
27 changes: 27 additions & 0 deletions apps/backend-nest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@vben/backend-nest",
"version": "0.0.1",
"description": "",
"private": true,
"license": "MIT",
"author": "",
"scripts": {
"build": "pnpm vite build --mode production",
"build:analyze": "pnpm vite build --mode analyze",
"dev": "pnpm vite --mode development",
"preview": "vite preview",
"typecheck": "vue-tsc --noEmit --skipLibCheck"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@types/express": "^4.17.17",
"@types/node": "catalog:"
}
}
5 changes: 5 additions & 0 deletions apps/backend-nest/src/app/index.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Module } from '@nestjs/common';

@Module({})
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class AppModule {}
29 changes: 29 additions & 0 deletions apps/backend-nest/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { INestApplication } from '@nestjs/common';

import { NestFactory } from '@nestjs/core';

import { AppModule } from './index.module';

let app: INestApplication;

async function createApp() {
const app = await NestFactory.create(AppModule);

app.enableCors();

return app;
}

export async function useApp() {
if (!app) {
app = await createApp();
}

return app;
}

export async function bootstrap() {
const app = await useApp();

await app.listen(import.meta.env.VITE_PORT);
}
5 changes: 5 additions & 0 deletions apps/backend-nest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { bootstrap, useApp } from './app';

if (import.meta.env.PROD) bootstrap();

export default useApp();
4 changes: 4 additions & 0 deletions apps/backend-nest/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
12 changes: 12 additions & 0 deletions apps/backend-nest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@vben/tsconfig/node.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"#/*": ["./src/*"]
},
"types": ["vite/client"]
},
"include": ["src/**/*.ts"]
}
8 changes: 8 additions & 0 deletions apps/backend-nest/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from '@vben/vite-config';

export default defineConfig(async () => {
return {
server: {},
vite: {},
};
}, 'server');
53 changes: 27 additions & 26 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@
"language": "en,en-US",
"allowCompoundWords": true,
"words": [
"clsx",
"esno",
"demi",
"unref",
"taze",
"acmr",
"antd",
"lucide",
"antdv",
"astro",
"brotli",
"clsx",
"defu",
"demi",
"echarts",
"ependencies",
"esno",
"etag",
"execa",
"iconify",
"iconoir",
"intlify",
"lockb",
"lucide",
"mkdist",
"mockjs",
"vitejs",
"naiveui",
"nestjs",
"nocheck",
"noopener",
"noreferrer",
"nprogress",
"nuxt",
"pinia",
"prefixs",
"publint",
"qrcode",
"shadcn",
"sonner",
"sortablejs",
"styl",
"taze",
"ui-kit",
"uicons",
"unplugin",
"unref",
"vben",
"vbenjs",
"vueuse",
"yxxx",
"nuxt",
"lockb",
"astro",
"ui-kit",
"styl",
"vnode",
"nocheck",
"prefixs",
"vitepress",
"antdv",
"ependencies",
"vite",
"echarts",
"sortablejs",
"etag",
"naiveui",
"uicons",
"iconoir"
"vitejs",
"vitepress",
"vnode",
"vueuse",
"yxxx"
],
"ignorePaths": [
"**/node_modules/**",
Expand Down
3 changes: 2 additions & 1 deletion internal/vite-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"vite-plugin-compression": "catalog:",
"vite-plugin-dts": "catalog:",
"vite-plugin-html": "catalog:",
"vite-plugin-lazy-import": "catalog:"
"vite-plugin-lazy-import": "catalog:",
"vite-plugin-node": "catalog:"
}
}
4 changes: 2 additions & 2 deletions internal/vite-config/src/config/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { defineConfig, loadEnv, mergeConfig } from 'vite';

import { defaultImportmapOptions, getDefaultPwaOptions } from '../options';
import { loadApplicationPlugins } from '../plugins';
import { loadAndConvertEnv } from '../utils/env';
import { loadApplicationEnv } from '../utils/env';
import { getCommonConfig } from './common';

function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
return defineConfig(async (config) => {
const options = await userConfigPromise?.(config);
const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
const { appTitle, base, port, ...envConfig } = await loadApplicationEnv();
const { command, mode } = config;
const { application = {}, vite = {} } = options || {};
const root = process.cwd();
Expand Down
6 changes: 5 additions & 1 deletion internal/vite-config/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { join } from 'node:path';

import { defineApplicationConfig } from './application';
import { defineLibraryConfig } from './library';
import { defineServerConfig } from './server';

export * from './application';
export * from './library';

function defineConfig(
userConfigPromise?: DefineConfig,
type: 'application' | 'auto' | 'library' = 'auto',
type: 'application' | 'auto' | 'library' | 'server' = 'auto',
) {
let projectType = type;

Expand All @@ -28,6 +29,9 @@ function defineConfig(
case 'library': {
return defineLibraryConfig(userConfigPromise);
}
case 'server': {
return defineServerConfig(userConfigPromise);
}
default: {
throw new Error(`Unsupported project type: ${projectType}`);
}
Expand Down
63 changes: 63 additions & 0 deletions internal/vite-config/src/config/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { UserConfig } from 'vite';

import type { DefineServerOptions } from '../typing';

import { defineConfig, loadEnv, mergeConfig } from 'vite';

import { loadServerPlugins } from '../plugins';
import { loadServerEnv } from '../utils/env';
import { getCommonConfig } from './common';

function defineServerConfig(userConfigPromise?: DefineServerOptions) {
return defineConfig(async (config) => {
const options = await userConfigPromise?.(config);
const { port, ...envConfig } = await loadServerEnv();
const { command, mode } = config;
const { server = {}, vite = {} } = options || {};
const root = process.cwd();
const isBuild = command === 'build';
const env = loadEnv(mode, root);

const plugins = await loadServerPlugins({
env,
injectMetadata: true,
isBuild,
mode,
noElement: true,
...envConfig,
...server,
});

const serverConfig: UserConfig = {
esbuild: {
drop: isBuild
? [
// 'console',
'debugger',
]
: [],
legalComments: 'none',
},
optimizeDeps: {
exclude: [
'@nestjs/microservices',
'@nestjs/websockets',
'cache-manager',
'class-transformer',
'class-validator',
'fastify-swagger',
],
},
plugins,
server: { host: true, port },
};

const mergedCommonConfig = mergeConfig(
await getCommonConfig(),
serverConfig,
);
return mergeConfig(mergedCommonConfig, vite);
});
}

export { defineServerConfig };
2 changes: 1 addition & 1 deletion internal/vite-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './config';
export * from './options';
export * from './plugins';
export { loadAndConvertEnv } from './utils/env';
export { loadApplicationEnv as loadAndConvertEnv } from './utils/env';
Loading

0 comments on commit bfd9cbb

Please sign in to comment.