You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.2 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NullDependency = require("./NullDependency");
  7. class AMDDefineDependency extends NullDependency {
  8. constructor(range, arrayRange, functionRange, objectRange, namedModule) {
  9. super();
  10. this.range = range;
  11. this.arrayRange = arrayRange;
  12. this.functionRange = functionRange;
  13. this.objectRange = objectRange;
  14. this.namedModule = namedModule;
  15. this.localModule = null;
  16. }
  17. get type() {
  18. return "amd define";
  19. }
  20. }
  21. AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
  22. get definitions() {
  23. return {
  24. f: [
  25. "var __WEBPACK_AMD_DEFINE_RESULT__;",
  26. `!(__WEBPACK_AMD_DEFINE_RESULT__ = (#).call(exports, __webpack_require__, exports, module),
  27. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
  28. ],
  29. o: ["", "!(module.exports = #)"],
  30. of: [
  31. "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  32. `!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#),
  33. __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  34. (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, __webpack_require__, exports, module)) :
  35. __WEBPACK_AMD_DEFINE_FACTORY__),
  36. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
  37. ],
  38. af: [
  39. "var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  40. `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = (#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
  41. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
  42. ],
  43. ao: ["", "!(#, module.exports = #)"],
  44. aof: [
  45. "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
  46. `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#),
  47. __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  48. (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
  49. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
  50. ],
  51. lf: [
  52. "var XXX, XXXmodule;",
  53. "!(XXXmodule = { id: YYY, exports: {}, loaded: false }, XXX = #.call(XXXmodule.exports, __webpack_require__, XXXmodule.exports, XXXmodule), XXXmodule.loaded = true, XXX === undefined && (XXX = XXXmodule.exports))"
  54. ],
  55. lo: ["var XXX;", "!(XXX = #)"],
  56. lof: [
  57. "var XXX, XXXfactory, XXXmodule;",
  58. "!(XXXfactory = (#), (XXXmodule = { id: YYY, exports: {}, loaded: false }), XXX = (typeof XXXfactory === 'function' ? (XXXfactory.call(XXXmodule.exports, __webpack_require__, XXXmodule.exports, XXXmodule)) : XXXfactory), (XXXmodule.loaded = true), XXX === undefined && (XXX = XXXmodule.exports))"
  59. ],
  60. laf: [
  61. "var __WEBPACK_AMD_DEFINE_ARRAY__, XXX;",
  62. "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = ((#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)))"
  63. ],
  64. lao: ["var XXX;", "!(#, XXX = #)"],
  65. laof: [
  66. "var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_FACTORY__, XXX;",
  67. `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#),
  68. XXX = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  69. (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__))`
  70. ]
  71. };
  72. }
  73. apply(dependency, source) {
  74. const branch = this.branch(dependency);
  75. const defAndText = this.definitions[branch];
  76. const definitions = defAndText[0];
  77. const text = defAndText[1];
  78. this.replace(dependency, source, definitions, text);
  79. }
  80. localModuleVar(dependency) {
  81. return (
  82. dependency.localModule &&
  83. dependency.localModule.used &&
  84. dependency.localModule.variableName()
  85. );
  86. }
  87. branch(dependency) {
  88. const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
  89. const arrayRange = dependency.arrayRange ? "a" : "";
  90. const objectRange = dependency.objectRange ? "o" : "";
  91. const functionRange = dependency.functionRange ? "f" : "";
  92. return localModuleVar + arrayRange + objectRange + functionRange;
  93. }
  94. replace(dependency, source, definition, text) {
  95. const localModuleVar = this.localModuleVar(dependency);
  96. if (localModuleVar) {
  97. text = text.replace(/XXX/g, localModuleVar.replace(/\$/g, "$$$$"));
  98. definition = definition.replace(
  99. /XXX/g,
  100. localModuleVar.replace(/\$/g, "$$$$")
  101. );
  102. }
  103. if (dependency.namedModule) {
  104. text = text.replace(/YYY/g, JSON.stringify(dependency.namedModule));
  105. }
  106. const texts = text.split("#");
  107. if (definition) source.insert(0, definition);
  108. let current = dependency.range[0];
  109. if (dependency.arrayRange) {
  110. source.replace(current, dependency.arrayRange[0] - 1, texts.shift());
  111. current = dependency.arrayRange[1];
  112. }
  113. if (dependency.objectRange) {
  114. source.replace(current, dependency.objectRange[0] - 1, texts.shift());
  115. current = dependency.objectRange[1];
  116. } else if (dependency.functionRange) {
  117. source.replace(current, dependency.functionRange[0] - 1, texts.shift());
  118. current = dependency.functionRange[1];
  119. }
  120. source.replace(current, dependency.range[1] - 1, texts.shift());
  121. if (texts.length > 0) throw new Error("Implementation error");
  122. }
  123. };
  124. module.exports = AMDDefineDependency;