Skip to content

Commit cc3976e

Browse files
committed
feat: add handlebar helper prefixModuleScript
1 parent 93771ce commit cc3976e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Usage in templates:
4040
- {{ withVersion url }} -- appends `'?v=' + version` to the passed string
4141
- {{ extend name options }} -- calls the function options.fn with content of block **name** as param and adds it to named block
4242
- {{ prefixScript url name }} -- add a script tag with version set to script block **name**
43+
- {{ prefixModuleScript url name }} -- add a script tag with type module and with version set to script block **name**
4344
- {{ prefixStyle url name media }} -- add a style tag for named media type with version set to style block **name**
4445
- {{ render name }} -- used by a layout to render script and style blocks in appropriate places
4546

lib/handlebars/helpers/headerContent.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ module.exports = function headerContent(options) {
8787
_blocks[blockName].push(`<script src="${url}"></script>`)
8888
})
8989

90+
/** @function prefixModuleScript
91+
*
92+
* @param {string} url -- local path to script excluding proxyPrefixPath
93+
* @param {string} blockName -- (optional) name of script block to include with, defaults to 'scripts'
94+
*
95+
* @return output with {{ render blockName }}
96+
*
97+
* @example
98+
* {{prefixModuleScript '/path/to/script.js' 'scripts'}}
99+
*/
100+
Handlebars.registerHelper('prefixModuleScript', function prefixModuleScript(urlIn, blockNameIn) {
101+
// Check params and give useful error message since stack traces aren't very useable in layouts
102+
if (typeof urlIn !== 'string') {
103+
throw new Error('[prefixModuleScript] helper requires first parameter (url) to be a string. Got: ' + urlIn)
104+
}
105+
106+
const blockName = typeof blockNameIn === 'string' ? blockNameIn : 'scripts'
107+
_blocks[blockName] = _blocks[blockName] || []
108+
109+
const url = `${proxyPrefixPathUri}${urlIn}?v=${encodeURIComponent(version)}`
110+
111+
_blocks[blockName].push(`<script type="module" src="${url}"></script>`)
112+
})
113+
90114
/** @function prefixStyle
91115
*
92116
* @param {string} url -- local path to script excluding proxyPrefixPath

0 commit comments

Comments
 (0)