From 226598edc4221067a43f2bce0329734676bdd9ea Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 28 Jul 2020 16:37:13 -0700 Subject: [PATCH] js: Use Handlebars as a module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/activity.js | 1 - frontend_tests/node_tests/compose.js | 1 - frontend_tests/node_tests/composebox_typeahead.js | 1 - frontend_tests/node_tests/drafts.js | 1 - frontend_tests/node_tests/filter.js | 1 - frontend_tests/node_tests/i18n.js | 1 - frontend_tests/node_tests/input_pill.js | 1 - frontend_tests/node_tests/search_pill.js | 1 - frontend_tests/node_tests/search_suggestion.js | 1 - .../node_tests/search_suggestion_legacy.js | 1 - frontend_tests/node_tests/typeahead_helper.js | 1 - frontend_tests/zjsunit/handlebars.js | 14 +++++--------- frontend_tests/zjsunit/index.js | 3 ++- static/js/bundles/app.js | 1 - static/js/compose.js | 2 ++ static/js/drafts.js | 1 + static/js/filter.js | 1 + static/js/search_suggestion.js | 2 ++ static/js/templates.js | 2 ++ static/js/typeahead_helper.js | 2 ++ tools/webpack.assets.json | 3 --- tools/webpack.config.ts | 1 - 23 files changed, 17 insertions(+), 27 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index ef9c8a183f..3d56f5861f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -99,7 +99,6 @@ "$": false, "FetchStatus": false, "Filter": false, - "Handlebars": false, "LightboxCanvas": false, "ListCursor": false, "MessageListData": false, diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js index a8176028c5..ae139374b9 100644 --- a/frontend_tests/node_tests/activity.js +++ b/frontend_tests/node_tests/activity.js @@ -75,7 +75,6 @@ set_global("stream_popover", _stream_popover); set_global("ui", _ui); zrequire("compose_fade"); -set_global("Handlebars", global.make_handlebars()); zrequire("unread"); zrequire("hash_util"); zrequire("narrow"); diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index b1ae9fb5ca..d1d0b6c052 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -67,7 +67,6 @@ zrequire("compose_ui"); const util = zrequire("util"); zrequire("rtl"); zrequire("common"); -set_global("Handlebars", global.make_handlebars()); zrequire("stream_data"); zrequire("compose_state"); zrequire("people"); diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index d78682abde..2b4ccec0a4 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -2,7 +2,6 @@ const emoji = zrequire("emoji", "shared/js/emoji"); const typeahead = zrequire("typeahead", "shared/js/typeahead"); zrequire("compose_state"); zrequire("pm_conversations"); -set_global("Handlebars", global.make_handlebars()); zrequire("templates"); zrequire("typeahead_helper"); zrequire("people"); diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js index 41c39b19bb..5e03fb09e1 100644 --- a/frontend_tests/node_tests/drafts.js +++ b/frontend_tests/node_tests/drafts.js @@ -5,7 +5,6 @@ set_global("$", global.make_zjquery()); zrequire("localstorage"); zrequire("drafts"); zrequire("timerender"); -set_global("Handlebars", global.make_handlebars()); zrequire("stream_color"); zrequire("colorspace"); diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.js index 903b0cac81..6247f8477b 100644 --- a/frontend_tests/node_tests/filter.js +++ b/frontend_tests/node_tests/filter.js @@ -1,7 +1,6 @@ zrequire("unread"); zrequire("stream_data"); zrequire("people"); -set_global("Handlebars", global.make_handlebars()); global.stub_out_jquery(); set_global("$", global.make_zjquery()); zrequire("message_util", "js/message_util"); diff --git a/frontend_tests/node_tests/i18n.js b/frontend_tests/node_tests/i18n.js index 3cb745b068..23616fee5c 100644 --- a/frontend_tests/node_tests/i18n.js +++ b/frontend_tests/node_tests/i18n.js @@ -1,4 +1,3 @@ -set_global("Handlebars", global.make_handlebars()); zrequire("templates"); // We download our translations in `page_params` (which diff --git a/frontend_tests/node_tests/input_pill.js b/frontend_tests/node_tests/input_pill.js index d677dd4dcb..aef52736c3 100644 --- a/frontend_tests/node_tests/input_pill.js +++ b/frontend_tests/node_tests/input_pill.js @@ -1,7 +1,6 @@ set_global("$", global.make_zjquery()); zrequire("input_pill"); -set_global("Handlebars", global.make_handlebars()); zrequire("templates"); set_global("document", {}); diff --git a/frontend_tests/node_tests/search_pill.js b/frontend_tests/node_tests/search_pill.js index 200c60d524..b26776af02 100644 --- a/frontend_tests/node_tests/search_pill.js +++ b/frontend_tests/node_tests/search_pill.js @@ -1,7 +1,6 @@ zrequire("search_pill"); zrequire("input_pill"); zrequire("Filter", "js/filter"); -set_global("Handlebars", global.make_handlebars()); const is_starred_item = { display_value: "is:starred", diff --git a/frontend_tests/node_tests/search_suggestion.js b/frontend_tests/node_tests/search_suggestion.js index 7ca20e0762..5bb1cbd590 100644 --- a/frontend_tests/node_tests/search_suggestion.js +++ b/frontend_tests/node_tests/search_suggestion.js @@ -13,7 +13,6 @@ page_params.realm_email_address_visibility = const huddle_data = zrequire("huddle_data"); zrequire("typeahead_helper"); -set_global("Handlebars", global.make_handlebars()); zrequire("Filter", "js/filter"); zrequire("narrow_state"); zrequire("stream_data"); diff --git a/frontend_tests/node_tests/search_suggestion_legacy.js b/frontend_tests/node_tests/search_suggestion_legacy.js index 92d693d40e..301502879b 100644 --- a/frontend_tests/node_tests/search_suggestion_legacy.js +++ b/frontend_tests/node_tests/search_suggestion_legacy.js @@ -12,7 +12,6 @@ page_params.realm_email_address_visibility = const huddle_data = zrequire("huddle_data"); zrequire("typeahead_helper"); -set_global("Handlebars", global.make_handlebars()); zrequire("Filter", "js/filter"); zrequire("narrow_state"); zrequire("stream_data"); diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.js index 5354617ec1..a1911f766c 100644 --- a/frontend_tests/node_tests/typeahead_helper.js +++ b/frontend_tests/node_tests/typeahead_helper.js @@ -5,7 +5,6 @@ const settings_config = zrequire("settings_config"); page_params.realm_email_address_visibility = settings_config.email_address_visibility_values.admins_only.code; -set_global("Handlebars", global.make_handlebars()); zrequire("recent_senders"); zrequire("pm_conversations"); zrequire("people"); diff --git a/frontend_tests/zjsunit/handlebars.js b/frontend_tests/zjsunit/handlebars.js index a1a5d8d490..9daafc87e2 100644 --- a/frontend_tests/zjsunit/handlebars.js +++ b/frontend_tests/zjsunit/handlebars.js @@ -1,13 +1,11 @@ const fs = require("fs"); const path = require("path"); -const Handlebars = require("handlebars/dist/cjs/handlebars.js"); +const Handlebars = require("handlebars"); const {SourceMapConsumer, SourceNode} = require("source-map"); const templates_path = path.resolve(__dirname, "../../static/templates"); -exports.make_handlebars = () => Handlebars.create(); - exports.stub_templates = (stub) => { window.template_stub = stub; }; @@ -34,18 +32,16 @@ require.extensions[".hbs"] = (module, filename) => { const pc = hb.precompile(code, {preventIndent: true, srcName: filename}); const node = new SourceNode(); node.add([ - "let hb, template;\n", + 'const Handlebars = require("handlebars/runtime");\n', + "const template = Handlebars.template(", + SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)), + ");\n", "module.exports = (...args) => {\n", " if (window.template_stub !== undefined) {\n", " return window.template_stub(", JSON.stringify(name), ", ...args);\n", " }\n", - " if (hb !== Handlebars) {\n", - " template = (hb = Handlebars).template(", - SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)), - ");\n", - " }\n", " return template(...args);\n", "};\n", ]); diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.js index 1fa70d006a..6cfdb072c0 100644 --- a/frontend_tests/zjsunit/index.js +++ b/frontend_tests/zjsunit/index.js @@ -2,6 +2,7 @@ const fs = require("fs"); const Module = require("module"); const path = require("path"); +const Handlebars = require("handlebars/runtime"); const _ = require("lodash"); const finder = require("./finder.js"); @@ -57,7 +58,6 @@ global.with_stub = stub.with_stub; global.make_zjquery = zjquery.make_zjquery; // Set up Handlebars -global.make_handlebars = handlebars.make_handlebars; global.stub_templates = handlebars.stub_templates; const noop = function () {}; @@ -136,6 +136,7 @@ try { } namespace.restore(); + Handlebars.HandlebarsEnvironment(); }); } catch (e) { if (e.stack) { diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 1639e361c4..90c85c5278 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -7,7 +7,6 @@ import "jquery-caret-plugin/src/jquery.caret.js"; import "../../third/jquery-idle/jquery.idle.js"; import "spectrum-colorpicker"; import "jquery-validation/dist/jquery.validate.js"; -import "handlebars/dist/cjs/handlebars.runtime.js"; import "flatpickr/dist/flatpickr.js"; import "flatpickr/dist/plugins/confirmDate/confirmDate.js"; diff --git a/static/js/compose.js b/static/js/compose.js index 9b3a8bcfc5..00ecb4c71c 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -1,3 +1,5 @@ +const Handlebars = require("handlebars/runtime"); + const render_compose_all_everyone = require("../templates/compose_all_everyone.hbs"); const render_compose_announce = require("../templates/compose_announce.hbs"); const render_compose_invite_users = require("../templates/compose_invite_users.hbs"); diff --git a/static/js/drafts.js b/static/js/drafts.js index 848403cde7..f4f491416f 100644 --- a/static/js/drafts.js +++ b/static/js/drafts.js @@ -1,3 +1,4 @@ +const Handlebars = require("handlebars/runtime"); const XDate = require("xdate"); const render_draft_table_body = require("../templates/draft_table_body.hbs"); diff --git a/static/js/filter.js b/static/js/filter.js index e5461f15b0..d9768c8380 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -1,3 +1,4 @@ +const Handlebars = require("handlebars/runtime"); const _ = require("lodash"); const util = require("./util"); diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js index 24aed65337..d39aeeeace 100644 --- a/static/js/search_suggestion.js +++ b/static/js/search_suggestion.js @@ -1,3 +1,5 @@ +const Handlebars = require("handlebars/runtime"); + const huddle_data = require("./huddle_data"); const settings_data = require("./settings_data"); diff --git a/static/js/templates.js b/static/js/templates.js index a93fc79e7f..762312de78 100644 --- a/static/js/templates.js +++ b/static/js/templates.js @@ -1,3 +1,5 @@ +const Handlebars = require("handlebars/runtime"); + const util = require("./util"); // Below, we register Zulip-specific extensions to the handlebars API. diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.js index c14d4f3c58..6d29acf065 100644 --- a/static/js/typeahead_helper.js +++ b/static/js/typeahead_helper.js @@ -1,3 +1,5 @@ +const Handlebars = require("handlebars/runtime"); + const pygments_data = require("../generated/pygments_data.json"); const emoji = require("../shared/js/emoji"); const typeahead = require("../shared/js/typeahead"); diff --git a/tools/webpack.assets.json b/tools/webpack.assets.json index d199fd4aa8..edb6a875b2 100644 --- a/tools/webpack.assets.json +++ b/tools/webpack.assets.json @@ -8,7 +8,6 @@ "archive": [ "./static/js/bundles/portico.js", "katex/dist/katex.min.js", - "handlebars/dist/cjs/handlebars.runtime.js", "./static/js/archive.js", "./static/js/colorspace.js", "./static/js/floating_recipient_bar.js", @@ -28,7 +27,6 @@ "./static/styles/portico/landing-page.scss", "./static/js/billing/helpers.js", "./static/js/billing/billing.js", - "handlebars/dist/cjs/handlebars.runtime.js", "./static/js/templates.js", "./static/js/loading.js", "./static/styles/portico/billing.scss" @@ -39,7 +37,6 @@ "./static/styles/portico/landing-page.scss", "./static/js/billing/helpers.js", "./static/js/billing/upgrade.js", - "handlebars/dist/cjs/handlebars.runtime.js", "./static/js/templates.js", "./static/js/loading.js", "./static/styles/portico/billing.scss" diff --git a/tools/webpack.config.ts b/tools/webpack.config.ts index 44d6f42191..58bde4f359 100644 --- a/tools/webpack.config.ts +++ b/tools/webpack.config.ts @@ -246,7 +246,6 @@ export default (env?: string): webpack.Configuration[] => { {path: "./debug-require.js", name: "require"}, {path: "../static/js/debug.js"}, {path: "jquery/dist/jquery.js", name: ["$", "jQuery"]}, - {path: "handlebars/dist/cjs/handlebars.runtime.js", name: "Handlebars"}, ]; config.module!.rules.unshift(...getExposeLoaders(exposeOptions));