Skip to content

Commit 4445898

Browse files
committed
Add custom copy plugin for esbuild
Introduces a new copy plugin in build/plugins/copy.ts and integrates it into the esbuild build process. This refactors asset handling by replacing the previous loader-based approach with a plugin, simplifying configuration and improving maintainability.
1 parent 07b5c21 commit 4445898

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

build/esbuild.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,28 @@ import * as esbuild from '@esbuild';
33
import { denoPlugin as esbuildPluginDeno } from "@deno/esbuild-plugin";
44
import { bold, green, magenta } from '@std/fmt/colors';
55
import { parseArgs } from '@std/cli/parse-args';
6+
import { copy as esbuildPluginCopy } from './plugins/copy.ts';
67

78
const args = parseArgs<{
89
watch: boolean | undefined,
910
develop: boolean | undefined,
1011
logLevel: esbuild.LogLevel
1112
}>(Deno.args);
1213

13-
// convert array to esbuild copy loader object
14-
const loaders : { [ext: string]: esbuild.Loader } = [
15-
".html",
16-
".css",
17-
".svg",
18-
".png",
19-
".jpg",
20-
".jpeg",
21-
".ico"
22-
].reduce((
23-
previousExtension,
24-
currentExtension
25-
) => ({
26-
...previousExtension,
27-
[currentExtension]: 'copy'
28-
}), {})
29-
3014
const copyConfig : esbuild.BuildOptions = {
3115
allowOverwrite: true,
3216
logLevel: args.logLevel ?? 'info',
3317
color: true,
34-
loader: loaders,
3518
outdir: './dist',
3619
outbase: './src/client',
3720
entryPoints: [
3821
'./src/client/**/index.html',
39-
'./src/client/**/index.css',
4022
'./src/client/**/assets/*',
4123
'./src/client/static/**/*'
4224
],
25+
plugins: [
26+
esbuildPluginCopy()
27+
]
4328
}
4429

4530
const filesConfig : esbuild.BuildOptions = {
@@ -59,7 +44,8 @@ const filesConfig : esbuild.BuildOptions = {
5944
outdir: './dist',
6045
outbase: './src/client',
6146
entryPoints: [
62-
'./src/client/index.tsx'
47+
'./src/client/index.tsx',
48+
'./src/client/index.css'
6349
],
6450
supported: {
6551
'import-attributes': true,

build/plugins/copy.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type * as esbuild from '@esbuild';
2+
3+
4+
export function copy() : esbuild.Plugin {
5+
return ({
6+
name: 'copy',
7+
setup(build) : void {
8+
build.onLoad({filter: /.*/}, (args) => {
9+
return {
10+
contents: Deno.readTextFileSync(args.path),
11+
loader: 'copy',
12+
}
13+
})
14+
}
15+
})
16+
}

0 commit comments

Comments
 (0)