Skip to content

Commit 361ff6c

Browse files
committed
feat: add runtime option for the handlebars preprocessor
1 parent 85ce3a5 commit 361ff6c

16 files changed

Lines changed: 122 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
## 3.16.0 (2024-07-23)
4+
5+
- feat: add `runtime` option for the `handlebars` preprocessor
6+
- test: add test for the `runtime` option
7+
- docs: update readme
8+
39
## 3.15.1 (2024-07-07)
410

511
- fix: resolving source file in a tag attribute when another attribute contains the `>` char, e.g.:

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
## HTML template as entry point
1717

1818
The **HTML Bundler** generates static HTML or [template function](#template-in-js) from [various templates](#template-engine) containing source files of scripts, styles, images, fonts and other resources, similar to how it works in [Vite](https://vitejs.dev/guide/#index-html-and-project-root).
19-
This plugin allows using a template file as an [entry point](#option-entry).
19+
This plugin looks at the template files in [entry](#option-entry) to start building the bundle.
20+
The source files of dependencies (scripts, styles, etc.) can be defined directly in the template.
2021

2122
The plugin resolves source files of assets in templates and replaces them with correct output URLs in the generated HTML.
2223
The resolved assets will be processed via Webpack plugins/loaders and placed into the output directory.
2324
You can use a relative path or Webpack alias to a source file.
2425

2526
A template imported in JS will be compiled into [template function](#template-in-js). You can use the **template function** in JS to render the template with variables in runtime on the client-side in the browser.
2627

27-
2828
This plugin is an **advanced replacement** of `html-webpack-plugin` and many other [plugins and loaders](#list-of-plugins).
2929

3030
<!--
@@ -169,6 +169,10 @@ Thank you to all our sponsors and patrons!
169169
<img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/43568167/0ef77126597d460c9505bdd0aea2eea9/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=7izh1FZTToAqf4Qks3Qrk8YcNbGymF-sBi0hkK_aJO8%3D" width="50" title="Raymond Ackloo" alt="patron" style="max-width: 100%;">
170170
<p>Raymond<br>Ackloo</p>
171171
</a></td>
172+
<td style="border: 0"><a href="https://github.com/chkpnt">
173+
<img src="https://avatars.githubusercontent.com/u/1956979?s=50&amp;v=4" width="50" title="Gregor Dschung" alt="Gregor Dschung" style="max-width: 100%;">
174+
<p>Gregor<br>Dschung</p>
175+
</a></td>
172176
</tr>
173177
</table>
174178

@@ -3502,6 +3506,12 @@ Include the partials in the `src/views/page/home.html` template with the `includ
35023506
35033507
The `include` helper automatically resolves `.html` and `.hbs` extensions, it can be omitted.
35043508
3509+
**The `runtime` option**
3510+
3511+
The path to the handlebars runtime library. The path can be absolute or relative to `node_modules` directory.
3512+
Defaults runtime file is `handlebars/runtime`.
3513+
This options is used only by import templates in JavaScript, in [compile mode](#compile-mode).
3514+
35053515
**The `partials` option**
35063516
35073517
Type: `Array<string>|Object` Default: `[]`
@@ -4476,6 +4486,8 @@ _./partials/star-button.html_
44764486
</button>
44774487
```
44784488

4489+
<a id="compile-mode" name="compile-mode"></a>
4490+
44794491
### Compile mode
44804492

44814493
The `compile` is the default mode for the template imported in JavaScript file.
@@ -4521,7 +4533,7 @@ _./partials/people.ejs_
45214533
`include` is supported
45224534
- [ejs](#loader-option-preprocessor-options-ejs) - generates a fast smallest pure template function w/o runtime (**recommended** for use on client-side)\
45234535
`include` is supported
4524-
- [handlebars](#loader-option-preprocessor-options-handlebars) - generates a precompiled template with runtime (~28KB)\
4536+
- [handlebars](#loader-option-preprocessor-options-handlebars) - generates a precompiled template with runtime (~18KB)\
45254537
`include` is NOT supported (yet)
45264538
- [nunjucks](#loader-option-preprocessor-options-nunjucks) - generates a precompiled template with runtime (~41KB)\
45274539
`include` is supported

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "3.15.1",
3+
"version": "3.16.0",
44
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
55
"keywords": [
66
"html",

src/Loader/Preprocessors/Handlebars/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path');
22
const Dependency = require('../../Dependency');
3-
const { escapeSequences, stringifyJSON } = require('../../Utils');
3+
const { stringifyJSON } = require('../../Utils');
44
const { loadModule, readDirRecursiveSync } = require('../../../Common/FileUtils');
55
const { isWin, pathToPosix } = require('../../../Common/Helpers');
66

@@ -11,6 +11,10 @@ const preprocessor = (loaderContext, options) => {
1111
const extensions = ['.html', '.hbs', '.handlebars'];
1212
const includeFiles = [/\.(html|hbs|handlebars)$/i];
1313
const root = options?.root || rootContext;
14+
//const runtime = options?.runtime || 'handlebars/dist/handlebars.runtime.min';
15+
const runtime = options?.runtime || 'handlebars/runtime';
16+
// fix windows-like path
17+
const runtimeFile = require.resolve(runtime).replace(/\\/g, '/');
1418
let views = options?.views || rootContext;
1519
let helpers = {};
1620
let partials = {};
@@ -212,8 +216,6 @@ const preprocessor = (loaderContext, options) => {
212216
* @return {string} The exported template function.
213217
*/
214218
export(precompiledTemplate, { data }) {
215-
// fix windows-like path
216-
const runtimeFile = require.resolve('handlebars/dist/handlebars.runtime.min').replace(/\\/g, '/');
217219
const exportFunctionName = 'templateFn';
218220
const exportCode = 'module.exports=';
219221

test/cases/js-tmpl-hbs-compile/expected/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/loader-option-preprocessor-handlebars-runtime/expected/app.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.6 KB
Loading
1.66 KB
Loading
1.72 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My Title</title>
5+
<!-- load the rendered template in JS and add it into HTML in runtime -->
6+
<script src="app.js" defer="defer"></script>
7+
</head>
8+
<body>
9+
<div id="main"></div>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)