relate-context.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const path = require('path');
  2. const rootCompiler = compiler => {
  3. while (compiler.parentCompilation) {
  4. compiler = compiler.parentCompilation.compiler;
  5. }
  6. return compiler;
  7. };
  8. const compilerContext = (exports.compilerContext = function compilerContext(
  9. compiler,
  10. ) {
  11. return rootCompiler(compiler.compiler ? compiler.compiler : compiler).context;
  12. });
  13. const relateNormalPath = (exports.relateNormalPath = function relateNormalPath(
  14. compiler,
  15. key,
  16. ) {
  17. if (typeof key !== 'string') {
  18. return key;
  19. }
  20. if (compilerContext(compiler) === key) {
  21. return '.';
  22. }
  23. if (key === '') {
  24. return key;
  25. }
  26. const rel = path.relative(compilerContext(compiler), key.split('?')[0]);
  27. return [rel.replace(/\\/g, '/')].concat(key.split('?').slice(1)).join('?');
  28. });
  29. const relateNormalRequest = (exports.relateNormalRequest = function relateNormalRequest(
  30. compiler,
  31. key,
  32. ) {
  33. return key
  34. .split('!')
  35. .map(subkey => relateNormalPath(compiler, subkey))
  36. .join('!');
  37. });
  38. const relateNormalModuleId = (exports.relateNormalModuleId = function relateNormalModuleId(
  39. compiler,
  40. id,
  41. ) {
  42. return id.substring(0, 24) + relateNormalRequest(compiler, id.substring(24));
  43. });
  44. const relateNormalLoaders = (exports.relateNormalLoaders = function relateNormalLoaders(
  45. compiler,
  46. loaders,
  47. ) {
  48. return loaders.map(loader =>
  49. Object.assign({}, loader, {
  50. loader: relateNormalPath(compiler, loader.loader),
  51. }),
  52. );
  53. });
  54. const relateNormalPathArray = (exports.relateNormalPathArray = function relateNormalPathArray(
  55. compiler,
  56. paths,
  57. ) {
  58. return paths.map(subpath => relateNormalPath(compiler, subpath));
  59. });
  60. const relateNormalPathSet = (exports.relateNormalPathSet = function relateNormalPathSet(
  61. compiler,
  62. paths,
  63. ) {
  64. return relateNormalPathArray(compiler, Array.from(paths));
  65. });
  66. /**
  67. *
  68. */
  69. // Cache whether we need to replace path.sep because contextNormalPath is called _very_ frequently
  70. const resolveRelativeCompilerContext =
  71. '/' === path.sep
  72. ? function(context, key) {
  73. return path.resolve(context, key);
  74. }
  75. : function(context, key) {
  76. return path.resolve(context, key).replace(/\//g, path.sep);
  77. };
  78. const contextNormalPath = (exports.contextNormalPath = function contextNormalPath(
  79. compiler,
  80. key,
  81. ) {
  82. if (typeof key !== 'string' || key === '') {
  83. return key;
  84. }
  85. const context = compilerContext(compiler);
  86. if (key === '.') {
  87. return context;
  88. }
  89. const markIndex = key.indexOf('?');
  90. if (markIndex === -1) {
  91. return resolveRelativeCompilerContext(context, key);
  92. }
  93. const abs = resolveRelativeCompilerContext(
  94. context,
  95. key.substring(0, markIndex),
  96. );
  97. return abs + '?' + key.substring(markIndex + 1);
  98. });
  99. const contextNormalRequest = (exports.contextNormalRequest = function contextNormalRequest(
  100. compiler,
  101. key,
  102. ) {
  103. // return key
  104. // .split('!')
  105. // .map(subkey => contextNormalPath(compiler, subkey))
  106. // .join('!');
  107. let i = -1;
  108. let j = -1;
  109. let _newkey = '';
  110. while ((i = key.indexOf('!', i + 1)) !== -1) {
  111. _newkey += contextNormalPath(compiler, key.substring(j + 1, i));
  112. _newkey += '!';
  113. j = i;
  114. }
  115. _newkey += contextNormalPath(compiler, key.substring(j + 1));
  116. return _newkey;
  117. });
  118. const contextNormalModuleId = (exports.contextNormalModuleId = function contextNormalModuleId(
  119. compiler,
  120. id,
  121. ) {
  122. return id.substring(0, 24) + contextNormalRequest(compiler, id.substring(24));
  123. });
  124. const contextNormalLoaders = (exports.contextNormalLoaders = function contextNormalLoaders(
  125. compiler,
  126. loaders,
  127. ) {
  128. return loaders.map(loader =>
  129. Object.assign({}, loader, {
  130. loader: contextNormalPath(compiler, loader.loader),
  131. }),
  132. );
  133. });
  134. const contextNormalPathArray = (exports.contextNormalPathArray = function contextNormalPathArray(
  135. compiler,
  136. paths,
  137. ) {
  138. return paths.map(subpath => contextNormalPath(compiler, subpath));
  139. });
  140. const contextNormalPathSet = (exports.contextNormalPathSet = function contextNormalPathSet(
  141. compiler,
  142. paths,
  143. ) {
  144. return new Set(contextNormalPathArray(compiler, paths));
  145. });
  146. /**
  147. *
  148. */
  149. const maybeAbsolutePath = (exports.maybeAbsolutePath = function maybeAbsolutePath(
  150. path,
  151. ) {
  152. return /^([a-zA-Z]:\\\\|\/)/.test(path);
  153. });
  154. const relateAbsolutePath = (exports.relateAbsolutePath = function relateAbsolutePath(
  155. context,
  156. absPath,
  157. ) {
  158. if (maybeAbsolutePath(absPath)) {
  159. return path.relative(context, absPath);
  160. }
  161. return absPath;
  162. });
  163. const relateAbsoluteRequest = (exports.relateAbsoluteRequest = function relateAbsoluteRequest(
  164. context,
  165. absReq,
  166. ) {
  167. return absReq
  168. .split(/!/g)
  169. .map(path => relateAbsolutePath(context, path))
  170. .join('!');
  171. });