Skip to content

Commit

Permalink
chore: updated dependencies
Browse files Browse the repository at this point in the history
test: added basic runtime test

doc: provide an example for plugin options
  • Loading branch information
Inqnuam committed Jan 30, 2024
1 parent 3dae7e2 commit 1b98cf9
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 157 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
registry-url: "https://registry.npmjs.org"
- run: yarn install
- run: yarn build
- run: yarn test
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
/dist
dist
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.github
node_modules
node_modules
test
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"printWidth": 180,
"bracketSpacing": true,
"endOfLine": "lf"
}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ You can also set additionalHelpers and precompileOptions:

```js
const hbsOptions = {
additionalHelpers: {},
additionalPartials: {},
precompileOptions: {}
}
additionalHelpers: {},
additionalPartials: {
childTemplate: "./child.hbs",
},
precompileOptions: {},
};

// usual esbuild config
{
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esbuild-plugin-handlebars",
"description": "an esbuild plugin to handle ... handlebars!",
"version": "1.0.2",
"version": "1.0.3",
"main": "dist/index.js",
"author": "Inqnuam",
"license": "MIT",
Expand All @@ -11,13 +11,18 @@
"url": "git+https://github.com/inqnuam/esbuild-plugin-handlebars.git"
},
"dependencies": {
"handlebars": "^4.7.7"
"handlebars": "^4.7.8"
},
"devDependencies": {
"@types/node": "^18.7.19",
"esbuild": "^0.16.5"
},
"scripts": {
"build": "node build.js"
}
"build": "node build.js",
"test": "node test/test.js"
},
"keywords": [
"handlebars",
"esbuild"
]
}
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { stat, readFile } from "fs/promises";

let foundHelpers: string[] = [];
const fileCache = new Map();

// @ts-ignore
class ESBuildHandlebarsJSCompiler extends handlebars.JavaScriptCompiler {
constructor() {
Expand All @@ -17,11 +18,12 @@ class ESBuildHandlebarsJSCompiler extends handlebars.JavaScriptCompiler {
return super.nameLookup(parent, name, type);
}
}
function hbs(options: { additionalHelpers: any; additionalPartials: any; precompileOptions: any } = { additionalHelpers: {}, additionalPartials: {}, precompileOptions: {} }) {
const onloadOpt: OnLoadOptions = {
filter: /\.(hbs|handlebars)$/i,
};

const onloadOpt: OnLoadOptions = {
filter: /\.(hbs|handlebars)$/i,
};

function hbs(options: { additionalHelpers: any; additionalPartials: any; precompileOptions: any } = { additionalHelpers: {}, additionalPartials: {}, precompileOptions: {} }) {
const { additionalHelpers = {}, additionalPartials = {}, precompileOptions = {} } = options;
return {
name: "handlebars",
Expand Down Expand Up @@ -51,22 +53,32 @@ function hbs(options: { additionalHelpers: any; additionalPartials: any; precomp
fileCache.delete(filename);
}
}

const source = await readFile(filename, "utf-8");
const knownHelpers = Object.keys(additionalHelpers).reduce((prev: any, helper: string) => {
prev[helper] = true;
return prev;
}, {});

// Compile options
const compileOptions = {
...precompileOptions,
knownHelpersOnly: true,
knownHelpers,
};

try {
foundHelpers = [];
const template = hb.precompile(source, compileOptions);
const foundAndMatchedHelpers = foundHelpers.filter((helper) => additionalHelpers[helper] !== undefined);
const contents = ["import * as Handlebars from 'handlebars/runtime';", ...foundAndMatchedHelpers.map((helper) => `import ${helper} from '${additionalHelpers[helper]}';`), ...Object.entries(additionalPartials).map(([name, path]) => `import ${name} from '${path}';`), `Handlebars.registerHelper({${foundAndMatchedHelpers.join()}});`, `Handlebars.registerPartial({${Object.keys(additionalPartials).join()}});`, `export default Handlebars.template(${template});`].join("\n");
const contents = [
"import * as Handlebars from 'handlebars/runtime';",
...foundAndMatchedHelpers.map((helper) => `import ${helper} from '${additionalHelpers[helper]}';`),
...Object.entries(additionalPartials).map(([name, path]) => `import ${name} from '${path}';`),
`Handlebars.registerHelper({${foundAndMatchedHelpers.join()}});`,
`Handlebars.registerPartial({${Object.keys(additionalPartials).join()}});`,
`export default Handlebars.template(${template});`,
].join("\n");
return { contents };
} catch (err: any) {
const esBuildError = { text: err.message };
Expand Down
8 changes: 8 additions & 0 deletions test/child.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<p>This is content from the child template.</p>
<ul>
{{#each items}}
<li>{{this}}</li>
{{/each}}
</ul>
</div>
3 changes: 3 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import template from "./template.hbs";

module.exports = template;
9 changes: 9 additions & 0 deletions test/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>

<div class="content">
{{> childTemplate}}
</div>
37 changes: 37 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// basic runtime test

const esbuild = require("esbuild");
const handlebarsPlugin = require("../dist");

const esBuildConfig = {
bundle: true,
minify: true,
platform: "node",
target: "es6",
outdir: "./test/dist",
entryPoints: ["./test/index.ts"],
plugins: [
handlebarsPlugin({
additionalPartials: {
childTemplate: "./child.hbs",
},
}),
],
};

const hbsProp = {
people: ["Yehuda Katz", "Alan Johnson", "Charles Jolley"],
items: ["item 1", "item 2", "item 3"],
};

esbuild
.build(esBuildConfig)
.then(() => {
const compiledTemplate = require("./dist/index.js");

console.log(compiledTemplate(hbsProp));
})
.catch((e) => {
console.error(e);
process.exit(1);
});
5 changes: 5 additions & 0 deletions test/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "*.hbs" {
const template: (...args: any[]) => string;

export default template;
}
Loading

0 comments on commit 1b98cf9

Please sign in to comment.