From d71644f458d40ea2f273731f3ba21ea81211a1f3 Mon Sep 17 00:00:00 2001 From: biodiscus Date: Sun, 8 Dec 2024 20:24:44 +0100 Subject: [PATCH] feat: add support for handlebars helpers in compile mode --- CHANGELOG.md | 4 ++ package.json | 2 +- src/Loader/Preprocessors/Handlebars/index.js | 18 +++++- src/Loader/Utils.js | 23 +++++-- .../js-tmpl-hbs-compile-helpers/.editorconfig | 7 +++ .../expected/app.js | 1 + .../expected/index.html | 11 ++++ .../js-tmpl-hbs-compile-helpers/src/app.hbs | 2 + .../js-tmpl-hbs-compile-helpers/src/app.js | 9 +++ .../js-tmpl-hbs-compile-helpers/src/index.hbs | 11 ++++ .../src/partials/footer.html | 1 + .../webpack.config.js | 52 ++++++++++++++++ .../expected/app.js | 2 +- .../expected/index.html | 4 +- .../js-tmpl-hbs-compile-partials/src/app.js | 2 +- .../src/index.hbs | 4 +- test/preprocessor.test.js | 1 + test/unit.test.js | 61 ++++++++++++++++--- 18 files changed, 195 insertions(+), 20 deletions(-) create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/.editorconfig create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/app.js create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/index.html create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.hbs create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.js create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/index.hbs create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/partials/footer.html create mode 100644 test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/webpack.config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce58f92..af55188e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 4.10.0 (2024-12-08) + +- feat: add support for handlebars helpers in compile mode + ## 4.9.2 (2024-12-07) - fix: Error Cannot find module 'nunjucks', introduced in `4.9.1` diff --git a/package.json b/package.json index 4138eccf..163b1750 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-bundler-webpack-plugin", - "version": "4.9.2", + "version": "4.10.0", "description": "Processing HTML templates and bundling assets. Build-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.", "keywords": [ "html", diff --git a/src/Loader/Preprocessors/Handlebars/index.js b/src/Loader/Preprocessors/Handlebars/index.js index 5abe47dd..ab788431 100644 --- a/src/Loader/Preprocessors/Handlebars/index.js +++ b/src/Loader/Preprocessors/Handlebars/index.js @@ -1,5 +1,5 @@ const path = require('path'); -const { stringifyJSON } = require('../../Utils'); +const { stringifyJSON, stringifyFn } = require('../../Utils'); const { loadModule, readDirRecursiveSync } = require('../../../Common/FileUtils'); const { isWin, pathToPosix } = require('../../../Common/Helpers'); const LoaderFactory = require('../../LoaderFactory'); @@ -250,11 +250,25 @@ const preprocessor = (loaderContext, options) => { export(precompiledTemplate, { data }) { const exportFunctionName = 'templateFn'; const exportCode = 'module.exports='; + let precompiledHelpers = ''; + + if (options.helpers || Array.isArray(options.helpers)) { + const helpers = getHelpers(options.helpers); + + for (let name in helpers) { + let helper = helpers[name]; + let source = stringifyFn(helper); + + precompiledHelpers += ` + Handlebars.registerHelper('${name}', ${source});`; + } + } return ` var Handlebars = require('${runtimeFile}'); var data = ${stringifyJSON(data)}; - ${precompiledTemplate}; + ${precompiledHelpers} + ${precompiledTemplate} var ${exportFunctionName} = (context) => { var template = (Handlebars['default'] || Handlebars).template(precompiledTemplate); return template(Object.assign({}, data, context)); diff --git a/src/Loader/Utils.js b/src/Loader/Utils.js index 176f7d3e..5e1aa0fc 100644 --- a/src/Loader/Utils.js +++ b/src/Loader/Utils.js @@ -182,11 +182,11 @@ const stringifyJSON = (data) => { if (typeof value === 'function') { value = value.toString().replace(/\n/g, ''); - // transform `{ fn() {} }` to `{"fn":()=>{}}` + // transform `{ fn() {} }` to `{"fn":function(){}}` const keySize = key.length; - if (key === value.slice(0, keySize)) { - const pos = value.indexOf(')', keySize + 1) + 1; - value = value.slice(keySize, pos) + '=>' + value.slice(pos).trimStart(); + const pos = value.indexOf('('); + if (pos > 0 && value.slice(0, pos).trim() !== 'function') { + value = 'function' + value.slice(keySize); } value = quoteMark + value + quoteMark; @@ -202,6 +202,20 @@ const stringifyJSON = (data) => { : json || '{}'; }; +const stringifyFn = (fn) => { + let value = fn.toString().replace(/\n/g, ''); + let isArrowFunction = value.indexOf('=>', 1) > 0; + + if (!isArrowFunction) { + const pos = value.indexOf('('); + if (pos > 0 && value.slice(0, pos).trim() !== 'function') { + value = 'function' + value.slice(pos); + } + } + + return value; +}; + module.exports = { baseUri, urlPathPrefix, @@ -220,4 +234,5 @@ module.exports = { escapeSequences, escapeCodesForJSON, stringifyJSON, + stringifyFn, }; diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/.editorconfig b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/.editorconfig new file mode 100644 index 00000000..3280e6bd --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/.editorconfig @@ -0,0 +1,7 @@ +root = true + +[*.html] +insert_final_newline = true + +[*.hbs] +insert_final_newline = true diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/app.js b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/app.js new file mode 100644 index 00000000..cf42a5a2 --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/app.js @@ -0,0 +1 @@ +(()=>{var e={916:(e,t,r)=>{"use strict";function o(e){return e&&e.__esModule?e:{default:e}}function n(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}t.__esModule=!0;var a=n(r(387)),l=o(r(16)),i=o(r(564)),s=n(r(456)),u=n(r(433)),c=o(r(157));function p(){var e=new a.HandlebarsEnvironment;return s.extend(e,a),e.SafeString=l.default,e.Exception=i.default,e.Utils=s,e.escapeExpression=s.escapeExpression,e.VM=u,e.template=function(t){return u.template(t,e)},e}var d=p();d.create=p,c.default(d),d.default=d,t.default=d,e.exports=t.default},387:(e,t,r)=>{"use strict";function o(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.HandlebarsEnvironment=p;var n=r(456),a=o(r(564)),l=r(880),i=r(103),s=o(r(677)),u=r(352);t.VERSION="4.7.8",t.COMPILER_REVISION=8,t.LAST_COMPATIBLE_COMPILER_REVISION=7,t.REVISION_CHANGES={1:"<= 1.0.rc.2",2:"== 1.0.0-rc.3",3:"== 1.0.0-rc.4",4:"== 1.x.x",5:"== 2.0.0-alpha.x",6:">= 2.0.0-beta.1",7:">= 4.0.0 <4.3.0",8:">= 4.3.0"};var c="[object Object]";function p(e,t,r){this.helpers=e||{},this.partials=t||{},this.decorators=r||{},l.registerDefaultHelpers(this),i.registerDefaultDecorators(this)}p.prototype={constructor:p,logger:s.default,log:s.default.log,registerHelper:function(e,t){if(n.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple helpers");n.extend(this.helpers,e)}else this.helpers[e]=t},unregisterHelper:function(e){delete this.helpers[e]},registerPartial:function(e,t){if(n.toString.call(e)===c)n.extend(this.partials,e);else{if(void 0===t)throw new a.default('Attempting to register a partial called "'+e+'" as undefined');this.partials[e]=t}},unregisterPartial:function(e){delete this.partials[e]},registerDecorator:function(e,t){if(n.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple decorators");n.extend(this.decorators,e)}else this.decorators[e]=t},unregisterDecorator:function(e){delete this.decorators[e]},resetLoggedPropertyAccesses:function(){u.resetLoggedProperties()}};var d=s.default.log;t.log=d,t.createFrame=n.createFrame,t.logger=s.default},103:(e,t,r)=>{"use strict";t.__esModule=!0,t.registerDefaultDecorators=function(e){n.default(e)};var o,n=(o=r(731))&&o.__esModule?o:{default:o}},731:(e,t,r)=>{"use strict";t.__esModule=!0;var o=r(456);t.default=function(e){e.registerDecorator("inline",(function(e,t,r,n){var a=e;return t.partials||(t.partials={},a=function(n,a){var l=r.partials;r.partials=o.extend({},l,t.partials);var i=e(n,a);return r.partials=l,i}),t.partials[n.args[0]]=n.fn,a}))},e.exports=t.default},564:(e,t)=>{"use strict";t.__esModule=!0;var r=["description","fileName","lineNumber","endLineNumber","message","name","number","stack"];function o(e,t){var n=t&&t.loc,a=void 0,l=void 0,i=void 0,s=void 0;n&&(a=n.start.line,l=n.end.line,i=n.start.column,s=n.end.column,e+=" - "+a+":"+i);for(var u=Error.prototype.constructor.call(this,e),c=0;c{"use strict";function o(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.registerDefaultHelpers=function(e){n.default(e),a.default(e),l.default(e),i.default(e),s.default(e),u.default(e),c.default(e)},t.moveHelperToHooks=function(e,t,r){e.helpers[t]&&(e.hooks[t]=e.helpers[t],r||delete e.helpers[t])};var n=o(r(582)),a=o(r(646)),l=o(r(170)),i=o(r(264)),s=o(r(233)),u=o(r(205)),c=o(r(331))},582:(e,t,r)=>{"use strict";t.__esModule=!0;var o=r(456);t.default=function(e){e.registerHelper("blockHelperMissing",(function(t,r){var n=r.inverse,a=r.fn;if(!0===t)return a(this);if(!1===t||null==t)return n(this);if(o.isArray(t))return t.length>0?(r.ids&&(r.ids=[r.name]),e.helpers.each(t,r)):n(this);if(r.data&&r.ids){var l=o.createFrame(r.data);l.contextPath=o.appendContextPath(r.data.contextPath,r.name),r={data:l}}return a(t,r)}))},e.exports=t.default},646:(e,t,r)=>{"use strict";t.__esModule=!0;var o,n=r(456),a=(o=r(564))&&o.__esModule?o:{default:o};t.default=function(e){e.registerHelper("each",(function(e,t){if(!t)throw new a.default("Must pass iterator to #each");var r,o=t.fn,l=t.inverse,i=0,s="",u=void 0,c=void 0;function p(t,r,a){u&&(u.key=t,u.index=r,u.first=0===r,u.last=!!a,c&&(u.contextPath=c+t)),s+=o(e[t],{data:u,blockParams:n.blockParams([e[t],t],[c+t,null])})}if(t.data&&t.ids&&(c=n.appendContextPath(t.data.contextPath,t.ids[0])+"."),n.isFunction(e)&&(e=e.call(this)),t.data&&(u=n.createFrame(t.data)),e&&"object"==typeof e)if(n.isArray(e))for(var d=e.length;i{"use strict";t.__esModule=!0;var o,n=(o=r(564))&&o.__esModule?o:{default:o};t.default=function(e){e.registerHelper("helperMissing",(function(){if(1!==arguments.length)throw new n.default('Missing helper: "'+arguments[arguments.length-1].name+'"')}))},e.exports=t.default},264:(e,t,r)=>{"use strict";t.__esModule=!0;var o,n=r(456),a=(o=r(564))&&o.__esModule?o:{default:o};t.default=function(e){e.registerHelper("if",(function(e,t){if(2!=arguments.length)throw new a.default("#if requires exactly one argument");return n.isFunction(e)&&(e=e.call(this)),!t.hash.includeZero&&!e||n.isEmpty(e)?t.inverse(this):t.fn(this)})),e.registerHelper("unless",(function(t,r){if(2!=arguments.length)throw new a.default("#unless requires exactly one argument");return e.helpers.if.call(this,t,{fn:r.inverse,inverse:r.fn,hash:r.hash})}))},e.exports=t.default},233:(e,t)=>{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("log",(function(){for(var t=[void 0],r=arguments[arguments.length-1],o=0;o{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("lookup",(function(e,t,r){return e?r.lookupProperty(e,t):e}))},e.exports=t.default},331:(e,t,r)=>{"use strict";t.__esModule=!0;var o,n=r(456),a=(o=r(564))&&o.__esModule?o:{default:o};t.default=function(e){e.registerHelper("with",(function(e,t){if(2!=arguments.length)throw new a.default("#with requires exactly one argument");n.isFunction(e)&&(e=e.call(this));var r=t.fn;if(n.isEmpty(e))return t.inverse(this);var o=t.data;return t.data&&t.ids&&((o=n.createFrame(t.data)).contextPath=n.appendContextPath(t.data.contextPath,t.ids[0])),r(e,{data:o,blockParams:n.blockParams([e],[o&&o.contextPath])})}))},e.exports=t.default},955:(e,t,r)=>{"use strict";t.__esModule=!0,t.createNewLookupObject=function(){for(var e=arguments.length,t=Array(e),r=0;r{"use strict";t.__esModule=!0,t.createProtoAccessControl=function(e){var t=Object.create(null);t.constructor=!1,t.__defineGetter__=!1,t.__defineSetter__=!1,t.__lookupGetter__=!1;var r=Object.create(null);return r.__proto__=!1,{properties:{whitelist:n.createNewLookupObject(r,e.allowedProtoProperties),defaultValue:e.allowProtoPropertiesByDefault},methods:{whitelist:n.createNewLookupObject(t,e.allowedProtoMethods),defaultValue:e.allowProtoMethodsByDefault}}},t.resultIsAllowed=function(e,t,r){return function(e,t){return void 0!==e.whitelist[t]?!0===e.whitelist[t]:void 0!==e.defaultValue?e.defaultValue:(function(e){!0!==l[e]&&(l[e]=!0,a.default.log("error",'Handlebars: Access has been denied to resolve the property "'+e+'" because it is not an "own property" of its parent.\nYou can add a runtime option to disable the check or this warning:\nSee https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details'))}(t),!1)}("function"==typeof e?t.methods:t.properties,r)},t.resetLoggedProperties=function(){Object.keys(l).forEach((function(e){delete l[e]}))};var o,n=r(955),a=(o=r(677))&&o.__esModule?o:{default:o},l=Object.create(null)},423:(e,t)=>{"use strict";t.__esModule=!0,t.wrapHelper=function(e,t){return"function"!=typeof e?e:function(){return arguments[arguments.length-1]=t(arguments[arguments.length-1]),e.apply(this,arguments)}}},677:(e,t,r)=>{"use strict";t.__esModule=!0;var o=r(456),n={methodMap:["debug","info","warn","error"],level:"info",lookupLevel:function(e){if("string"==typeof e){var t=o.indexOf(n.methodMap,e.toLowerCase());e=t>=0?t:parseInt(e,10)}return e},log:function(e){if(e=n.lookupLevel(e),"undefined"!=typeof console&&n.lookupLevel(n.level)<=e){var t=n.methodMap[e];console[t]||(t="log");for(var r=arguments.length,o=Array(r>1?r-1:0),a=1;a{"use strict";t.__esModule=!0,t.default=function(e){"object"!=typeof globalThis&&(Object.prototype.__defineGetter__("__magic__",(function(){return this})),__magic__.globalThis=__magic__,delete Object.prototype.__magic__);var t=globalThis.Handlebars;e.noConflict=function(){return globalThis.Handlebars===e&&(globalThis.Handlebars=t),e}},e.exports=t.default},433:(e,t,r)=>{"use strict";t.__esModule=!0,t.checkRevision=function(e){var t=e&&e[0]||1,r=l.COMPILER_REVISION;if(!(t>=l.LAST_COMPATIBLE_COMPILER_REVISION&&t<=l.COMPILER_REVISION)){if(t{"use strict";function r(e){this.string=e}t.__esModule=!0,r.prototype.toString=r.prototype.toHTML=function(){return""+this.string},t.default=r,e.exports=t.default},456:(e,t)=>{"use strict";t.__esModule=!0,t.extend=l,t.indexOf=function(e,t){for(var r=0,o=e.length;r":">",'"':""","'":"'","`":"`","=":"="},o=/[&<>"'`=]/g,n=/[&<>"'`=]/;function a(e){return r[e]}function l(e){for(var t=1;t{e.exports=r(916).default},768:(e,t,r)=>{var o=r(128),n={};o.registerHelper("bold",(function(e){return new o.SafeString(`${e.fn(this)}`)})),o.partials.footer=o.template({compiler:[8,">= 4.3.0"],main:function(e,t,r,o,n){return"
-= FOOTER =-
\n"},useData:!0});var a={1:function(e,t,r,o,n){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return e.escapeExpression("function"==typeof(a=null!=(a=l(r,"helloName")||(null!=t?l(t,"helloName"):t))?a:e.hooks.helperMissing)?a.call(null!=t?t:e.nullContext||{},{name:"helloName",hash:{},data:n,loc:{start:{line:1,column:18},end:{line:1,column:31}}}):a)},compiler:[8,">= 4.3.0"],main:function(e,t,r,o,n){var a,l,i,s=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]},u="

Hello ";return l=null!=(l=s(r,"bold")||(null!=t?s(t,"bold"):t))?l:e.hooks.helperMissing,i={name:"bold",hash:{},fn:e.program(1,n,0),inverse:e.noop,data:n,loc:{start:{line:1,column:9},end:{line:1,column:40}}},a="function"==typeof l?l.call(null!=t?t:e.nullContext||{},i):l,s(r,"bold")||(a=e.hooks.blockHelperMissing.call(t,a,i)),null!=a&&(u+=a),u+"!

\n"+(null!=(a=e.invokePartial(s(o,"footer"),t,{name:"footer",data:n,helpers:r,partials:o,decorators:e.decorators}))?a:"")},usePartial:!0,useData:!0};e.exports=e=>(o.default||o).template(a)(Object.assign({},n,e))}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var a=t[o]={exports:{}};return e[o](a,a.exports,r),a.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{"use strict";var e=r(768),t=r.n(e);document.getElementById("app").innerHTML=t()({helloName:"World"}),console.log(">> app")})()})(); \ No newline at end of file diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/index.html b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/index.html new file mode 100644 index 00000000..503259ea --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/expected/index.html @@ -0,0 +1,11 @@ + + + + Home + + + + +
+ + diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.hbs b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.hbs new file mode 100644 index 00000000..0a4e5961 --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.hbs @@ -0,0 +1,2 @@ +

Hello {{#bold}}{{helloName}}{{/bold}}!

+{{> 'footer'}} diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.js b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.js new file mode 100644 index 00000000..c94faf90 --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/app.js @@ -0,0 +1,9 @@ +import tmpl from './app.hbs'; + +const locals = { + helloName: 'World', +}; + +document.getElementById('app').innerHTML = tmpl(locals); + +console.log('>> app'); diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/index.hbs b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/index.hbs new file mode 100644 index 00000000..503259ea --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/index.hbs @@ -0,0 +1,11 @@ + + + + Home + + + + +
+ + diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/partials/footer.html b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/partials/footer.html new file mode 100644 index 00000000..e8c9c364 --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/src/partials/footer.html @@ -0,0 +1 @@ +
-= FOOTER =-
diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/webpack.config.js b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/webpack.config.js new file mode 100644 index 00000000..f7f9f579 --- /dev/null +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-helpers/webpack.config.js @@ -0,0 +1,52 @@ +const path = require('path'); +const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin'); +const Handlebars = require('handlebars'); + +module.exports = { + mode: 'production', + + output: { + path: path.join(__dirname, 'dist/'), + }, + + resolve: { + alias: { + '@images': path.join(__dirname, '../../../fixtures/images'), + }, + }, + + plugins: [ + new HtmlBundlerPlugin({ + entry: { + index: './src/index.hbs', + }, + + preprocessor: 'handlebars', + preprocessorOptions: { + //knownHelpersOnly: false, + // define helpers manually + helpers: { + // WARNING: don't use the arrow function with `this` otherwise webpack compile `this` into `void 0` + //bold: (options) => new Handlebars.SafeString(`${options.fn(this)}`), + + bold(options) { + return new Handlebars.SafeString(`${options.fn(this)}`); + }, + }, + partials: ['src/partials'], + }, + }), + ], + + module: { + rules: [ + { + test: /\.(png|svg|jpe?g|webp)$/i, + type: 'asset/resource', + generator: { + filename: 'img/[name].[hash:8][ext]', + }, + }, + ], + }, +}; diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/app.js b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/app.js index 688039db..6fa84f62 100644 --- a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/app.js +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/app.js @@ -1 +1 @@ -(()=>{var e={916:(e,t,r)=>{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}t.__esModule=!0;var a=o(r(387)),l=n(r(16)),i=n(r(564)),s=o(r(456)),u=o(r(433)),c=n(r(157));function p(){var e=new a.HandlebarsEnvironment;return s.extend(e,a),e.SafeString=l.default,e.Exception=i.default,e.Utils=s,e.escapeExpression=s.escapeExpression,e.VM=u,e.template=function(t){return u.template(t,e)},e}var d=p();d.create=p,c.default(d),d.default=d,t.default=d,e.exports=t.default},387:(e,t,r)=>{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.HandlebarsEnvironment=p;var o=r(456),a=n(r(564)),l=r(880),i=r(103),s=n(r(677)),u=r(352);t.VERSION="4.7.8",t.COMPILER_REVISION=8,t.LAST_COMPATIBLE_COMPILER_REVISION=7,t.REVISION_CHANGES={1:"<= 1.0.rc.2",2:"== 1.0.0-rc.3",3:"== 1.0.0-rc.4",4:"== 1.x.x",5:"== 2.0.0-alpha.x",6:">= 2.0.0-beta.1",7:">= 4.0.0 <4.3.0",8:">= 4.3.0"};var c="[object Object]";function p(e,t,r){this.helpers=e||{},this.partials=t||{},this.decorators=r||{},l.registerDefaultHelpers(this),i.registerDefaultDecorators(this)}p.prototype={constructor:p,logger:s.default,log:s.default.log,registerHelper:function(e,t){if(o.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple helpers");o.extend(this.helpers,e)}else this.helpers[e]=t},unregisterHelper:function(e){delete this.helpers[e]},registerPartial:function(e,t){if(o.toString.call(e)===c)o.extend(this.partials,e);else{if(void 0===t)throw new a.default('Attempting to register a partial called "'+e+'" as undefined');this.partials[e]=t}},unregisterPartial:function(e){delete this.partials[e]},registerDecorator:function(e,t){if(o.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple decorators");o.extend(this.decorators,e)}else this.decorators[e]=t},unregisterDecorator:function(e){delete this.decorators[e]},resetLoggedPropertyAccesses:function(){u.resetLoggedProperties()}};var d=s.default.log;t.log=d,t.createFrame=o.createFrame,t.logger=s.default},103:(e,t,r)=>{"use strict";t.__esModule=!0,t.registerDefaultDecorators=function(e){o.default(e)};var n,o=(n=r(731))&&n.__esModule?n:{default:n}},731:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456);t.default=function(e){e.registerDecorator("inline",(function(e,t,r,o){var a=e;return t.partials||(t.partials={},a=function(o,a){var l=r.partials;r.partials=n.extend({},l,t.partials);var i=e(o,a);return r.partials=l,i}),t.partials[o.args[0]]=o.fn,a}))},e.exports=t.default},564:(e,t)=>{"use strict";t.__esModule=!0;var r=["description","fileName","lineNumber","endLineNumber","message","name","number","stack"];function n(e,t){var o=t&&t.loc,a=void 0,l=void 0,i=void 0,s=void 0;o&&(a=o.start.line,l=o.end.line,i=o.start.column,s=o.end.column,e+=" - "+a+":"+i);for(var u=Error.prototype.constructor.call(this,e),c=0;c{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.registerDefaultHelpers=function(e){o.default(e),a.default(e),l.default(e),i.default(e),s.default(e),u.default(e),c.default(e)},t.moveHelperToHooks=function(e,t,r){e.helpers[t]&&(e.hooks[t]=e.helpers[t],r||delete e.helpers[t])};var o=n(r(582)),a=n(r(646)),l=n(r(170)),i=n(r(264)),s=n(r(233)),u=n(r(205)),c=n(r(331))},582:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456);t.default=function(e){e.registerHelper("blockHelperMissing",(function(t,r){var o=r.inverse,a=r.fn;if(!0===t)return a(this);if(!1===t||null==t)return o(this);if(n.isArray(t))return t.length>0?(r.ids&&(r.ids=[r.name]),e.helpers.each(t,r)):o(this);if(r.data&&r.ids){var l=n.createFrame(r.data);l.contextPath=n.appendContextPath(r.data.contextPath,r.name),r={data:l}}return a(t,r)}))},e.exports=t.default},646:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("each",(function(e,t){if(!t)throw new a.default("Must pass iterator to #each");var r,n=t.fn,l=t.inverse,i=0,s="",u=void 0,c=void 0;function p(t,r,a){u&&(u.key=t,u.index=r,u.first=0===r,u.last=!!a,c&&(u.contextPath=c+t)),s+=n(e[t],{data:u,blockParams:o.blockParams([e[t],t],[c+t,null])})}if(t.data&&t.ids&&(c=o.appendContextPath(t.data.contextPath,t.ids[0])+"."),o.isFunction(e)&&(e=e.call(this)),t.data&&(u=o.createFrame(t.data)),e&&"object"==typeof e)if(o.isArray(e))for(var d=e.length;i{"use strict";t.__esModule=!0;var n,o=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("helperMissing",(function(){if(1!==arguments.length)throw new o.default('Missing helper: "'+arguments[arguments.length-1].name+'"')}))},e.exports=t.default},264:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("if",(function(e,t){if(2!=arguments.length)throw new a.default("#if requires exactly one argument");return o.isFunction(e)&&(e=e.call(this)),!t.hash.includeZero&&!e||o.isEmpty(e)?t.inverse(this):t.fn(this)})),e.registerHelper("unless",(function(t,r){if(2!=arguments.length)throw new a.default("#unless requires exactly one argument");return e.helpers.if.call(this,t,{fn:r.inverse,inverse:r.fn,hash:r.hash})}))},e.exports=t.default},233:(e,t)=>{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("log",(function(){for(var t=[void 0],r=arguments[arguments.length-1],n=0;n{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("lookup",(function(e,t,r){return e?r.lookupProperty(e,t):e}))},e.exports=t.default},331:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("with",(function(e,t){if(2!=arguments.length)throw new a.default("#with requires exactly one argument");o.isFunction(e)&&(e=e.call(this));var r=t.fn;if(o.isEmpty(e))return t.inverse(this);var n=t.data;return t.data&&t.ids&&((n=o.createFrame(t.data)).contextPath=o.appendContextPath(t.data.contextPath,t.ids[0])),r(e,{data:n,blockParams:o.blockParams([e],[n&&n.contextPath])})}))},e.exports=t.default},955:(e,t,r)=>{"use strict";t.__esModule=!0,t.createNewLookupObject=function(){for(var e=arguments.length,t=Array(e),r=0;r{"use strict";t.__esModule=!0,t.createProtoAccessControl=function(e){var t=Object.create(null);t.constructor=!1,t.__defineGetter__=!1,t.__defineSetter__=!1,t.__lookupGetter__=!1;var r=Object.create(null);return r.__proto__=!1,{properties:{whitelist:o.createNewLookupObject(r,e.allowedProtoProperties),defaultValue:e.allowProtoPropertiesByDefault},methods:{whitelist:o.createNewLookupObject(t,e.allowedProtoMethods),defaultValue:e.allowProtoMethodsByDefault}}},t.resultIsAllowed=function(e,t,r){return function(e,t){return void 0!==e.whitelist[t]?!0===e.whitelist[t]:void 0!==e.defaultValue?e.defaultValue:(function(e){!0!==l[e]&&(l[e]=!0,a.default.log("error",'Handlebars: Access has been denied to resolve the property "'+e+'" because it is not an "own property" of its parent.\nYou can add a runtime option to disable the check or this warning:\nSee https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details'))}(t),!1)}("function"==typeof e?t.methods:t.properties,r)},t.resetLoggedProperties=function(){Object.keys(l).forEach((function(e){delete l[e]}))};var n,o=r(955),a=(n=r(677))&&n.__esModule?n:{default:n},l=Object.create(null)},423:(e,t)=>{"use strict";t.__esModule=!0,t.wrapHelper=function(e,t){return"function"!=typeof e?e:function(){return arguments[arguments.length-1]=t(arguments[arguments.length-1]),e.apply(this,arguments)}}},677:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456),o={methodMap:["debug","info","warn","error"],level:"info",lookupLevel:function(e){if("string"==typeof e){var t=n.indexOf(o.methodMap,e.toLowerCase());e=t>=0?t:parseInt(e,10)}return e},log:function(e){if(e=o.lookupLevel(e),"undefined"!=typeof console&&o.lookupLevel(o.level)<=e){var t=o.methodMap[e];console[t]||(t="log");for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;a{"use strict";t.__esModule=!0,t.default=function(e){"object"!=typeof globalThis&&(Object.prototype.__defineGetter__("__magic__",(function(){return this})),__magic__.globalThis=__magic__,delete Object.prototype.__magic__);var t=globalThis.Handlebars;e.noConflict=function(){return globalThis.Handlebars===e&&(globalThis.Handlebars=t),e}},e.exports=t.default},433:(e,t,r)=>{"use strict";t.__esModule=!0,t.checkRevision=function(e){var t=e&&e[0]||1,r=l.COMPILER_REVISION;if(!(t>=l.LAST_COMPATIBLE_COMPILER_REVISION&&t<=l.COMPILER_REVISION)){if(t{"use strict";function r(e){this.string=e}t.__esModule=!0,r.prototype.toString=r.prototype.toHTML=function(){return""+this.string},t.default=r,e.exports=t.default},456:(e,t)=>{"use strict";t.__esModule=!0,t.extend=l,t.indexOf=function(e,t){for(var r=0,n=e.length;r":">",'"':""","'":"'","`":"`","=":"="},n=/[&<>"'`=]/g,o=/[&<>"'`=]/;function a(e){return r[e]}function l(e){for(var t=1;t{e.exports=r(916).default},768:(e,t,r)=>{var n=r(128),o={title:"My Title"},a={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return(null!=(a=e.invokePartial(l(n,"person"),t,{name:"person",data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")+(null!=(a=e.invokePartial(l(n,"person"),t,{name:"person",hash:{age:"30",name:"Jerry"},data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")+(null!=(a=e.invokePartial(l(n,"star"),t,{name:"star",data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")},usePartial:!0,useData:!0};n.partials.content=n.template(a),n.partials.footer=n.template({compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){return"
MY FOOTER
\n"},useData:!0});var l={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l,i=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return"

Hello "+e.escapeExpression("function"==typeof(l=i(r,"helloName")||e.strict(t,"helloName",{start:{line:1,column:13},end:{line:1,column:22}}))?l.call(null!=t?t:e.nullContext||{},{name:"helloName",hash:{},data:o,loc:{start:{line:1,column:10},end:{line:1,column:25}}}):l)+'!

\n\n\n
\n'+(null!=(a=e.invokePartial(i(n,"contentContainer"),t,{name:"contentContainer",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:"")+'
\n\n"},usePartial:!0,useData:!0};n.partials.layout=n.template(l);var i={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return e.escapeExpression("function"==typeof(a=l(r,"navText")||e.strict(t,"navText",{start:{line:1,column:2},end:{line:1,column:9}}))?a.call(null!=t?t:e.nullContext||{},{name:"navText",hash:{},data:o,loc:{start:{line:1,column:0},end:{line:1,column:11}}}):a)+': Home | People\n'},useData:!0};n.partials.nav=n.template(i);var s={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.strict,i=null!=t?t:e.nullContext||{},s="function",u=e.escapeExpression,c=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return"
Person: "+u(typeof(a=c(r,"name")||l(t,"name",{start:{line:1,column:15},end:{line:1,column:19}}))===s?a.call(i,{name:"name",hash:{},data:o,loc:{start:{line:1,column:13},end:{line:1,column:21}}}):a)+", "+u(typeof(a=c(r,"age")||l(t,"age",{start:{line:1,column:25},end:{line:1,column:28}}))===s?a.call(i,{name:"age",hash:{},data:o,loc:{start:{line:1,column:23},end:{line:1,column:30}}}):a)+"
\n"},useData:!0};n.partials.person=n.template(s);var u={compiler:[8,">= 4.3.0"],main:function(e,t,n,o,a){return'
Resolve image in sub partial
\n\n'},useData:!0};n.partials.star=n.template(u);var c={1:function(e,t,r,n,o,a,l){return e.lookupProperty,""},2:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"nav"),t,{name:"nav",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},4:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"content"),t,{name:"content",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},6:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"footer"),t,{name:"footer",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o,a,l){var i,s=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(i=e.invokePartial(s(n,"layout"),t,{name:"layout",fn:e.program(1,o,0,a,l),inverse:e.noop,data:o,helpers:r,partials:n,decorators:e.decorators}))?i:""},"1_d":function(e,t,r,n,o,a,l){var i=r.decorators,s=r.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return e=s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(2,o,0,a,l),inverse:r.noop,args:["navContainer"],data:o,loc:{start:{line:2,column:2},end:{line:4,column:13}}})||e,e=s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(4,o,0,a,l),inverse:r.noop,args:["contentContainer"],data:o,loc:{start:{line:5,column:2},end:{line:7,column:13}}})||e,s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(6,o,0,a,l),inverse:r.noop,args:["footerContainer"],data:o,loc:{start:{line:8,column:2},end:{line:10,column:13}}})||e},useDecorators:!0,usePartial:!0,useData:!0,useDepths:!0};e.exports=e=>(n.default||n).template(c)(Object.assign({},o,e))},487:(e,t,r)=>{"use strict";e.exports=r.p+"img/stern.6adb226f.svg"}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var a=t[n]={exports:{}};return e[n](a,a.exports,r),a.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e;r.g.importScripts&&(e=r.g.location+"");var t=r.g.document;if(!e&&t&&(t.currentScript&&"SCRIPT"===t.currentScript.tagName.toUpperCase()&&(e=t.currentScript.src),!e)){var n=t.getElementsByTagName("script");if(n.length)for(var o=n.length-1;o>-1&&(!e||!/^http(s?):/.test(e));)e=n[o--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),r.p=e})(),(()=>{"use strict";var e=r(768),t=r.n(e);document.getElementById("main").innerHTML=t()({helloName:"World",name:"Max",age:21,navText:"Navigation"}),console.log(">> app")})()})(); \ No newline at end of file +(()=>{var e={916:(e,t,r)=>{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}t.__esModule=!0;var a=o(r(387)),l=n(r(16)),i=n(r(564)),s=o(r(456)),u=o(r(433)),c=n(r(157));function p(){var e=new a.HandlebarsEnvironment;return s.extend(e,a),e.SafeString=l.default,e.Exception=i.default,e.Utils=s,e.escapeExpression=s.escapeExpression,e.VM=u,e.template=function(t){return u.template(t,e)},e}var d=p();d.create=p,c.default(d),d.default=d,t.default=d,e.exports=t.default},387:(e,t,r)=>{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.HandlebarsEnvironment=p;var o=r(456),a=n(r(564)),l=r(880),i=r(103),s=n(r(677)),u=r(352);t.VERSION="4.7.8",t.COMPILER_REVISION=8,t.LAST_COMPATIBLE_COMPILER_REVISION=7,t.REVISION_CHANGES={1:"<= 1.0.rc.2",2:"== 1.0.0-rc.3",3:"== 1.0.0-rc.4",4:"== 1.x.x",5:"== 2.0.0-alpha.x",6:">= 2.0.0-beta.1",7:">= 4.0.0 <4.3.0",8:">= 4.3.0"};var c="[object Object]";function p(e,t,r){this.helpers=e||{},this.partials=t||{},this.decorators=r||{},l.registerDefaultHelpers(this),i.registerDefaultDecorators(this)}p.prototype={constructor:p,logger:s.default,log:s.default.log,registerHelper:function(e,t){if(o.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple helpers");o.extend(this.helpers,e)}else this.helpers[e]=t},unregisterHelper:function(e){delete this.helpers[e]},registerPartial:function(e,t){if(o.toString.call(e)===c)o.extend(this.partials,e);else{if(void 0===t)throw new a.default('Attempting to register a partial called "'+e+'" as undefined');this.partials[e]=t}},unregisterPartial:function(e){delete this.partials[e]},registerDecorator:function(e,t){if(o.toString.call(e)===c){if(t)throw new a.default("Arg not supported with multiple decorators");o.extend(this.decorators,e)}else this.decorators[e]=t},unregisterDecorator:function(e){delete this.decorators[e]},resetLoggedPropertyAccesses:function(){u.resetLoggedProperties()}};var d=s.default.log;t.log=d,t.createFrame=o.createFrame,t.logger=s.default},103:(e,t,r)=>{"use strict";t.__esModule=!0,t.registerDefaultDecorators=function(e){o.default(e)};var n,o=(n=r(731))&&n.__esModule?n:{default:n}},731:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456);t.default=function(e){e.registerDecorator("inline",(function(e,t,r,o){var a=e;return t.partials||(t.partials={},a=function(o,a){var l=r.partials;r.partials=n.extend({},l,t.partials);var i=e(o,a);return r.partials=l,i}),t.partials[o.args[0]]=o.fn,a}))},e.exports=t.default},564:(e,t)=>{"use strict";t.__esModule=!0;var r=["description","fileName","lineNumber","endLineNumber","message","name","number","stack"];function n(e,t){var o=t&&t.loc,a=void 0,l=void 0,i=void 0,s=void 0;o&&(a=o.start.line,l=o.end.line,i=o.start.column,s=o.end.column,e+=" - "+a+":"+i);for(var u=Error.prototype.constructor.call(this,e),c=0;c{"use strict";function n(e){return e&&e.__esModule?e:{default:e}}t.__esModule=!0,t.registerDefaultHelpers=function(e){o.default(e),a.default(e),l.default(e),i.default(e),s.default(e),u.default(e),c.default(e)},t.moveHelperToHooks=function(e,t,r){e.helpers[t]&&(e.hooks[t]=e.helpers[t],r||delete e.helpers[t])};var o=n(r(582)),a=n(r(646)),l=n(r(170)),i=n(r(264)),s=n(r(233)),u=n(r(205)),c=n(r(331))},582:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456);t.default=function(e){e.registerHelper("blockHelperMissing",(function(t,r){var o=r.inverse,a=r.fn;if(!0===t)return a(this);if(!1===t||null==t)return o(this);if(n.isArray(t))return t.length>0?(r.ids&&(r.ids=[r.name]),e.helpers.each(t,r)):o(this);if(r.data&&r.ids){var l=n.createFrame(r.data);l.contextPath=n.appendContextPath(r.data.contextPath,r.name),r={data:l}}return a(t,r)}))},e.exports=t.default},646:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("each",(function(e,t){if(!t)throw new a.default("Must pass iterator to #each");var r,n=t.fn,l=t.inverse,i=0,s="",u=void 0,c=void 0;function p(t,r,a){u&&(u.key=t,u.index=r,u.first=0===r,u.last=!!a,c&&(u.contextPath=c+t)),s+=n(e[t],{data:u,blockParams:o.blockParams([e[t],t],[c+t,null])})}if(t.data&&t.ids&&(c=o.appendContextPath(t.data.contextPath,t.ids[0])+"."),o.isFunction(e)&&(e=e.call(this)),t.data&&(u=o.createFrame(t.data)),e&&"object"==typeof e)if(o.isArray(e))for(var d=e.length;i{"use strict";t.__esModule=!0;var n,o=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("helperMissing",(function(){if(1!==arguments.length)throw new o.default('Missing helper: "'+arguments[arguments.length-1].name+'"')}))},e.exports=t.default},264:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("if",(function(e,t){if(2!=arguments.length)throw new a.default("#if requires exactly one argument");return o.isFunction(e)&&(e=e.call(this)),!t.hash.includeZero&&!e||o.isEmpty(e)?t.inverse(this):t.fn(this)})),e.registerHelper("unless",(function(t,r){if(2!=arguments.length)throw new a.default("#unless requires exactly one argument");return e.helpers.if.call(this,t,{fn:r.inverse,inverse:r.fn,hash:r.hash})}))},e.exports=t.default},233:(e,t)=>{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("log",(function(){for(var t=[void 0],r=arguments[arguments.length-1],n=0;n{"use strict";t.__esModule=!0,t.default=function(e){e.registerHelper("lookup",(function(e,t,r){return e?r.lookupProperty(e,t):e}))},e.exports=t.default},331:(e,t,r)=>{"use strict";t.__esModule=!0;var n,o=r(456),a=(n=r(564))&&n.__esModule?n:{default:n};t.default=function(e){e.registerHelper("with",(function(e,t){if(2!=arguments.length)throw new a.default("#with requires exactly one argument");o.isFunction(e)&&(e=e.call(this));var r=t.fn;if(o.isEmpty(e))return t.inverse(this);var n=t.data;return t.data&&t.ids&&((n=o.createFrame(t.data)).contextPath=o.appendContextPath(t.data.contextPath,t.ids[0])),r(e,{data:n,blockParams:o.blockParams([e],[n&&n.contextPath])})}))},e.exports=t.default},955:(e,t,r)=>{"use strict";t.__esModule=!0,t.createNewLookupObject=function(){for(var e=arguments.length,t=Array(e),r=0;r{"use strict";t.__esModule=!0,t.createProtoAccessControl=function(e){var t=Object.create(null);t.constructor=!1,t.__defineGetter__=!1,t.__defineSetter__=!1,t.__lookupGetter__=!1;var r=Object.create(null);return r.__proto__=!1,{properties:{whitelist:o.createNewLookupObject(r,e.allowedProtoProperties),defaultValue:e.allowProtoPropertiesByDefault},methods:{whitelist:o.createNewLookupObject(t,e.allowedProtoMethods),defaultValue:e.allowProtoMethodsByDefault}}},t.resultIsAllowed=function(e,t,r){return function(e,t){return void 0!==e.whitelist[t]?!0===e.whitelist[t]:void 0!==e.defaultValue?e.defaultValue:(function(e){!0!==l[e]&&(l[e]=!0,a.default.log("error",'Handlebars: Access has been denied to resolve the property "'+e+'" because it is not an "own property" of its parent.\nYou can add a runtime option to disable the check or this warning:\nSee https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details'))}(t),!1)}("function"==typeof e?t.methods:t.properties,r)},t.resetLoggedProperties=function(){Object.keys(l).forEach((function(e){delete l[e]}))};var n,o=r(955),a=(n=r(677))&&n.__esModule?n:{default:n},l=Object.create(null)},423:(e,t)=>{"use strict";t.__esModule=!0,t.wrapHelper=function(e,t){return"function"!=typeof e?e:function(){return arguments[arguments.length-1]=t(arguments[arguments.length-1]),e.apply(this,arguments)}}},677:(e,t,r)=>{"use strict";t.__esModule=!0;var n=r(456),o={methodMap:["debug","info","warn","error"],level:"info",lookupLevel:function(e){if("string"==typeof e){var t=n.indexOf(o.methodMap,e.toLowerCase());e=t>=0?t:parseInt(e,10)}return e},log:function(e){if(e=o.lookupLevel(e),"undefined"!=typeof console&&o.lookupLevel(o.level)<=e){var t=o.methodMap[e];console[t]||(t="log");for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;a{"use strict";t.__esModule=!0,t.default=function(e){"object"!=typeof globalThis&&(Object.prototype.__defineGetter__("__magic__",(function(){return this})),__magic__.globalThis=__magic__,delete Object.prototype.__magic__);var t=globalThis.Handlebars;e.noConflict=function(){return globalThis.Handlebars===e&&(globalThis.Handlebars=t),e}},e.exports=t.default},433:(e,t,r)=>{"use strict";t.__esModule=!0,t.checkRevision=function(e){var t=e&&e[0]||1,r=l.COMPILER_REVISION;if(!(t>=l.LAST_COMPATIBLE_COMPILER_REVISION&&t<=l.COMPILER_REVISION)){if(t{"use strict";function r(e){this.string=e}t.__esModule=!0,r.prototype.toString=r.prototype.toHTML=function(){return""+this.string},t.default=r,e.exports=t.default},456:(e,t)=>{"use strict";t.__esModule=!0,t.extend=l,t.indexOf=function(e,t){for(var r=0,n=e.length;r":">",'"':""","'":"'","`":"`","=":"="},n=/[&<>"'`=]/g,o=/[&<>"'`=]/;function a(e){return r[e]}function l(e){for(var t=1;t{e.exports=r(916).default},768:(e,t,r)=>{var n=r(128),o={title:"My Title"},a={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return(null!=(a=e.invokePartial(l(n,"person"),t,{name:"person",data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")+(null!=(a=e.invokePartial(l(n,"person"),t,{name:"person",hash:{age:"30",name:"Jerry"},data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")+(null!=(a=e.invokePartial(l(n,"star"),t,{name:"star",data:o,helpers:r,partials:n,decorators:e.decorators}))?a:"")},usePartial:!0,useData:!0};n.partials.content=n.template(a),n.partials.footer=n.template({compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){return"
MY FOOTER
\n"},useData:!0});var l={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l,i=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return"

Hello "+e.escapeExpression("function"==typeof(l=i(r,"helloName")||e.strict(t,"helloName",{start:{line:1,column:13},end:{line:1,column:22}}))?l.call(null!=t?t:e.nullContext||{},{name:"helloName",hash:{},data:o,loc:{start:{line:1,column:10},end:{line:1,column:25}}}):l)+'!

\n\n\n
\n'+(null!=(a=e.invokePartial(i(n,"contentContainer"),t,{name:"contentContainer",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:"")+'
\n\n"},usePartial:!0,useData:!0};n.partials.layout=n.template(l);var i={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return e.escapeExpression("function"==typeof(a=l(r,"navText")||e.strict(t,"navText",{start:{line:1,column:2},end:{line:1,column:9}}))?a.call(null!=t?t:e.nullContext||{},{name:"navText",hash:{},data:o,loc:{start:{line:1,column:0},end:{line:1,column:11}}}):a)+': Home | People\n'},useData:!0};n.partials.nav=n.template(i);var s={compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o){var a,l=e.strict,i=null!=t?t:e.nullContext||{},s="function",u=e.escapeExpression,c=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return"
Person: "+u(typeof(a=c(r,"name")||l(t,"name",{start:{line:1,column:15},end:{line:1,column:19}}))===s?a.call(i,{name:"name",hash:{},data:o,loc:{start:{line:1,column:13},end:{line:1,column:21}}}):a)+", "+u(typeof(a=c(r,"age")||l(t,"age",{start:{line:1,column:25},end:{line:1,column:28}}))===s?a.call(i,{name:"age",hash:{},data:o,loc:{start:{line:1,column:23},end:{line:1,column:30}}}):a)+"
\n"},useData:!0};n.partials.person=n.template(s);var u={compiler:[8,">= 4.3.0"],main:function(e,t,n,o,a){return'
Resolve image in sub partial
\n\n'},useData:!0};n.partials.star=n.template(u);var c={1:function(e,t,r,n,o,a,l){return e.lookupProperty,""},2:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"nav"),t,{name:"nav",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},4:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"content"),t,{name:"content",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},6:function(e,t,r,n,o){var a,l=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(a=e.invokePartial(l(n,"footer"),t,{name:"footer",data:o,indent:" ",helpers:r,partials:n,decorators:e.decorators}))?a:""},compiler:[8,">= 4.3.0"],main:function(e,t,r,n,o,a,l){var i,s=e.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return null!=(i=e.invokePartial(s(n,"layout"),t,{name:"layout",fn:e.program(1,o,0,a,l),inverse:e.noop,data:o,helpers:r,partials:n,decorators:e.decorators}))?i:""},"1_d":function(e,t,r,n,o,a,l){var i=r.decorators,s=r.lookupProperty||function(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]};return e=s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(2,o,0,a,l),inverse:r.noop,args:["navContainer"],data:o,loc:{start:{line:2,column:2},end:{line:4,column:13}}})||e,e=s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(4,o,0,a,l),inverse:r.noop,args:["contentContainer"],data:o,loc:{start:{line:5,column:2},end:{line:7,column:13}}})||e,s(i,"inline")(e,t,r,{name:"inline",hash:{},fn:r.program(6,o,0,a,l),inverse:r.noop,args:["footerContainer"],data:o,loc:{start:{line:8,column:2},end:{line:10,column:13}}})||e},useDecorators:!0,usePartial:!0,useData:!0,useDepths:!0};e.exports=e=>(n.default||n).template(c)(Object.assign({},o,e))},487:(e,t,r)=>{"use strict";e.exports=r.p+"img/stern.6adb226f.svg"}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var a=t[n]={exports:{}};return e[n](a,a.exports,r),a.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e;r.g.importScripts&&(e=r.g.location+"");var t=r.g.document;if(!e&&t&&(t.currentScript&&"SCRIPT"===t.currentScript.tagName.toUpperCase()&&(e=t.currentScript.src),!e)){var n=t.getElementsByTagName("script");if(n.length)for(var o=n.length-1;o>-1&&(!e||!/^http(s?):/.test(e));)e=n[o--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),r.p=e})(),(()=>{"use strict";var e=r(768),t=r.n(e);document.getElementById("app").innerHTML=t()({helloName:"World",name:"Max",age:21,navText:"Navigation"}),console.log(">> app")})()})(); \ No newline at end of file diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/index.html b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/index.html index 3e6a465b..232bd3c1 100644 --- a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/index.html +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/expected/index.html @@ -6,6 +6,6 @@ -
+
- \ No newline at end of file + diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/app.js b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/app.js index cd07b5b0..05e1ea78 100644 --- a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/app.js +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/app.js @@ -7,6 +7,6 @@ const locals = { navText: 'Navigation', }; -document.getElementById('main').innerHTML = tmpl(locals); +document.getElementById('app').innerHTML = tmpl(locals); console.log('>> app'); diff --git a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/index.hbs b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/index.hbs index 6c6538fb..c66a6cd4 100644 --- a/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/index.hbs +++ b/test/cases/_preprocessor/js-tmpl-hbs-compile-partials/src/index.hbs @@ -6,6 +6,6 @@ -
+
- \ No newline at end of file + diff --git a/test/preprocessor.test.js b/test/preprocessor.test.js index d9c79cec..756b4b4f 100644 --- a/test/preprocessor.test.js +++ b/test/preprocessor.test.js @@ -98,6 +98,7 @@ describe('usage template in js on client side', () => { // Handlebars test('hbs: compile to fn', () => compareFiles('_preprocessor/js-tmpl-hbs-compile')); + test('hbs: compile to fn with helpers', () => compareFiles('_preprocessor/js-tmpl-hbs-compile-helpers')); test('hbs: compile to fn with partials', () => compareFiles('_preprocessor/js-tmpl-hbs-compile-partials')); test('hbs: compile to fn with variables', () => compareFiles('_preprocessor/js-tmpl-hbs-compile-variables')); diff --git a/test/unit.test.js b/test/unit.test.js index 1b5e03e6..a5a447a3 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -7,6 +7,7 @@ import { HtmlParser } from '../src/Common/HtmlParser'; import { isDir, loadModule, resolveFile, filterParentPaths, relativePathVerbose } from '../src/Common/FileUtils'; import { stringifyJSON, + stringifyFn, injectBeforeEndHead, injectBeforeEndBody, escapeSequences, @@ -236,13 +237,6 @@ describe('file extension', () => { }); describe('utils', () => { - test('stringifyJSON', () => { - const json = { fn() {} }; - const received = stringifyJSON(json); - const expected = `{"fn":()=>{}}`; - return expect(received).toEqual(expected); - }); - test('injectBeforeEndHead', () => { const html = `test

body

`; const received = injectBeforeEndHead(html, ``); @@ -297,6 +291,59 @@ describe('utils', () => { }); }); +describe('stringifyJSON', () => { + test('{ fn() {} }', () => { + const json = { fn() {} }; + const received = stringifyJSON(json); + const expected = `{"fn":function() {}}`; + return expect(received).toEqual(expected); + }); + + test('{ fn: () => {} }', () => { + const json = { fn: () => {} }; + const received = stringifyJSON(json); + const expected = `{"fn":() => {}}`; + return expect(received).toEqual(expected); + }); + + test('{ fn: function() {} }', () => { + const json = { fn: function () {} }; + const received = stringifyJSON(json); + const expected = `{"fn":function () {}}`; + return expect(received).toEqual(expected); + }); +}); + +describe('stringifyFn', () => { + test('{ fn() {} }', () => { + const obj = { fn() {} }; + const received = stringifyFn(obj.fn); + const expected = `function() {}`; + return expect(received).toEqual(expected); + }); + + test('{ fn: function() {} }', () => { + const obj = { fn: function () {} }; + const received = stringifyFn(obj.fn); + const expected = `function () {}`; + return expect(received).toEqual(expected); + }); + + test('{ fn: () => {} }', () => { + const obj = { fn: () => {} }; + const received = stringifyFn(obj.fn); + const expected = `() => {}`; + return expect(received).toEqual(expected); + }); + + test('{ fn: o => o.toString() }', () => { + const obj = { fn: (o) => o.toString() }; + const received = stringifyFn(obj.fn); + const expected = `o => o.toString()`; + return expect(received).toEqual(expected); + }); +}); + describe('parse attributes in tag', () => { test('parseTag img without src attr', () => { const source = 'apple';