Skip to content

Commit

Permalink
Merge pull request #125 from AddSearch/sc-10506/duplicated-library-sc…
Browse files Browse the repository at this point in the history
…ript-load-breaks-search

[sc-10506] export Handlebars_runtime as module's prop instead of wind…
  • Loading branch information
haoAddsearch authored Sep 5, 2024
2 parents 3ff94b3 + b38324e commit b3b0238
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,13 +928,15 @@ Or precompile the whole directory
```js
handlebars your-dir/all-templates/> output-dir/all-precompiled-templates-srp.js -c handlebars/runtime
```
`-c` flag creates a `require` statement for Handlebars-runtime in your precompiled template file.
`-c` flag creates a `require` statement for Handlebars-runtime in your precompiled template file. Please note that -c will build your templates into a CommonJS module, which is not supported in the browser.

Use the precompiled template inside a component, for example:
```js
// Handlebars are exposed globally when loading Search UI Library
// Handlebars are exported within Search UI Library's module as Handlebars_runtime
// You can also install handlebars runtime package and import it
// Below code will use your precompiled template of the file name "searchfield.handlebars"

var Handlebars = AddSearchUI.Handlebars_runtime;
var precompiledTemplates = Handlebars.templates;
searchui.searchField({
containerId: 'searchfield-container',
Expand Down
2 changes: 1 addition & 1 deletion dist/addsearch-search-ui.min.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/addsearch-search-ui.min.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ module.exports.LOAD_MORE_TYPE = require("./src/components/loadmore/index.js").LO
module.exports.RECOMMENDATION_TYPE = require("./src/components/recommendations/index.js").RECOMMENDATION_TYPE;
module.exports.RANGE_FACETS_TYPE = require("./src/components/rangefacets/index.js").RANGE_FACETS_TYPE;

if (typeof window !== 'undefined' && typeof window.Handlebars === 'undefined') {
window.Handlebars = require('handlebars/runtime');
}
module.exports.Handlebars_runtime = require('handlebars/runtime');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "addsearch-search-ui",
"version": "0.8.13",
"version": "0.8.14",
"description": "JavaScript library to develop Search UIs for the web",
"main": "./dist/addsearch-search-ui.min.js",
"jsdelivr": "./dist/addsearch-search-ui.min.js",
Expand Down
6 changes: 2 additions & 4 deletions src/util/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ export function registerDefaultHelpers() {

export function registerHelper(helperName, helperFunction) {
handlebars.registerHelper(helperName, helperFunction);
const HandlebarsRunTime = (typeof window !== 'undefined' && window.Handlebars) ? window.Handlebars : HandlebarsRuntime;
HandlebarsRunTime.registerHelper(helperName, helperFunction);
HandlebarsRuntime.registerHelper(helperName, helperFunction);
}
export function registerPartial(partialName, partialTemplate) {
handlebars.registerPartial(partialName, partialTemplate);
const HandlebarsRunTime = (typeof window !== 'undefined' && window.Handlebars) ? window.Handlebars : HandlebarsRuntime;
HandlebarsRunTime.registerPartial(partialName, partialTemplate);
HandlebarsRuntime.registerPartial(partialName, partialTemplate);
}

0 comments on commit b3b0238

Please sign in to comment.