Skip to content

Commit

Permalink
Merge pull request #192 from chialab/html-preprocess
Browse files Browse the repository at this point in the history
Add `extensions` and `preprocess` options to HTML plugin.
  • Loading branch information
edoardocavazza authored Nov 4, 2024
2 parents 8e82ec2 + f5fcd89 commit 3d55727
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 149 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-avocados-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/esbuild-plugin-html': patch
---

Add `extensions` and `preprocess` options to HTML plugin.
38 changes: 38 additions & 0 deletions docs/guide/esbuild-plugin-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ It can be `link` or `script` (default).

The options for the minification process. If the `htmlnano` module is installed, the plugin will minify the HTML output.

#### `extensions`

An array of extensions to consider as HTML entrypoints.

#### `preprocess(html: string, path: string): string | Promise<string>`

A function to preprocess the HTML content before parsing it.

## How it works

**Esbuild Plugin HTML** instructs esbuild to load a HTML file as entrypoint. It parses the HTML and runs esbuild on scripts, styles, assets and icons.
Expand Down Expand Up @@ -279,3 +287,33 @@ await esbuild.build({
],
});
```

## Preprocess

The plugin accepts a `preprocess` function that can be used to modify the HTML content before parsing it.

If you are using a specific extension for template files, you should also add it to the `extensions` option.

```ts
import htmlPlugin from '@chialab/esbuild-plugin-html';
import esbuild from 'esbuild';

await esbuild.build({
entryPoints: ['src/index.hbs'],
outdir: 'public',
assetNames: 'assets/[name]-[hash]',
chunkNames: '[ext]/[name]-[hash]',
plugins: [
htmlPlugin({
extensions: ['.html', '.hbs'],
preprocess: async (html, path) => {
const { compile } = await import('handlebars');
const template = compile(contents);
return template({});
},
}),
],
});
```

The plugin will rename the output file to the `.html` extension.
Loading

0 comments on commit 3d55727

Please sign in to comment.