RequireEnsureDependency.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. const DepBlockHelpers = require("./DepBlockHelpers");
  8. class RequireEnsureDependency extends NullDependency {
  9. constructor(block) {
  10. super();
  11. this.block = block;
  12. }
  13. get type() {
  14. return "require.ensure";
  15. }
  16. }
  17. RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate {
  18. apply(dep, source, outputOptions, requestShortener) {
  19. const depBlock = dep.block;
  20. const wrapper = DepBlockHelpers.getLoadDepBlockWrapper(depBlock, outputOptions, requestShortener, "require.ensure");
  21. const errorCallbackExists = depBlock.expr.arguments.length === 4 || (!depBlock.chunkName && depBlock.expr.arguments.length === 3);
  22. const startBlock = wrapper[0] + "(";
  23. const middleBlock = `).bind(null, __webpack_require__)${wrapper[1]}`;
  24. const endBlock = `${middleBlock}__webpack_require__.oe${wrapper[2]}`;
  25. source.replace(depBlock.expr.range[0], depBlock.expr.arguments[1].range[0] - 1, startBlock);
  26. if(errorCallbackExists) {
  27. source.replace(depBlock.expr.arguments[1].range[1], depBlock.expr.arguments[2].range[0] - 1, middleBlock);
  28. source.replace(depBlock.expr.arguments[2].range[1], depBlock.expr.range[1] - 1, wrapper[2]);
  29. } else {
  30. source.replace(depBlock.expr.arguments[1].range[1], depBlock.expr.range[1] - 1, endBlock);
  31. }
  32. }
  33. };
  34. module.exports = RequireEnsureDependency;