From 24a2607210a1e51bbfdae4dfb1daa80143a07619 Mon Sep 17 00:00:00 2001 From: hemengke <23536175@qq.com> Date: Fri, 31 May 2024 16:20:04 +0800 Subject: [PATCH] feat: path-like option support relative/absolute --- README.zh.md | 2 +- playground/spa/vite.config.ts | 2 +- src/node/global-config/GlobalConfigBuilder.ts | 9 ++--- src/node/helper/default-options.ts | 5 ++- src/node/helper/utils.ts | 29 ++++---------- src/node/processor/FileCacheProcessor.ts | 14 ++++--- src/node/processor/ManifestCacheProcessor.ts | 39 ++++++++++--------- 7 files changed, 44 insertions(+), 56 deletions(-) diff --git a/README.zh.md b/README.zh.md index 0858677..bf213a3 100644 --- a/README.zh.md +++ b/README.zh.md @@ -64,7 +64,7 @@ pnpm add vite-plugin-public-typescript -D | -------------- | -------------------------------------- | --------------------------------------------- | ---------------------------------------------- | | inputDir | `string` | `public-typescript` | 存放需要编译的 `typescript` 的目录 | | publicDir | `string` | vite config 中的 publicDir | public 目录 | -| outputDir | `string` | `/` | 输出公共 javascript 的目录,相对于 `publicDir` | +| outputDir | `string` | '' | 输出公共 javascript 的目录,相对于 `publicDir` | | esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 | | babel | `boolean | ESBuildPluginBabelOptions` | `false` | babel编译(如果需要兼容es6以下浏览器,请开启) | | manifestName | `string` | `manifest` | `manifest` 的文件名 | diff --git a/playground/spa/vite.config.ts b/playground/spa/vite.config.ts index 15ccefb..3dea408 100644 --- a/playground/spa/vite.config.ts +++ b/playground/spa/vite.config.ts @@ -18,7 +18,7 @@ export default defineConfig(() => ({ plugins: [ react(), publicTypescript({ - outputDir: 'out', + outputDir: '/out', destination: 'memory', babel: true, }), diff --git a/src/node/global-config/GlobalConfigBuilder.ts b/src/node/global-config/GlobalConfigBuilder.ts index 0e27b20..7636c23 100644 --- a/src/node/global-config/GlobalConfigBuilder.ts +++ b/src/node/global-config/GlobalConfigBuilder.ts @@ -3,16 +3,13 @@ import { type OptionsTypeWithDefault } from '../helper/utils' import { type CacheValue, type ManifestCache } from '../manifest-cache/ManifestCache' import { type BaseCacheProcessor } from '../processor/BaseCacheProcessor' -export type UserConfig = { +export type GlobalConfig = Required & { + viteDevServer?: ViteDevServer manifestCache: ManifestCache originFilesGlob: string[] viteConfig: ResolvedConfig cacheProcessor: BaseCacheProcessor logger: Logger -} & Required - -export type GlobalConfig = UserConfig & { - viteDevServer?: ViteDevServer } export class GlobalConfigBuilder { @@ -22,7 +19,7 @@ export class GlobalConfigBuilder { this._globalConfig = {} as GlobalConfig } - init(c: UserConfig) { + init(c: GlobalConfig) { this._globalConfig = { ...c, } diff --git a/src/node/helper/default-options.ts b/src/node/helper/default-options.ts index acfafbb..75cee69 100644 --- a/src/node/helper/default-options.ts +++ b/src/node/helper/default-options.ts @@ -11,12 +11,13 @@ export function resolveOptions( resolvedViteConfig?: ResolvedConfig, options?: VitePublicTypescriptOptions, ): OptionsTypeWithDefault { - const publicDir = resolve(resolvedViteConfig?.root, resolvedViteConfig?.publicDir || 'public') + const publicDir = options?.publicDir + ? resolve(resolvedViteConfig?.root, options?.publicDir) + : resolvedViteConfig?.publicDir || resolve(resolvedViteConfig?.root, 'public') return { inputDir: resolve(resolvedViteConfig?.root, options?.inputDir || 'public-typescript'), // 相对于 publicDir - // 用户也可以自定义输出目录, e.g. outputDir: 'out/js' outputDir: resolve(publicDir, options?.outputDir || ''), esbuildOptions: options?.esbuildOptions ?? {}, babel: options?.babel ?? false, diff --git a/src/node/helper/utils.ts b/src/node/helper/utils.ts index b088e35..c013d8f 100644 --- a/src/node/helper/utils.ts +++ b/src/node/helper/utils.ts @@ -39,20 +39,16 @@ export function isInTest() { return process.env.VITEST || isCI } -export function isPublicTypescript(args: { filePath: string; inputDir: string; root: string }) { - const { filePath, root, inputDir } = args +export function isPublicTypescript(args: { filePath: string; inputDir: string }) { + const { filePath, inputDir } = args - return ( - path.extname(filePath) === '.ts' && - normalizePath(path.resolve(root, inputDir)).endsWith(normalizePath(path.dirname(filePath))) - ) + return path.extname(filePath) === '.ts' && normalizePath(inputDir).endsWith(normalizePath(path.dirname(filePath))) } export function _isPublicTypescript(filePath: string) { return isPublicTypescript({ filePath, inputDir: globalConfig.get('inputDir'), - root: globalConfig.get('viteConfig')!.root, }) } @@ -117,14 +113,13 @@ export function normalizeAssetsDirPath(dir: string) { } export function getInputDir(resolvedRoot: string, originInputDir: string, suffix = '') { - return normalizePath(path.resolve(resolvedRoot, `${originInputDir}${suffix}`)) + return normalizePath(path.join(resolvedRoot, `${originInputDir}${suffix}`)) } export async function findAllOldJsFile(args: { originFiles: string[]; outputDir: string }) { const { outputDir, originFiles } = args - const dir = outputDir const oldFiles: string[] = [] - if (fs.existsSync(dir)) { + if (fs.existsSync(outputDir)) { for (const originFile of originFiles) { const old = await glob(normalizePath(`${outputDir}/${originFile}.?(*.)js`)) if (old.length > 0) { @@ -145,20 +140,12 @@ export function removeOldJsFiles(oldFiles: string[]) { } } -// NOTE: remmember call this before write compiled js file to disk -export function removeBase(filePath: string, base: string): string { - const devBase = base.at(-1) === '/' ? base : `${base}/` - return filePath.startsWith(devBase) ? filePath.slice(devBase.length - 1) : filePath -} - export async function setupGlobalConfig(viteConfig: ResolvedConfig, opts: OptionsTypeWithDefault) { - const resolvedRoot = normalizePath(viteConfig.root ? path.resolve(viteConfig.root) : process.cwd()) - - fs.ensureDirSync(getInputDir(resolvedRoot, opts.inputDir)) + fs.ensureDirSync(opts.inputDir) - const originFilesGlob = await glob(getInputDir(resolvedRoot, opts.inputDir, `/*.ts`), { + const originFilesGlob = await glob('*.ts', { absolute: true, - cwd: resolvedRoot, + cwd: opts.inputDir, }) const cacheProcessor = initCacheProcessor(opts, manifestCache) diff --git a/src/node/processor/FileCacheProcessor.ts b/src/node/processor/FileCacheProcessor.ts index 9eaf692..4711083 100644 --- a/src/node/processor/FileCacheProcessor.ts +++ b/src/node/processor/FileCacheProcessor.ts @@ -23,11 +23,11 @@ export class FileCacheProcessor extends ManifestCacheProcessor { async deleteOldJs(args: DeleteFileArgs): Promise { const { originFile, compiledFileName = '', silent } = args - const { outputDir } = globalConfig.get(['outputDir', 'viteConfig']) + const { outputDir } = globalConfig.get(['outputDir']) let oldFiles: string[] = [] + fs.ensureDirSync(outputDir) try { - fs.ensureDirSync(outputDir) oldFiles = await findAllOldJsFile({ originFiles: [originFile], outputDir, @@ -62,12 +62,14 @@ export class FileCacheProcessor extends ManifestCacheProcessor { async addNewJs(args: AddFileArgs): Promise { const { code = '' } = args + const { publicDir } = globalConfig.get('viteConfig') - const jsFilePath = this.setCache(args, globalConfig.all) + const pathToDisk = this.setCache(args, globalConfig.all) + const jsFilePath = normalizePath(path.join(publicDir, pathToDisk)) - if (!fs.existsSync(jsFilePath)) { - fs.ensureFileSync(normalizePath(jsFilePath)) - } + if (!jsFilePath) return + + fs.ensureDirSync(path.dirname(jsFilePath)) writeFile(jsFilePath, code) } diff --git a/src/node/processor/ManifestCacheProcessor.ts b/src/node/processor/ManifestCacheProcessor.ts index 044f52a..41d92e6 100644 --- a/src/node/processor/ManifestCacheProcessor.ts +++ b/src/node/processor/ManifestCacheProcessor.ts @@ -1,5 +1,4 @@ import createDebug from 'debug' -import path from 'node:path' import { normalizePath } from 'vite' import { type GlobalConfig } from '../global-config/GlobalConfigBuilder' import { type CacheValueEx } from '../manifest-cache' @@ -24,6 +23,11 @@ export interface AddFileArgs { code?: string } +function removeBase(filePath: string, base: string): string { + const devBase = base.at(-1) === '/' ? base : `${base}/` + return filePath.startsWith(devBase) ? filePath.slice(devBase.length - 1) : filePath +} + export abstract class ManifestCacheProcessor extends BaseCacheProcessor { constructor(manifestCache: ManifestCache) { super(manifestCache) @@ -41,30 +45,27 @@ export abstract class ManifestCacheProcessor extends BaseCacheProcessor