webpack
import { webpack } from '@dr.pogodin/react-utils';
Encapsulates Webpack-related utilities.
Functions
- requireWeak() - Requires a module at server side, and prevents it from being bundled by Webpack.
- resolveWeak() - Resolves a module path with the help of
Babel module resolver, thus the same way the resolution works for the normal
require().
Functions
requireWeak()
import { webpack } from '@dr.pogodin/react-utils';
type RequireWeakOptionsT = {
basePath?: string;
};
webpack.requireWeak<Module extends NodeJS.Module>(
modulePath: string,
basePathOrOptions?: RequireWeakOptionsT | string,
): Module | null;
Implements server-side loading of the specified JS module in the way which prevents it from being bundled into the client-side code by Webpack.
-
Server-side: the
modulePathargument is processed by Babel module resolver; ifbasePathis given, the resulting module path is additionally resolved from the base path using path.resolve(); the module is then loaded from the resulting path, and returned, similar to the regularrequire(), but invisible to Webpack, hence the module required this way is not bundled into the client side code. -
Client-side: always returns null.
dr.pogodin/react-utils versions:-
v1.46.0: On server side it now always throws an error in case of the module loading failure. Environments where it causes problems should set the global
REACT_UTILS_FORCE_CLIENT_SIDEflag to true. RemovesthrowOnErroroption. -
v1.44.10: Turns the second, optional argument into an object with
basePathandthrowOnErroroptions. In past versions this optional argument was justbasePathstring, which still works but will be deprecated in future version. -
v1.40.11:
- TypeScript: Added
Modulegeneric argument for typing of the result. You want to use it like:import type M from 'some-module';
const m = webpack.requireWeak<typeof M>('some-module');
- TypeScript: Added
-
v1.14.0:
- Safe to call at the client side, and always returns null there.
- The
modulePathargument is automatically processed by Babel module resovler. basePathargument introduced.
resolveWeak()
webpack.resolveWeak(modulePath): string
Resolves modulePath module path with the help of Babel module resolver,
thus the same way the resolution works for the normal require().
Arguments & Result
modulePath- string - Module path.- Returns string - The resolved relative path to the module.