diff --git a/.changeset/metal-walls-switch.md b/.changeset/metal-walls-switch.md new file mode 100644 index 0000000..bcc9d63 --- /dev/null +++ b/.changeset/metal-walls-switch.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-static-copy': patch +--- + +using absolute path src with `structured: true` was not working diff --git a/src/utils.ts b/src/utils.ts index c1c6f3d..8c0f713 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) { @@ -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, diff --git a/test/fixtures/vite.structured.config.ts b/test/fixtures/vite.structured.config.ts index af63a0d..990c2e2 100644 --- a/test/fixtures/vite.structured.config.ts +++ b/test/fixtures/vite.structured.config.ts @@ -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 @@ -32,6 +37,10 @@ export default defineConfig({ { src: 'dir/bar.txt', dest: '' + }, + { + src: normalizePath(path.resolve(_dirname, 'dir/*.txt')), + dest: 'fixture4' } ], structured: true diff --git a/test/testcases.ts b/test/testcases.ts index 959edfb..27bc87d 100644 --- a/test/testcases.ts +++ b/test/testcases.ts @@ -161,6 +161,11 @@ export const testcases: Record = { 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' } ] }