Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AliasPlugin.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. function startsWith(string, searchString) {
  7. const stringLength = string.length;
  8. const searchLength = searchString.length;
  9. // early out if the search length is greater than the search string
  10. if(searchLength > stringLength) {
  11. return false;
  12. }
  13. let index = -1;
  14. while(++index < searchLength) {
  15. if(string.charCodeAt(index) !== searchString.charCodeAt(index)) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. module.exports = class AliasPlugin {
  22. constructor(source, options, target) {
  23. this.source = source;
  24. this.options = Array.isArray(options) ? options : [options];
  25. this.target = target;
  26. }
  27. apply(resolver) {
  28. const target = resolver.ensureHook(this.target);
  29. resolver.getHook(this.source).tapAsync("AliasPlugin", (request, resolveContext, callback) => {
  30. const innerRequest = request.request || request.path;
  31. if(!innerRequest) return callback();
  32. for(const item of this.options) {
  33. if(innerRequest === item.name || (!item.onlyModule && startsWith(innerRequest, item.name + "/"))) {
  34. if(innerRequest !== item.alias && !startsWith(innerRequest, item.alias + "/")) {
  35. const newRequestStr = item.alias + innerRequest.substr(item.name.length);
  36. const obj = Object.assign({}, request, {
  37. request: newRequestStr
  38. });
  39. return resolver.doResolve(target, obj, "aliased with mapping '" + item.name + "': '" + item.alias + "' to '" + newRequestStr + "'", resolveContext, (err, result) => {
  40. if(err) return callback(err);
  41. // Don't allow other aliasing or raw request
  42. if(result === undefined) return callback(null, null);
  43. callback(null, result);
  44. });
  45. }
  46. }
  47. }
  48. return callback();
  49. });
  50. }
  51. };