Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增nest模板、新增server类型的vite配置 #4743

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions apps/backend-nest/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 端口号
VITE_PORT=5777

# 是否开启 SWC
VITE_SWC=true
5 changes: 5 additions & 0 deletions apps/backend-nest/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 端口号
VITE_PORT=3000

# 是否开启 SWC
VITE_SWC=false
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"
},
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved
"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;
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved

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

app.enableCors();

return app;
}
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved

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

return app;
}
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved

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

await app.listen(import.meta.env.VITE_PORT);
}
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved
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();
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved

export default useApp();
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 3 additions & 1 deletion internal/vite-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"@pnpm/workspace.read-manifest": "catalog:",
"@swc/core": "catalog:",
"@types/archiver": "catalog:",
"@types/html-minifier-terser": "catalog:",
"@vben/node-utils": "workspace:*",
Expand All @@ -55,6 +56,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',
]
: [],
MZ-Dlovely marked this conversation as resolved.
Show resolved Hide resolved
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