ImportEagerDependency.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ModuleDependency = require("./ModuleDependency");
  7. const webpackMissingPromiseModule = require("./WebpackMissingModule").promise;
  8. class ImportEagerDependency extends ModuleDependency {
  9. constructor(request, range) {
  10. super(request);
  11. this.range = range;
  12. }
  13. get type() {
  14. return "import()";
  15. }
  16. }
  17. ImportEagerDependency.Template = class ImportEagerDependencyTemplate {
  18. apply(dep, source, outputOptions, requestShortener) {
  19. const comment = this.getOptionalComment(outputOptions.pathinfo, requestShortener.shorten(dep.request));
  20. const content = this.getContent(dep, comment);
  21. source.replace(dep.range[0], dep.range[1] - 1, content);
  22. }
  23. getOptionalComment(pathinfo, shortenedRequest) {
  24. if(!pathinfo) {
  25. return "";
  26. }
  27. return `/*! ${shortenedRequest} */ `;
  28. }
  29. getContent(dep, comment) {
  30. if(dep.module) {
  31. const stringifiedId = JSON.stringify(dep.module.id);
  32. return `new Promise(function(resolve) { resolve(__webpack_require__(${comment}${stringifiedId})); })`;
  33. }
  34. return webpackMissingPromiseModule(dep.request);
  35. }
  36. };
  37. module.exports = ImportEagerDependency;