Skip to content
Open
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
64 changes: 41 additions & 23 deletions src/features/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ export interface CopyEntry {
export type CopyOptions = Arrayable<string | CopyEntry>
export type CopyOptionsFn = (options: ResolvedConfig) => Awaitable<CopyOptions>

export async function copy(options: ResolvedConfig): Promise<void> {
if (!options.copy) return
type ResolvedCopyEntry = CopyEntry & { from: string; to: string }

export async function resolveCopyEntries(
options: ResolvedConfig,
): Promise<ResolvedCopyEntry[]> {
if (!options.copy) return []

const copy: CopyOptions =
typeof options.copy === 'function'
Expand All @@ -61,11 +65,25 @@ export async function copy(options: ResolvedConfig): Promise<void> {
})
}

return from.map((file) => resolveCopyEntry({ ...entry, from: file }))
return from.map((file) =>
resolveCopyEntry(
{ ...entry, from: file },
options.cwd,
options.outDir,
),
)
}),
)
).flat()

return resolved
}

export async function copy(options: ResolvedConfig): Promise<void> {
if (!options.copy) return

const resolved = await resolveCopyEntries(options)

if (!resolved.length) {
options.logger.warn(options.nameLabel, `No files matched for copying.`)
return
Expand All @@ -85,28 +103,28 @@ export async function copy(options: ResolvedConfig): Promise<void> {
return fsCopy(from, to)
}),
)
}

// https://github.com/vladshcherbin/rollup-plugin-copy/blob/master/src/index.js
// MIT License
function resolveCopyEntry(
entry: CopyEntry & { from: string },
): CopyEntry & { from: string; to: string } {
const { flatten = true, rename } = entry
const from = path.resolve(options.cwd, entry.from)
const to = entry.to ? path.resolve(options.cwd, entry.to) : options.outDir

const { base, dir } = path.parse(path.relative(options.cwd, from))
const destFolder =
flatten || (!flatten && !dir)
? to
: dir.replace(dir.split(path.sep)[0], to)
const dest = path.join(
destFolder,
rename ? renameTarget(base, rename, from) : base,
)
// https://github.com/vladshcherbin/rollup-plugin-copy/blob/master/src/index.js
// MIT License
function resolveCopyEntry(
entry: CopyEntry & { from: string },
cwd: string,
outDir: string,
): CopyEntry & { from: string; to: string } {
const { flatten = true, rename } = entry
const from = path.resolve(cwd, entry.from)
const to = entry.to ? path.resolve(cwd, entry.to) : outDir

return { ...entry, from, to: dest }
}
const { base, dir } = path.parse(path.relative(cwd, from))
const destFolder =
flatten || (!flatten && !dir) ? to : dir.replace(dir.split(path.sep)[0], to)
const dest = path.join(
destFolder,
rename ? renameTarget(base, rename, from) : base,
)

return { ...entry, from, to: dest }
}

function renameTarget(
Expand Down
12 changes: 11 additions & 1 deletion src/features/watch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addOutDirToChunks } from '../utils/chunks.ts'
import { resolveComma, toArray } from '../utils/general.ts'
import { resolveCopyEntries } from './copy.ts'
import type { TsdownBundle } from '../config/types.ts'
import type { Plugin } from 'rolldown'

Expand All @@ -19,7 +20,7 @@ export function WatchPlugin(
inputOptions.watch.exclude.push(...config.ignoreWatch)
}
: undefined,
buildStart() {
async buildStart() {
config.tsconfig && this.addWatchFile(config.tsconfig)
for (const file of configFiles) {
this.addWatchFile(file)
Expand All @@ -32,6 +33,15 @@ export function WatchPlugin(
if (config.pkg) {
this.addWatchFile(config.pkg.packageJsonPath)
}

// Watch copy source files
if (config.copy) {
const resolvedEntries = await resolveCopyEntries(config)

for (const entry of resolvedEntries) {
this.addWatchFile(entry.from)
}
}
},
generateBundle: {
order: 'post',
Expand Down
Loading