Skip to main content

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 modulePath argument is processed by Babel module resolver; if basePath is 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 regular require(), but invisible to Webpack, hence the module required this way is not bundled into the client side code.

  • Client-side: always returns null.

Changes across 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_SIDE flag to true. Removes throwOnError option.

  • v1.44.10: Turns the second, optional argument into an object with basePath and throwOnError options. In past versions this optional argument was just basePath string, which still works but will be deprecated in future version.

  • v1.40.11:

    • TypeScript: Added Module generic 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');
  • v1.14.0:

    • Safe to call at the client side, and always returns null there.
    • The modulePath argument is automatically processed by Babel module resovler.
    • basePath argument 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.