From afb9ae43efbecb8542fe56bd3574032914962ae9 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 7 Jul 2023 14:54:37 +0200 Subject: [PATCH 1/4] feat: warn when publicDir and outDir are nested --- packages/vite/src/node/build.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 41a360a4fd87ed..484d141397576a 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -753,7 +753,21 @@ function prepareOutDir( config.publicDir && fs.existsSync(config.publicDir) ) { - copyDir(config.publicDir, outDir) + if (areSeparateFolders(outDir, config.publicDir)) { + copyDir(config.publicDir, outDir) + } else { + config.logger.warn( + colors.yellow( + `\n${colors.bold( + `(!)`, + )} The public directory feature is diabled. outDir ${colors.white( + colors.dim(outDir), + )} and publicDir ${colors.white( + colors.dim(config.publicDir), + )} are not separate folders.\n`, + ), + ) + } } } } @@ -1223,3 +1237,15 @@ export function toOutputFilePathWithoutRuntime( export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime + +function isSameOrSubfolder(parent: string, dir: string) { + const relative = path.relative(parent, dir) + return ( + relative === '' || + (!relative.startsWith('..') && !path.isAbsolute(relative)) + ) +} + +function areSeparateFolders(a: string, b: string) { + return !isSameOrSubfolder(a, b) && !isSameOrSubfolder(b, a) +} From 7da23f7867f3e60940ac880b42b337c1dedd61cf Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 10 Jul 2023 15:54:37 +0200 Subject: [PATCH 2/4] chore: fix typo Co-authored-by: Bjorn Lu --- packages/vite/src/node/build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 484d141397576a..4075f723c88196 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -760,7 +760,7 @@ function prepareOutDir( colors.yellow( `\n${colors.bold( `(!)`, - )} The public directory feature is diabled. outDir ${colors.white( + )} The public directory feature is disabled. outDir ${colors.white( colors.dim(outDir), )} and publicDir ${colors.white( colors.dim(config.publicDir), From 2b9cc98b0643bc881f739ac141f6888a6cf840d2 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 12 Jul 2023 11:31:03 +0200 Subject: [PATCH 3/4] refactor: simplify areSeparateFolders --- packages/vite/src/node/build.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 4075f723c88196..ceaf719615428c 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1238,14 +1238,8 @@ export function toOutputFilePathWithoutRuntime( export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime -function isSameOrSubfolder(parent: string, dir: string) { - const relative = path.relative(parent, dir) - return ( - relative === '' || - (!relative.startsWith('..') && !path.isAbsolute(relative)) - ) -} - function areSeparateFolders(a: string, b: string) { - return !isSameOrSubfolder(a, b) && !isSameOrSubfolder(b, a) + const na = normalizePath(a) + const nb = normalizePath(b) + return na !== nb && !na.startsWith(nb + '/') && !nb.startsWith(na + '/') } From cf548227213e96729b4d5823c38513ddd22835c9 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 17 Jul 2023 10:57:09 +0200 Subject: [PATCH 4/4] chore: only emit warning, but still copy publicDir --- packages/vite/src/node/build.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index ceaf719615428c..c01bbfa2603257 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -753,14 +753,12 @@ function prepareOutDir( config.publicDir && fs.existsSync(config.publicDir) ) { - if (areSeparateFolders(outDir, config.publicDir)) { - copyDir(config.publicDir, outDir) - } else { + if (!areSeparateFolders(outDir, config.publicDir)) { config.logger.warn( colors.yellow( `\n${colors.bold( `(!)`, - )} The public directory feature is disabled. outDir ${colors.white( + )} The public directory feature may not work correctly. outDir ${colors.white( colors.dim(outDir), )} and publicDir ${colors.white( colors.dim(config.publicDir), @@ -768,6 +766,7 @@ function prepareOutDir( ), ) } + copyDir(config.publicDir, outDir) } } }