Blame view

node_modules/friendly-errors-webpack-plugin/src/formatters/moduleNotFound.js 2.38 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  'use strict';
  const concat = require('../utils').concat;
  
  function isRelative (module) {
    return module.startsWith('./') || module.startsWith('../');
  }
  
  function formatFileList (files) {
    const length = files.length;
    if (!length) return '';
    return ` in ${files[0]}${files[1] ? `, ${files[1]}` : ''}${length > 2 ? ` and ${length - 2} other${length === 3 ? '' : 's'}` : ''}`;
  }
  
  function formatGroup (group) {
    const files = group.errors.map(e => e.file).filter(Boolean);
    return `* ${group.module}${formatFileList(files)}`;
  }
  
  
  function forgetToInstall (missingDependencies) {
    const moduleNames = missingDependencies.map(missingDependency => missingDependency.module);
  
    if (missingDependencies.length === 1) {
      return `To install it, you can run: npm install --save ${moduleNames.join(' ')}`;
    }
  
    return `To install them, you can run: npm install --save ${moduleNames.join(' ')}`;
  }
  
  function dependenciesNotFound (dependencies) {
    if (dependencies.length === 0) return;
  
    return concat(
      dependencies.length === 1 ? 'This dependency was not found:' : 'These dependencies were not found:',
      '',
      dependencies.map(formatGroup),
      '',
      forgetToInstall(dependencies)
    );
  }
  
  function relativeModulesNotFound (modules) {
    if (modules.length === 0) return;
  
    return concat(
      modules.length === 1 ? 'This relative module was not found:' : 'These relative modules were not found:',
      '',
      modules.map(formatGroup)
    );
  }
  
  function groupModules (errors) {
    const missingModule = new Map();
  
    errors.forEach((error) => {
      if (!missingModule.has(error.module)) {
        missingModule.set(error.module, [])
      }
      missingModule.get(error.module).push(error);
    });
  
    return Array.from(missingModule.keys()).map(module => ({
      module: module,
      relative: isRelative(module),
      errors: missingModule.get(module),
    }));
  }
  
  function formatErrors (errors) {
    if (errors.length === 0) {
      return [];
    }
  
    const groups = groupModules(errors);
  
    const dependencies = groups.filter(group => !group.relative);
    const relativeModules = groups.filter(group => group.relative);
  
    return concat(
      dependenciesNotFound(dependencies),
      dependencies.length && relativeModules.length ? ['', ''] : null,
      relativeModulesNotFound(relativeModules)
    );
  }
  
  function format (errors) {
    return formatErrors(errors.filter((e) => (
      e.type === 'module-not-found'
    )));
  }
  
  module.exports = format;