Skip to content

Commit e6776db

Browse files
committed
Add HTML script tag plugin to esbuild build
Introduces a custom esbuild plugin to rewrite script src extensions in HTML files during the build process. Updates build configuration to use the new plugin and changes index.html to reference index.tsx directly, allowing the plugin to handle extension replacement.
1 parent 6ed52da commit e6776db

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

build/esbuild.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/// <reference lib="deno.ns" />
22
import * as esbuild from '@esbuild';
3-
import { denoPlugin as esbuildPluginDeno } from "@deno/esbuild-plugin";
43
import { bold, green, magenta } from '@std/fmt/colors';
54
import { parseArgs } from '@std/cli/parse-args';
65
import { copy as esbuildPluginCopy } from './plugins/copy.ts';
6+
import { denoPlugin as esbuildPluginDeno } from "@deno/esbuild-plugin";
7+
import { htmlScriptTags as esbuildPluginHtmlScriptTags } from './plugins/html_script_tags.ts';
78

89
const args = parseArgs<{
910
watch: boolean | undefined,
@@ -18,7 +19,6 @@ const copyConfig : esbuild.BuildOptions = {
1819
outdir: './dist',
1920
outbase: './src/client',
2021
entryPoints: [
21-
'./src/client/**/index.html',
2222
'./src/client/**/assets/*',
2323
'./src/client/static/**/*'
2424
],
@@ -27,7 +27,7 @@ const copyConfig : esbuild.BuildOptions = {
2727
]
2828
}
2929

30-
const filesConfig : esbuild.BuildOptions = {
30+
const buildConfig : esbuild.BuildOptions = {
3131
allowOverwrite: true,
3232
logLevel: args.logLevel ?? 'info',
3333
legalComments: args.develop ? 'inline' : 'none',
@@ -44,6 +44,7 @@ const filesConfig : esbuild.BuildOptions = {
4444
outdir: './dist',
4545
outbase: './src/client',
4646
entryPoints: [
47+
'./src/client/**/index.html',
4748
'./src/client/index.tsx',
4849
'./src/client/index.css'
4950
],
@@ -52,6 +53,7 @@ const filesConfig : esbuild.BuildOptions = {
5253
'nesting': true
5354
},
5455
plugins: [
56+
esbuildPluginHtmlScriptTags(),
5557
esbuildPluginDeno({
5658
preserveJsx: true,
5759
debug: args.develop ?? false
@@ -65,11 +67,11 @@ const timestampNow = Date.now();
6567

6668
if (args.watch) {
6769
esbuild.context(copyConfig).then((context) => context.watch());
68-
esbuild.context(filesConfig).then((context) => context.watch());
70+
esbuild.context(buildConfig).then((context) => context.watch());
6971
} else {
7072
Promise.all([
7173
esbuild.build(copyConfig),
72-
esbuild.build(filesConfig)
74+
esbuild.build(buildConfig)
7375
]).then(() => {
7476
esbuild.stop();
7577
console.log(green(`esbuild ${esbuild.version} finished build in ${(Date.now() - timestampNow).toString()}ms.`));

build/plugins/html_script_tags.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type * as esbuild from '@esbuild';
2+
3+
4+
const defaultExtensionReplacements = {
5+
'.tsx': '.js',
6+
'.ts': '.js'
7+
}
8+
9+
export function htmlScriptTags(options?: {
10+
pattern?: RegExp,
11+
extension?: {[extension: string] : string}
12+
}) : esbuild.Plugin {
13+
return ({
14+
name: 'html-script-tags',
15+
setup(build) : void {
16+
build.onLoad({filter: /\.html$/}, (args) => {
17+
return {
18+
contents: Deno.readTextFileSync(args.path).replace(
19+
options?.pattern ?? /(?<=<script.*src=")(.*)(?=".*>)/gm,
20+
(_, scriptSrcPath : string) => {
21+
let path = scriptSrcPath;
22+
let replaced = false;
23+
Object.entries(options?.extension ?? defaultExtensionReplacements).forEach(([extension, replacement]) => {
24+
if (scriptSrcPath.match(extension) && !replaced) {
25+
path = scriptSrcPath.replace(extension, replacement)
26+
replaced = true
27+
}
28+
})
29+
30+
return path;
31+
}
32+
),
33+
loader: 'copy',
34+
}
35+
})
36+
}
37+
})
38+
}

src/client/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>Home</title>
77
<link rel="shortcut icon" href="/static/favicon.svg" type="image/x-icon" />
88
<link rel="stylesheet" href="./index.css">
9-
<script type="module" src="./index.js" defer></script>
9+
<script type="module" src="./index.tsx" defer></script>
1010
</head>
1111
<body></body>
1212
</html>

0 commit comments

Comments
 (0)