From 8d64bf26970d209f6132a538f950c27ffcae96bc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 3 Mar 2022 23:38:59 -0600 Subject: [PATCH] custom: fix import leak --- app/soapbox/custom.js | 11 +++++++++++ app/soapbox/utils/features.js | 13 +++---------- 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 app/soapbox/custom.js diff --git a/app/soapbox/custom.js b/app/soapbox/custom.js new file mode 100644 index 000000000..31f565ed7 --- /dev/null +++ b/app/soapbox/custom.js @@ -0,0 +1,11 @@ +/** + * Functions for dealing with custom build configuration. + */ + +/** Require a custom JSON file if it exists */ +export const custom = (filename, fallback = {}) => { + const context = require.context('custom', false, /\.json$/); + const path = `./${filename}.json`; + + return context.keys().includes(path) ? context(path) : fallback; +}; diff --git a/app/soapbox/utils/features.js b/app/soapbox/utils/features.js index 917653a3a..44621a322 100644 --- a/app/soapbox/utils/features.js +++ b/app/soapbox/utils/features.js @@ -4,19 +4,12 @@ import { createSelector } from 'reselect'; import gte from 'semver/functions/gte'; import lt from 'semver/functions/lt'; -// FIXME: We have to use a dynamic import to trick Webpack into treating it as -// optional, but this causes custom locales to become part of the main chunk. -const importCustom = path => { - try { - return require(`custom/${path}.json`); - } catch(e) { - return {}; - } -}; +import { custom } from 'soapbox/custom'; // Import custom overrides, if exists -const overrides = importCustom('features'); +const overrides = custom('features'); +// Truthy array convenience function const any = arr => arr.some(Boolean); // For uglification