node.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const env = require('./env.js');
  3. /**
  4. * NOTE: In order to avoid circular dependencies, if you add a function to this module and it needs to print something,
  5. * you must either a) use `console.log` rather than the logger, or b) put your function elsewhere.
  6. */
  7. /**
  8. * Checks whether we're in the Node.js or Browser environment
  9. *
  10. * @returns Answer to given question
  11. */
  12. function isNodeEnv() {
  13. // explicitly check for browser bundles as those can be optimized statically
  14. // by terser/rollup.
  15. return (
  16. !env.isBrowserBundle() &&
  17. Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'
  18. );
  19. }
  20. /**
  21. * Requires a module which is protected against bundler minification.
  22. *
  23. * @param request The module path to resolve
  24. */
  25. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  26. function dynamicRequire(mod, request) {
  27. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  28. return mod.require(request);
  29. }
  30. /**
  31. * Helper for dynamically loading module that should work with linked dependencies.
  32. * The problem is that we _should_ be using `require(require.resolve(moduleName, { paths: [cwd()] }))`
  33. * However it's _not possible_ to do that with Webpack, as it has to know all the dependencies during
  34. * build time. `require.resolve` is also not available in any other way, so we cannot create,
  35. * a fake helper like we do with `dynamicRequire`.
  36. *
  37. * We always prefer to use local package, thus the value is not returned early from each `try/catch` block.
  38. * That is to mimic the behavior of `require.resolve` exactly.
  39. *
  40. * @param moduleName module name to require
  41. * @returns possibly required module
  42. */
  43. function loadModule(moduleName) {
  44. let mod;
  45. try {
  46. mod = dynamicRequire(module, moduleName);
  47. } catch (e) {
  48. // no-empty
  49. }
  50. try {
  51. const { cwd } = dynamicRequire(module, 'process');
  52. mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) ;
  53. } catch (e) {
  54. // no-empty
  55. }
  56. return mod;
  57. }
  58. exports.dynamicRequire = dynamicRequire;
  59. exports.isNodeEnv = isNodeEnv;
  60. exports.loadModule = loadModule;
  61. //# sourceMappingURL=node.js.map