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

feat!: improve flatten: false and rename to structured: true #57

Merged
merged 5 commits into from
Jun 3, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ See [options.ts](https://github.com/sapphi-red/vite-plugin-static-copy/blob/main
- Because `fast-glob` is used inside `vite`.
- `transform` could return `null` as a way to tell the plugin not to copy the file, this is similar to the [CopyWebpackPlugin#filter](https://webpack.js.org/plugins/copy-webpack-plugin/#filter) option, but it expects `transform` to return the original content in case you want it to be copied.
- `transform` can optionally be an object, with a `handler` property (with the same signature of the `rollup-plugin-copy` transform option) and an `encoding` property (`BufferEncoding | 'buffer'`) that will be used to read the file content so that the `handler`'s content argument will reflect the correct encoding (could be Buffer);
- `structured: true` preserves the directory structure. This is similar to `flatten: false` in `rollup-plugin-copy`, but it covers more edge cases.
4 changes: 2 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { copyAll, outputCopyLog } from './utils'

export const buildPlugin = ({
targets,
flatten,
structured,
silent
}: ResolvedViteStaticCopyOptions): Plugin => {
let config: ResolvedConfig
Expand All @@ -20,7 +20,7 @@ export const buildPlugin = ({
config.root,
config.build.outDir,
targets,
flatten
structured
)
if (!silent) outputCopyLog(config.logger, result)
}
Expand Down
12 changes: 7 additions & 5 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ export type ViteStaticCopyOptions = {
*/
targets: Target[]
/**
* Remove the directory structure.
* @default true
* Preserve the directory structure.
*
* Similar to `flatten: false` in rollup-plugin-copy
* @default false
*/
flatten?: boolean
structured?: boolean
/**
* Suppress console output.
* @default false
Expand All @@ -107,7 +109,7 @@ export type ViteStaticCopyOptions = {

export type ResolvedViteStaticCopyOptions = {
targets: Target[]
flatten: boolean
structured: boolean
silent: boolean
watch: {
options: WatchOptions
Expand All @@ -119,7 +121,7 @@ export const resolveOptions = (
options: ViteStaticCopyOptions
): ResolvedViteStaticCopyOptions => ({
targets: options.targets,
flatten: options.flatten ?? true,
structured: options.structured ?? false,
silent: options.silent ?? false,
watch: {
options: options.watch?.options ?? {},
Expand Down
4 changes: 2 additions & 2 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type FileMap = Map<string, FileMapValue[]>

export const servePlugin = ({
targets,
flatten,
structured,
watch,
silent
}: ResolvedViteStaticCopyOptions): Plugin => {
Expand All @@ -34,7 +34,7 @@ export const servePlugin = ({
const copyTargets = await collectCopyTargets(
config.root,
targets,
flatten
structured
)
updateFileMapFromTargets(copyTargets, fileMap)
} catch (e) {
Expand Down
21 changes: 12 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function renameTarget(
export const collectCopyTargets = async (
root: string,
targets: Target[],
flatten: boolean
structured: boolean
) => {
const copyTargets: Array<SimpleTarget> = []

Expand Down Expand Up @@ -69,13 +69,16 @@ export const collectCopyTargets = async (
}
}

// https://github.com/vladshcherbin/rollup-plugin-copy/blob/507bf5e99aa2c6d0d858821e627cb7617a1d9a6d/src/index.js#L32-L35
const { base, dir } = path.parse(matchedPath)
const destDir =
flatten || (!flatten && !dir)
? dest
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
dir.replace(dir.split('/')[0]!, dest)

let destDir: string
if (!structured || !dir) {
destDir = dest
} else {
const dirClean = dir.replace(/^(?:\.\.\/)+/, '')
const destClean = `${dest}/${dirClean}`.replace(/^\/+|\/+$/g, '')
destDir = destClean
}

copyTargets.push({
src: matchedPath,
Expand Down Expand Up @@ -137,9 +140,9 @@ export const copyAll = async (
rootSrc: string,
rootDest: string,
targets: Target[],
flatten: boolean
structured: boolean
) => {
const copyTargets = await collectCopyTargets(rootSrc, targets, flatten)
const copyTargets = await collectCopyTargets(rootSrc, targets, structured)
let copiedCount = 0

for (const copyTarget of copyTargets) {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"build:absolute": "vite --config vite.absolute.config.ts build",
"dev:base": "vite --config vite.base.config.ts",
"build:base": "vite --config vite.base.config.ts build",
"dev:noflatten": "vite --config vite.noflatten.config.ts",
"build:noflatten": "vite --config vite.noflatten.config.ts build"
"dev:structured": "vite --config vite.structured.config.ts",
"build:structured": "vite --config vite.structured.config.ts build"
},
"devDependencies": {
"vite": "^4.3.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { viteStaticCopy } from 'vite-plugin-static-copy'

export default defineConfig({
build: {
outDir: './dist-noflatten'
outDir: './dist-structured'
},
plugins: [
viteStaticCopy({
Expand All @@ -19,9 +19,21 @@ export default defineConfig({
{
src: '../fixtures2/*.txt',
dest: 'fixture3'
},
{
src: 'foo.js',
dest: ''
},
{
src: 'noext',
dest: '.'
},
{
src: 'dir/bar.txt',
dest: ''
}
],
flatten: false
structured: true
})
]
})
19 changes: 17 additions & 2 deletions test/testcases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const testcases: Record<string, Testcase[]> = {
dest: '/base/fixture1/foo.txt'
}
],
'vite.noflatten.config.ts': [
'vite.structured.config.ts': [
{
name: 'glob without dir',
src: './foo.txt',
Expand All @@ -140,12 +140,27 @@ export const testcases: Record<string, Testcase[]> = {
{
name: 'glob with dir',
src: './dir/bar.txt',
dest: '/fixture2/bar.txt'
dest: '/fixture2/dir/bar.txt'
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: 'glob with parent dir',
src: '../fixtures2/baz.txt',
dest: '/fixture3/fixtures2/baz.txt'
},
{
name: 'empty dest',
src: './foo.js',
dest: '/foo.js'
},
{
name: 'dot dest',
src: './noext',
dest: '/noext'
},
{
name: 'dir to empty dest',
src: './dir/bar.txt',
dest: '/dir/bar.txt'
}
]
}