Skip to content

Commit

Permalink
fix: using absolute path src not working with structured: true (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jan 25, 2024
1 parent 602627d commit 19b9fca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-walls-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-static-copy': patch
---

using absolute path src with `structured: true` was not working
15 changes: 10 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,21 @@ export const collectCopyTargets = async (
})

for (const matchedPath of matchedPaths) {
const relativeMatchedPath = path.isAbsolute(matchedPath)
? path.relative(root, matchedPath)
: matchedPath
const absoluteMatchedPath = path.resolve(root, matchedPath)

if (transform) {
const srcStat = await fs.stat(path.resolve(root, matchedPath))
const srcStat = await fs.stat(absoluteMatchedPath)
if (!srcStat.isFile()) {
throw new Error(
`"transform" option only supports a file: '${matchedPath}' is not a file`
`"transform" option only supports a file: '${relativeMatchedPath}' is not a file`
)
}
}

const { base, dir } = path.parse(matchedPath)
const { base, dir } = path.parse(relativeMatchedPath)

let destDir: string
if (!structured || !dir) {
Expand All @@ -81,10 +86,10 @@ export const collectCopyTargets = async (
}

copyTargets.push({
src: matchedPath,
src: relativeMatchedPath,
dest: path.join(
destDir,
rename ? await renameTarget(base, rename, matchedPath) : base
rename ? await renameTarget(base, rename, absoluteMatchedPath) : base
),
transform,
preserveTimestamps: preserveTimestamps ?? false,
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/vite.structured.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { defineConfig } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import path from 'node:path'
import url from 'node:url'
import { normalizePath } from 'vite'

const _dirname = path.dirname(url.fileURLToPath(import.meta.url))

export default defineConfig({
appType: 'custom', // disable SPA/MPA fallback
Expand Down Expand Up @@ -32,6 +37,10 @@ export default defineConfig({
{
src: 'dir/bar.txt',
dest: ''
},
{
src: normalizePath(path.resolve(_dirname, 'dir/*.txt')),
dest: 'fixture4'
}
],
structured: true
Expand Down
5 changes: 5 additions & 0 deletions test/testcases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export const testcases: Record<string, Testcase[]> = {
name: 'dir to empty dest',
src: './dir/bar.txt',
dest: '/dir/bar.txt'
},
{
name: 'absolute path',
src: './dir/bar.txt',
dest: '/fixture4/dir/bar.txt'
}
]
}

0 comments on commit 19b9fca

Please sign in to comment.