后端:解耦配置的读取

前端:实现配置文件从环境变量获取
This commit is contained in:
lsy 2024-11-11 23:57:35 +08:00
parent 7f2a02dc61
commit bd619e7519
11 changed files with 123 additions and 30 deletions

3
frontend/.env Normal file
View File

@ -0,0 +1,3 @@
VITE_APP_API='http://localhost:8000/api'
VITE_APP_THEMES='@/themes'
VITE_APP_PLUGINS='@/plugins'

28
frontend/eslint.config.js Normal file
View File

@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)

View File

@ -1,29 +0,0 @@
// services/dependency-checker.ts
import { Dependency } from "../types/common";
/**
*
*/
export class DependencyChecker {
/**
*
* @param dependencies
* @returns
*/
static async checkDependencies(dependencies: Dependency[]): Promise<boolean> {
for (const dep of dependencies) {
const response = await fetch(`/api/dependencies/check`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(dep)
});
if (!response.ok) {
console.error(`依赖检查失败: ${dep.id}`);
return false;
}
}
return true;
}
}

11
frontend/src/env.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_APP_API: string
readonly VITE_APP_THEMES: string
readonly VITE_APP_PLUGINS: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}

View File

@ -8,6 +8,8 @@ import {createContext} from "react";
export const serverAddressContext=createContext("localhost:8080") export const serverAddressContext=createContext("localhost:8080")
// 动态路由 // 动态路由
const RouterListener: React.FC = () => { const RouterListener: React.FC = () => {
const a=import.meta.env.VITE_APP_THEMES;
console.log(a)
const pathname = location.pathname.split("/"); const pathname = location.pathname.split("/");
console.log(pathname) console.log(pathname)
return ( return (

View File

@ -1,7 +1,8 @@
// page/page.tsx // page/page.tsx
import React from "react"; import React from "react";
const THEMEPATH= "../../themes" const THEMEPATH= "@/themes"
import {serverAddressContext} from "../main.tsx"; import {serverAddressContext} from "../main.tsx";
// 动态获取当前主题 // 动态获取当前主题
const getCurrentTheme = async (): Promise<string> => { const getCurrentTheme = async (): Promise<string> => {
return new Promise<string>((resolve) => { return new Promise<string>((resolve) => {

View File

@ -0,0 +1,29 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "Bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true,
"paths": {
"@/*": ["./*"]
}
},
"include": ["src","utils","themes","public"]
}

7
frontend/tsconfig.json Normal file
View File

@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" }
]
}

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "ES2022",
"lib": ["ES2023"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "Bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true,
},
"include": ["vite.config.ts"]
}

12
frontend/vite.config.ts Normal file
View File

@ -0,0 +1,12 @@
import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': __dirname,
}
}
})

5
package.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"react-helmet": "^6.1.0"
}
}