Fix getModule & Add searchGetters option

Co-authored-by: Zerebos <rauenzi@outlook.com>
This commit is contained in:
Strencher 2022-09-30 02:05:19 +02:00
parent f41b071727
commit e63695e17f
2 changed files with 19 additions and 9 deletions

View File

@ -640,11 +640,14 @@ BdApi.Webpack = {
* @param {object} [options] Options object to configure the search
* @param {Boolean} [options.first=true] Whether to return only the first matching module
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchGetters=true] Whether to execute the filter on webpack export getters.
* @return {any}
*/
getModule(filter, options = {}) {
if (("first" in options) && typeof(options.first) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.first", options.first, "boolean expected.");
if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected.");
if (("searchGetters" in options) && typeof(options.searchGetters) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchGetters", options.searchGetters, "boolean expected.");
return WebpackModules.getModule(filter, options);
},
@ -655,6 +658,7 @@ BdApi.Webpack = {
* @param {Function} queries.filter A function to use to filter modules
* @param {Boolean} [queries.first=true] Whether to return only the first matching module
* @param {Boolean} [queries.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [queries.searchGetters=true] Whether to execute the filter on webpack export getters.
* @return {any}
*/
getBulk(...queries) {return WebpackModules.getBulk(...queries);},
@ -665,11 +669,14 @@ BdApi.Webpack = {
* @param {object} [options] Options object to configure the listener
* @param {AbortSignal} [options.signal] AbortSignal of an AbortController to cancel the promise
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchGetters=true] Whether to execute the filter on webpack export getters.
* @returns {Promise<any>}
*/
waitForModule(filter, options = {}) {
if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected.");
if (("signal" in options) && !(options.signal instanceof AbortSignal)) return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.signal", options.signal, "AbortSignal expected.");
if (("searchGetters" in options) && typeof(options.searchGetters) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchGetters", options.searchGetters, "boolean expected.");
return WebpackModules.getLazy(filter, options);
},
};

View File

@ -128,10 +128,11 @@ export default class WebpackModules {
* @param {object} [options] Set of options to customize the search
* @param {Boolean} [options.first=true] Whether to return only the first matching module
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchGetters=true] Whether to execute the filter on webpack export getters.
* @return {Any}
*/
static getModule(filter, options = {}) {
const {first = true, defaultExport = true} = options;
const {first = true, defaultExport = true, searchGetters = true} = options;
const wrappedFilter = (exports, module, moduleId) => {
try {
if (exports?.default?.remove && exports?.default?.set && exports?.default?.clear && exports?.default?.get && !exports?.default?.sort) return false;
@ -161,8 +162,9 @@ export default class WebpackModules {
if (typeof(exports) === "object") {
const wrappers = Object.getOwnPropertyDescriptors(exports);
const getters = Object.keys(wrappers).filter(k => wrappers[k].get);
if (getters.length) {
if (getters.length && searchGetters) {
for (const getter of getters) {
foundModule = null;
const wrappedExport = exports[getter];
if (!wrappedExport) continue;
if (wrappedExport.__esModule && wrappedExport.default && wrappedFilter(wrappedExport.default, module, index)) foundModule = defaultExport ? wrappedExport.default : wrappedExport;
@ -170,7 +172,6 @@ export default class WebpackModules {
if (!foundModule) continue;
if (first) return foundModule;
rm.push(foundModule);
foundModule = null;
}
}
else {
@ -204,6 +205,7 @@ export default class WebpackModules {
* @param {Function} queries.filter A function to use to filter modules
* @param {Boolean} [queries.first=true] Whether to return only the first matching module
* @param {Boolean} [queries.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [queries.searchGetters=true] Whether to execute the filter on webpack export getters.
* @return {Any}
*/
static getBulk(...queries) {
@ -219,7 +221,7 @@ export default class WebpackModules {
for (let q = 0; q < queries.length; q++) {
const query = queries[q];
const {filter, first = true, defaultExport = true} = query;
const {filter, first = true, defaultExport = true, searchGetters = true} = query;
if (first && returnedModules[q]) continue; // If they only want the first, and we already found it, move on
if (!first && !returnedModules[q]) returnedModules[q] = []; // If they want multiple and we haven't setup the subarry, do it now
@ -238,8 +240,9 @@ export default class WebpackModules {
if (typeof(exports) === "object") {
const wrappers = Object.getOwnPropertyDescriptors(exports);
const getters = Object.keys(wrappers).filter(k => wrappers[k].get);
if (getters.length) {
if (getters.length && searchGetters) {
for (const getter of getters) {
foundModule = null;
const wrappedExport = exports[getter];
if (!wrappedExport) continue;
if (wrappedExport.__esModule && wrappedExport.default && wrappedFilter(wrappedExport.default, module, index)) foundModule = defaultExport ? wrappedExport.default : wrappedExport;
@ -356,12 +359,11 @@ export default class WebpackModules {
* @param {object} [options] Set of options to customize the search
* @param {AbortSignal} [options.signal] AbortSignal of an AbortController to cancel the promise
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchGetters=true] Whether to execute the filter on webpack export getters.
* @returns {Promise<any>}
*/
static getLazy(filter, options = {}) {
/** @type {AbortSignal} */
const abortSignal = options.signal;
const defaultExport = options.defaultExport ?? true;
const {signal: abortSignal, defaultExport = true, searchGetters = true} = options;
const fromCache = this.getModule(filter);
if (fromCache) return Promise.resolve(fromCache);
@ -385,8 +387,9 @@ export default class WebpackModules {
if (typeof(exports) === "object") {
const wrappers = Object.getOwnPropertyDescriptors(exports);
const getters = Object.keys(wrappers).filter(k => wrappers[k].get);
if (getters.length) {
if (getters.length && searchGetters) {
for (const getter of getters) {
foundModule = null;
const wrappedExport = exports[getter];
if (!wrappedExport) continue;
if (wrappedExport.__esModule && wrappedExport.default && wrappedFilter(wrappedExport.default)) foundModule = defaultExport ? wrappedExport.default : wrappedExport;