From 9f763b5b79a8e6c61407943615038bafa499bd22 Mon Sep 17 00:00:00 2001 From: Claire Date: Fri, 11 Feb 2022 23:06:37 +0100 Subject: [PATCH] Refactor compile-time glitch-soc theme handling --- config/webpack/configuration.js | 17 +++++------ config/webpack/shared.js | 51 +++++++++++++++------------------ 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/config/webpack/configuration.js b/config/webpack/configuration.js index f05c888d5c..55ee06c0cc 100644 --- a/config/webpack/configuration.js +++ b/config/webpack/configuration.js @@ -21,8 +21,7 @@ const core = function () { return data.pack ? data : {}; }(); -for (let i = 0; i < flavourFiles.length; i++) { - const flavourFile = flavourFiles[i]; +flavourFiles.forEach((flavourFile) => { const data = load(readFileSync(flavourFile), 'utf8'); data.name = basename(dirname(flavourFile)); data.skin = {}; @@ -35,27 +34,25 @@ for (let i = 0; i < flavourFiles.length; i++) { if (data.pack && typeof data.pack === 'object') { flavours[data.name] = data; } -} +}); -for (let i = 0; i < skinFiles.length; i++) { - const skinFile = skinFiles[i]; +skinFiles.forEach((skinFile) => { let skin = basename(skinFile); const name = basename(dirname(skinFile)); if (!flavours[name]) { - continue; + return; } const data = flavours[name].skin; if (lstatSync(skinFile).isDirectory()) { data[skin] = {}; const skinPacks = glob.sync(join(skinFile, '*.{css,scss}')); - for (let j = 0; j < skinPacks.length; j++) { - const pack = skinPacks[j]; + skinPacks.forEach((pack) => { data[skin][basename(pack, extname(pack))] = pack; - } + }); } else if ((skin = skin.match(/^(.*)\.s?css$/i))) { data[skin[1]] = { common: skinFile }; } -} +}); const output = { path: resolve('public', settings.public_output_path), diff --git a/config/webpack/shared.js b/config/webpack/shared.js index ce08ac2065..de347fa888 100644 --- a/config/webpack/shared.js +++ b/config/webpack/shared.js @@ -10,38 +10,33 @@ const rules = require('./rules'); const localePacks = require('./generateLocalePacks'); function reducePacks (data, into = {}) { - if (!data.pack) { - return into; - } - Object.keys(data.pack).reduce((map, entry) => { + if (!data.pack) return into; + + for (const entry in data.pack) { const pack = data.pack[entry]; - if (!pack) { - return map; - } + if (!pack) continue; + const packFile = typeof pack === 'string' ? pack : pack.filename; + if (packFile) { - map[data.name ? `flavours/${data.name}/${entry}` : `core/${entry}`] = resolve(data.pack_directory, packFile); + into[data.name ? `flavours/${data.name}/${entry}` : `core/${entry}`] = resolve(data.pack_directory, packFile); } - return map; - }, into); - if (data.name) { - Object.keys(data.skin).reduce((map, entry) => { - const skin = data.skin[entry]; - const skinName = entry; - if (!skin) { - return map; - } - Object.keys(skin).reduce((map, entry) => { - const packFile = skin[entry]; - if (!packFile) { - return map; - } - map[`skins/${data.name}/${skinName}/${entry}`] = resolve(packFile); - return map; - }, into); - return map; - }, into); } + + if (!data.name) return into; + + for (const skinName in data.skin) { + const skin = data.skin[skinName]; + if (!skin) continue; + + for (const entry in skin) { + const packFile = skin[entry]; + if (!packFile) continue; + + into[`skins/${data.name}/${skinName}/${entry}`] = resolve(packFile); + } + } + return into; } @@ -49,7 +44,7 @@ const entries = Object.assign( { locales: resolve('app', 'javascript', 'locales') }, localePacks, reducePacks(core), - Object.keys(flavours).reduce((map, entry) => reducePacks(flavours[entry], map), {}) + Object.values(flavours).reduce((map, data) => reducePacks(data, map), {}) );