UNPKG

@dr.pogodin/react-utils

Version:

Collection of generic ReactJS components and utils

65 lines (62 loc) 2 kB
// eslint-disable-next-line import/enforce-node-protocol-usage import { createRequire } from 'module'; import { IS_CLIENT_SIDE, IS_SERVER_SIDE } from "./isomorphy/index.js"; let require; if (IS_SERVER_SIDE) require = createRequire(import.meta.url); /** * Requires the specified module without including it into the bundle during * Webpack build. * @param modulePath * @param [basePath] * @return Required module. */ export function requireWeak(modulePath, // TODO: For now `basePath` can be provided directly as a string here, // for backward compatibility. Deprecate it in future, if any other // breaking changes are done for requireWeak(). basePathOrOptions) { if (IS_CLIENT_SIDE) return null; let basePath; let ops; if (typeof basePathOrOptions === 'string') { basePath = basePathOrOptions; } else { ops = basePathOrOptions ?? {}; ({ basePath } = ops); } // eslint-disable-next-line @typescript-eslint/unbound-method const { resolve } = require('node:path'); const path = basePath ? resolve(basePath, modulePath) : modulePath; const module = require(path); if (!('default' in module) || !module.default) return module; const { default: def, ...named } = module; const res = def; Object.entries(named).forEach(([name, value]) => { const assigned = res[name]; if (assigned === undefined) { res[name] = value; } else if (assigned !== value) { throw Error('Conflict between default and named exports'); } }); return res; } /** * Resolves specified module path with help of Babel's module resolver. * Yes, the function itself just returns its argument to the caller, but Babel * is configured to resolve the first argument of resolveWeak(..) function, thus * the result will be the resolved path. * @param {string} modulePath * @return {string} Absolute or relative path to the module. */ export function resolveWeak(modulePath) { return modulePath; } //# sourceMappingURL=webpack.js.map