From a66b75b117c63068dcbffb2bb1d597d9f37ea2eb Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Tue, 12 Nov 2024 17:01:21 +0800 Subject: [PATCH] chore: optimize public dir logic --- packages/core/src/config/index.ts | 3 ++- packages/core/src/config/schema.ts | 3 ++- packages/core/src/types/binding.ts | 5 +++++ packages/core/src/utils/fsUtils.ts | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index 23d5e1444..b3ddfc139 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -561,7 +561,8 @@ export const DEFAULT_DEV_SERVER_OPTIONS: NormalizedServerConfig = { export const DEFAULT_COMPILATION_OPTIONS: Partial = { output: { - path: './dist' + path: './dist', + copyPublicDir: true }, sourcemap: true, resolve: { diff --git a/packages/core/src/config/schema.ts b/packages/core/src/config/schema.ts index 4bc7a484a..67dff92c0 100644 --- a/packages/core/src/config/schema.ts +++ b/packages/core/src/config/schema.ts @@ -62,7 +62,8 @@ const outputSchema = z publicPath: z.string().optional(), assetsFilename: z.string().optional(), targetEnv: z.nativeEnum(TargetEnv).optional(), - format: z.enum(['cjs', 'esm']).optional() + format: z.enum(['cjs', 'esm']).optional(), + copyPublicDir: z.boolean().optional() }) .strict() .optional(); diff --git a/packages/core/src/types/binding.ts b/packages/core/src/types/binding.ts index 113d200a0..707642986 100644 --- a/packages/core/src/types/binding.ts +++ b/packages/core/src/types/binding.ts @@ -165,6 +165,11 @@ export interface OutputConfig { * clean output.path automatically or not */ clean?: boolean; + /** + * copy public directory to output.path or not + * Boolean @default true + */ + copyPublicDir?: boolean; } export interface ResolveConfig { diff --git a/packages/core/src/utils/fsUtils.ts b/packages/core/src/utils/fsUtils.ts index e28ef7a71..c3db4dcab 100644 --- a/packages/core/src/utils/fsUtils.ts +++ b/packages/core/src/utils/fsUtils.ts @@ -190,3 +190,20 @@ export async function copyPublicDirectory( ); } } + +export function copyDir(srcDir: string, destDir: string): void { + fs.mkdirSync(destDir, { recursive: true }); + for (const file of fs.readdirSync(srcDir)) { + const srcFile = path.resolve(srcDir, file); + if (srcFile === destDir) { + continue; + } + const destFile = path.resolve(destDir, file); + const stat = fs.statSync(srcFile); + if (stat.isDirectory()) { + copyDir(srcFile, destFile); + } else { + fs.copyFileSync(srcFile, destFile); + } + } +}