From 31230da7222df2b12d3368f792340c7be3324a1a Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Tue, 28 May 2024 10:33:08 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=93=A6=EF=B8=8F=20=E6=89=93?= =?UTF-8?q?=E5=8C=85=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/buildEnv.ts | 2 +- build/utils.ts | 24 ++++++++++- tsconfig.json | 101 ++++++++++++++++++++++++--------------------- tsconfig.node.json | 4 +- vite.config.ts | 2 + 5 files changed, 82 insertions(+), 51 deletions(-) diff --git a/build/buildEnv.ts b/build/buildEnv.ts index 041ca41..60a91a8 100644 --- a/build/buildEnv.ts +++ b/build/buildEnv.ts @@ -36,7 +36,7 @@ export const buildEnvironment = () => { entryFileNames: 'js/[name]-[hash].js', // 用于指定入口文件的输出文件名格式 assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 用于指定静态资源的输出文件名格式 // ? 配置文件生成方式 - manualChunks: (id, meta) => { + manualChunks: id => { // 如果是包含在包中则打包成 vendor if (id.includes('node_modules')) { return 'vendor'; diff --git a/build/utils.ts b/build/utils.ts index 257332a..ff5cfcf 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -1,5 +1,7 @@ import { readdir, stat } from 'node:fs'; import { formatBytes, sum } from '@pureadmin/utils'; +import { fileURLToPath } from 'node:url'; +import { dirname, resolve } from 'node:path'; const fileListTotal: number[] = []; @@ -30,4 +32,24 @@ const getPackageSize = options => { }); }; -export { getPackageSize }; +/** + * @description 根据可选的路径片段生成一个新的绝对路径 + * @param dir 路径片段,默认`build` + * @param metaUrl 模块的完整`url`,如果在`build`目录外调用必传`import.meta.url` + */ +const pathResolve = (dir = '.', metaUrl = import.meta.url) => { + // 当前文件目录的绝对路径 + const currentFileDir = dirname(fileURLToPath(metaUrl)); + // build 目录的绝对路径 + const buildDir = resolve(currentFileDir, 'build'); + // 解析的绝对路径 + const resolvedPath = resolve(currentFileDir, dir); + // 检查解析的绝对路径是否在 build 目录内 + if (resolvedPath.startsWith(buildDir)) { + // 在 build 目录内,返回当前文件路径 + return fileURLToPath(metaUrl); + } + // 不在 build 目录内,返回解析后的绝对路径 + return resolvedPath; +}; +export { getPackageSize, pathResolve }; diff --git a/tsconfig.json b/tsconfig.json index 2c6516e..fbb0204 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,50 +1,55 @@ { - "compilerOptions": { - // 是否在表达式和声明上有隐含的any类型时报错 - "noImplicitAny": false, - "target": "ES2020", - "useDefineForClassFields": true, - "module": "ESNext", - "lib": [ - "ES2020", - "DOM", - "DOM.Iterable" - ], - "baseUrl": ".", - "skipLibCheck": true, - "types": [ - // "webpack-env" - "node" - ], - "paths": { - "@/*": [ - "src/*" - ] - }, - /* Bundler mode */ - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "resolveJsonModule": true, - "isolatedModules": true, - "noEmit": true, - "jsx": "preserve", - /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true - }, - "include": [ - "src/**/*.ts", - "src/**/*.tsx", - "src/**/*.vue" - ], - "exclude": [ - "node_modules" - ], - "references": [ - { - "path": "./tsconfig.node.json" - } - ] + "compilerOptions": { + // 是否在表达式和声明上有隐含的any类型时报错 + "noImplicitAny": false, + "target": "ES2020", + "useDefineForClassFields": true, + "module": "ESNext", + "lib": [ + "ES2020", + "DOM", + "DOM.Iterable" + ], + "baseUrl": ".", + "skipLibCheck": true, + "types": [ + // "webpack-env" + "node" + ], + "paths": { + "@/*": [ + "src/*" + ], + "@build/*": [ + "build/*" + ] + }, + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "preserve", + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": [ + "src/**/*.ts", + "src/**/*.tsx", + "src/**/*.vue", + "build/*.ts", + "vite.config.ts" + ], + "exclude": [ + "node_modules" + ], + "references": [ + { + "path": "./tsconfig.node.json" + } + ] } \ No newline at end of file diff --git a/tsconfig.node.json b/tsconfig.node.json index 97ede7e..c7a071e 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -7,5 +7,7 @@ "allowSyntheticDefaultImports": true, "strict": true }, - "include": ["vite.config.ts"] + "include": [ + "vite.config.ts" + ] } diff --git a/vite.config.ts b/vite.config.ts index b80677f..279e030 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,6 +4,7 @@ import { buildEnvironment } from './build/buildEnv'; import { exclude, include } from './build/optimize'; import { getPluginsList } from './build/plugins'; import { serverOptions } from './build/server'; +import { pathResolve } from './build/utils.ts'; export default defineConfig( (): UserConfig => ({ @@ -12,6 +13,7 @@ export default defineConfig( alias: { '@': resolve(__dirname, './src'), 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js', + '@build': pathResolve(), }, }, optimizeDeps: { include, exclude },