diff --git a/README.md b/README.md index 3019cba..2b0047d 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ registerHeaderContentHelper({ - {{ extend name options }} -- calls the function options.fn with content of block **name** as param and adds it to named block - {{ prefixScript url name }} -- add a script tag with version set to script block **name** +- {{ prefixModuleScript url name }} -- add a script tag with type module and with version set to script block **name** - {{ prefixStyle url name media }} -- add a style tag for named media type with version set to style block **name** - {{ render name }} -- used by a layout to render script and style blocks in appropriate places - {{ withVersion url }} -- appends `'?v=' + version` to the passed string diff --git a/lib/handlebars/helpers/headerContent.js b/lib/handlebars/helpers/headerContent.js index 64fe295..35d5b7d 100644 --- a/lib/handlebars/helpers/headerContent.js +++ b/lib/handlebars/helpers/headerContent.js @@ -87,6 +87,30 @@ module.exports = function headerContent(options) { _blocks[blockName].push(``) }) + /** @function prefixModuleScript + * + * @param {string} url -- local path to script excluding proxyPrefixPath + * @param {string} blockName -- (optional) name of script block to include with, defaults to 'scripts' + * + * @return output with {{ render blockName }} + * + * @example + * {{prefixModuleScript '/path/to/script.js' 'scripts'}} + */ + Handlebars.registerHelper('prefixModuleScript', function prefixModuleScript(urlIn, blockNameIn) { + // Check params and give useful error message since stack traces aren't very useable in layouts + if (typeof urlIn !== 'string') { + throw new Error('[prefixModuleScript] helper requires first parameter (url) to be a string. Got: ' + urlIn) + } + + const blockName = typeof blockNameIn === 'string' ? blockNameIn : 'scripts' + _blocks[blockName] = _blocks[blockName] || [] + + const url = `${proxyPrefixPathUri}${urlIn}?v=${encodeURIComponent(version)}` + + _blocks[blockName].push(``) + }) + /** @function prefixStyle * * @param {string} url -- local path to script excluding proxyPrefixPath diff --git a/package-lock.json b/package-lock.json index 75bfa14..c49aa49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kth/kth-node-web-common", - "version": "9.0.2", + "version": "9.0.3-0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@kth/kth-node-web-common", - "version": "9.0.2", + "version": "9.0.3-0", "license": "MIT", "dependencies": { "@kth/cortina-block": "^5.1.1", diff --git a/package.json b/package.json index 26f7825..d7b6be0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kth/kth-node-web-common", - "version": "9.0.2", + "version": "9.0.3-0", "description": "Common components for node-web projects", "scripts": { "test": "jest",