ensure-require.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const throwError = require('./throw-error')
  2. module.exports = function (name, deps) {
  3. var i, len
  4. var missing = []
  5. if (typeof deps === 'string') {
  6. deps = [deps]
  7. }
  8. for (i = 0, len = deps.length; i < len; i++) {
  9. var mis
  10. var req = deps[i]
  11. if (typeof req === 'string') {
  12. mis = req
  13. } else {
  14. mis = req[1]
  15. req = req[0]
  16. }
  17. try {
  18. // hack for babel-runtime because it does not expose "main" field
  19. if (req === 'babel-runtime') {
  20. req = 'babel-runtime/core-js'
  21. }
  22. require.resolve(req)
  23. } catch (e) {
  24. missing.push(mis)
  25. }
  26. }
  27. if (missing.length > 0) {
  28. var message = 'You are trying to use "' + name + '". '
  29. var npmInstall = 'npm install --save-dev ' + missing.join(' ')
  30. if (missing.length > 1) {
  31. var last = missing.pop()
  32. message += missing.join(', ') + ' and ' + last + ' are '
  33. } else {
  34. message += missing[0] + ' is '
  35. }
  36. message += 'missing.\n\nTo install run:\n' + npmInstall
  37. throwError(message)
  38. }
  39. }