Add webpack aliases.

This commit is contained in:
Strencher 2022-12-29 23:27:52 +01:00
parent 6e93538465
commit 9875068228
2 changed files with 143 additions and 2 deletions

View File

@ -9,6 +9,10 @@ import WebpackModules, {Filters} from "../webpackmodules";
* @name Webpack
*/
const Webpack = {
/**
* A Proxy that returns the module source by ID.
*/
modules: WebpackModules.modules,
/**
* Series of {@link Filters} to be used for finding webpack modules.
@ -60,6 +64,22 @@ const Webpack = {
combine(...filters) {return Filters.combine(...filters);},
},
/**
* Searches for a module by value, returns module & matched key. Useful in combination with the Patcher.
* @param {(value: any, index: number, array: any[]) => boolean} filter A function to use to filter the module
* @param {object} [options] Set of options to customize the search
* @param {any} [options.target=null] Optional module target to look inside.
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters.
* @return {[Any, string]}
*/
getMangled(filter, options = {}) {
if (("first" in options)) return Logger.error("BdApi.Webpack~getModule", "Unsupported option first.");
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 (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected.");
return WebpackModules.getMangled(filter, options);
},
/**
* Finds a module using a filter function.
* @memberof Webpack
@ -105,9 +125,93 @@ const Webpack = {
if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected.");
return WebpackModules.getLazy(filter, options);
},
/**
* Finds all modules matching a filter function.
* @param {Function} filter A function to use to filter modules
*/
getModules(filter) {return WebpackModules.getModule(filter, {first: false});},
/**
* Finds a module using its code.
* @param {RegEx} regex A regular expression to use to filter modules
* @param {object} [options] Options to configure the search
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack exports
* @return {Any}
*/
getByRegex(regex, options = {}) {
return WebpackModules.getModule(Filters.byRegex(regex), options);
},
/**
* Finds akk modules using its code.
* @param {RegEx} regex A regular expression to use to filter modules
* @param {object} [options] Options to configure the search
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack exports
* @return {Any[]}
*/
getAllByRegex(regex, options = {}) {
return WebpackModules.getModule(Filters.byRegex(regex), options);
},
/**
* Finds a single module using properties on its prototype.
* @param {...string} prototypes Properties to use to filter modules
* @return {Any}
*/
getByPrototypes(...prototypes) {
return WebpackModules.getModule(Filters.byPrototypeFields(prototypes));
},
/**
* Finds all modules with a set of properties of its prototype.
* @param {...string} prototypes Properties to use to filter modules
* @return {Any[]}
*/
getAllByPrototypes(...prototypes) {
return WebpackModules.getModule(Filters.byPrototypeFields(prototypes), {first: false});
},
/**
* Finds a single module using its own properties.
* @param {...string} props Properties to use to filter modules
* @return {Any}
*/
getByProps(...props) {
return WebpackModules.getModule(Filters.byProps(props));
},
/**
* Finds all modules with a set of properties.
* @param {...string} props Properties to use to filter modules
* @return {Any[]}
*/
getAllByProps(...props) {
return WebpackModules.getModule(Filters.byProps(props), {first: false});
},
/**
* Finds a single module using a set of strings.
* @param {...String} props Strings to use to filter modules
* @return {Any}
*/
getByString(...strings) {
return WebpackModules.getModule(Filters.byStrings(...strings));
},
/**
* Finds all modules with a set of strings.
* @param {...String} strings Strings to use to filter modules
* @return {Any[]}
*/
getAllByString(...strings) {
return WebpackModules.getModule(Filters.byStrings(...strings), {first: false});
},
};
Object.freeze(Webpack);
Object.freeze(Webpack.Filters);
export default Webpack;
export default Webpack;

View File

@ -140,6 +140,25 @@ export default class WebpackModules {
static findByUniqueProperties(props, first = true) {return first ? this.getByProps(...props) : this.getAllByProps(...props);}
static findByDisplayName(name) {return this.getByDisplayName(name);}
/**
* A Proxy that returns the module source by ID.
*/
static modules = new Proxy({}, {
ownKeys() {return Object.keys(WebpackModules.require.m);},
getOwnPropertyDescriptor() {
return {
enumerable: true,
configurable: true, // Not actually
};
},
get(_, k) {
return WebpackModules.require.m[k];
},
set() {
throw new Error("[WebpackModules~modules] Setting modules is not allowed.");
}
});
/**
* Finds a module using a filter function.
* @param {function} filter A function to use to filter modules
@ -247,6 +266,24 @@ export default class WebpackModules {
return returnedModules;
}
/**
* Searches for a module by value, returns module & matched key. Useful in combination with the Patcher.
* @param {(value: any, index: number, array: any[]) => boolean} filter A function to use to filter the module
* @param {object} [options] Set of options to customize the search
* @param {any} [options.target=null] Optional module target to look inside.
* @param {Boolean} [options.defaultExport=true] Whether to return default export when matching the default export
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters.
* @return {[Any, string]}
*/
static *getMangled(filter, {target = null, ...rest} = {}) {
yield target ??= this.getModule(exports =>
Object.values(exports).some(filter),
rest
);
yield target && Object.keys(target).find(k => filter(target[k]));
}
/**
* Finds all modules matching a filter function.
* @param {Function} filter A function to use to filter modules
@ -481,4 +518,4 @@ export default class WebpackModules {
}
}
WebpackModules.initialize();
WebpackModules.initialize();