From 90f0dc0578069b3a1850f88fb001a9352e7f70c0 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:41:46 +0900 Subject: [PATCH 1/2] fix: using absolute path src not working with `structured: true` --- .changeset/metal-walls-switch.md | 5 +++++ src/utils.ts | 15 ++++++++++----- test/fixtures/vite.structured.config.ts | 9 +++++++++ test/testcases.ts | 7 ++++++- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 .changeset/metal-walls-switch.md 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..05c3ce1 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' + }, ] } From acf6610eeb0803efaa907e90dfa4e4cc7dda8c4d Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:42:48 +0900 Subject: [PATCH 2/2] chore: format --- test/testcases.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcases.ts b/test/testcases.ts index 05c3ce1..27bc87d 100644 --- a/test/testcases.ts +++ b/test/testcases.ts @@ -166,6 +166,6 @@ export const testcases: Record = { name: 'absolute path', src: './dir/bar.txt', dest: '/fixture4/dir/bar.txt' - }, + } ] }