resolver.mjs 801 B

123456789101112131415161718192021
  1. import resolveCb from 'enhanced-resolve';
  2. import { promisify } from 'util';
  3. import { CliExpectedError } from './error.mjs';
  4. export const commonJsResolve = promisify(resolveCb);
  5. /**
  6. * Resolves the module in context of the configuration.
  7. *
  8. * Only does traditional Node resolution without looking at the `exports` field
  9. * or alternative extensions (cjs/mjs) to match what the VS Code loader does.
  10. */
  11. export const mustResolve = async (context, moduleName) => {
  12. const path = await commonJsResolve(context, moduleName);
  13. if (!path) {
  14. let msg = `Could not resolve module "${moduleName}" in ${path}`;
  15. if (!moduleName.startsWith('.')) {
  16. msg += ' (you may need to install with `npm install`)';
  17. }
  18. throw new CliExpectedError(msg);
  19. }
  20. return path;
  21. };