Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: using absolute path src not working with structured: true #79

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
]
}
Loading