Build template list at Webpack time instead of manually keeping this in sync with the views folders

This commit is contained in:
Jérémie Astori 2018-01-02 01:38:57 -05:00
parent 068b842561
commit a122ed5b7f
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
2 changed files with 22 additions and 49 deletions

View File

@ -1,53 +1,26 @@
"use strict"; "use strict";
module.exports = { // This creates a version of `require()` in the context of the current
actions: { // directory, so we iterate over its content, which is a map statically built by
action: require("./actions/action.tpl"), // Webpack.
away: require("./actions/away.tpl"), // Second argument says it's recursive, third makes sure we only load templates.
back: require("./actions/back.tpl"), const requireViews = require.context(".", true, /\.tpl$/);
ban_list: require("./actions/ban_list.tpl"),
channel_list: require("./actions/channel_list.tpl"),
chghost: require("./actions/chghost.tpl"),
ctcp: require("./actions/ctcp.tpl"),
invite: require("./actions/invite.tpl"),
join: require("./actions/join.tpl"),
kick: require("./actions/kick.tpl"),
mode: require("./actions/mode.tpl"),
nick: require("./actions/nick.tpl"),
part: require("./actions/part.tpl"),
quit: require("./actions/quit.tpl"),
topic: require("./actions/topic.tpl"),
topic_set_by: require("./actions/topic_set_by.tpl"),
whois: require("./actions/whois.tpl"),
},
windows: { module.exports = requireViews.keys().reduce((acc, path) => {
sign_in: require("./windows/sign_in.tpl"), // We are going to create nested properties on the accumulator object.
settings: require("./windows/settings.tpl"), let tmp = acc;
connect: require("./windows/connect.tpl"),
help: require("./windows/help.tpl"),
changelog: require("./windows/changelog.tpl"),
},
chan: require("./chan.tpl"), // Split path by folders, and create a new property if necessary/
chat: require("./chat.tpl"), // First 2 characters are "./"/
contextmenu_divider: require("./contextmenu_divider.tpl"), // Last element in the array ends with `.tpl` and needs to be `require`d.
contextmenu_item: require("./contextmenu_item.tpl"), path.substr(2).split("/").forEach((key) => {
date_marker: require("./date-marker.tpl"), if (key.endsWith(".tpl")) { //
msg: require("./msg.tpl"), tmp[key.substr(0, key.length - 4)] = requireViews(path);
msg_action: require("./msg_action.tpl"), } else {
msg_condensed_toggle: require("./msg_condensed_toggle.tpl"), tmp[key] = tmp[key] || {};
msg_condensed: require("./msg_condensed.tpl"), }
msg_preview: require("./msg_preview.tpl"), tmp = tmp[key];
msg_preview_toggle: require("./msg_preview_toggle.tpl"), });
msg_unhandled: require("./msg_unhandled.tpl"),
network: require("./network.tpl"), return acc;
image_viewer: require("./image_viewer.tpl"), }, {});
join_channel: require("./join_channel.tpl"),
session: require("./session.tpl"),
unread_marker: require("./unread_marker.tpl"),
user: require("./user.tpl"),
user_filtered: require("./user_filtered.tpl"),
user_name: require("./user_name.tpl"),
version_checker: require("./version_checker.tpl"),
};