Skip to content

Commit c6bf44c

Browse files
authored
fix: handle absolute path dest correctly (#249)
1 parent 9ca2d6a commit c6bf44c

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'vite-plugin-static-copy': patch
3+
---
4+
5+
Fix absolute `dest` paths being nested under the output directory
6+
7+
When `dest` was an absolute path and the source file had a directory component (structured output), the path was incorrectly converted to a relative path, causing files to be nested under the build output directory instead of being copied to the specified absolute path.
8+
9+
```js
10+
{ src: 'foo/foo.txt', dest: '/home/user/my-repo/bar' }
11+
```
12+
13+
**Before**: `/home/user/my-repo/dist/home/user/my-repo/bar/foo/foo.txt`
14+
**After**: `/home/user/my-repo/bar/foo/foo.txt`

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ export const collectCopyTargets = async (
249249
if (!dir) {
250250
destDir = dest
251251
} else {
252-
const destClean = `${dest}/${dirClean}`.replace(/^\/+|\/+$/g, '')
253-
destDir = destClean
252+
destDir = path.join(dest, dirClean)
254253
}
255254

256255
copyTargets.push({

test/fixtures/vite.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ export default defineConfig({
151151
src: 'foo.txt',
152152
dest: path.resolve(_dirname, 'dist/fixture13'),
153153
},
154+
{
155+
src: 'dir/bar.txt',
156+
dest: path.resolve(_dirname, 'dist/fixture13-structured'),
157+
},
158+
{
159+
src: normalizePath(path.resolve(_dirname, 'dir/*.txt')),
160+
dest: 'fixture13-absolute-src',
161+
},
154162
{
155163
src: 'foo.txt',
156164
dest: 'fixture14/sub',

test/testcases.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ export const testcases: Record<string, Testcase[]> = {
157157
src: './foo.txt',
158158
dest: '/fixture13/foo.txt',
159159
},
160+
{
161+
name: 'absolute path dest with structured dir',
162+
src: './dir/bar.txt',
163+
dest: '/fixture13-structured/dir/bar.txt',
164+
},
165+
{
166+
name: 'absolute src and absolute dest',
167+
src: './dir/bar.txt',
168+
dest: '/fixture13-absolute-src/dir/bar.txt',
169+
},
160170
{
161171
name: 'rename with ../ (non-structured)',
162172
src: './foo.txt',

0 commit comments

Comments
 (0)