ImportDependency.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 DepBlockHelpers = require("./DepBlockHelpers");
  8. const webpackMissingPromiseModule = require("./WebpackMissingModule").promise;
  9. class ImportDependency extends ModuleDependency {
  10. constructor(request, block) {
  11. super(request);
  12. this.block = block;
  13. }
  14. get type() {
  15. return "import()";
  16. }
  17. }
  18. ImportDependency.Template = class ImportDependencyTemplate {
  19. apply(dep, source, outputOptions, requestShortener) {
  20. const depBlock = dep.block;
  21. const promise = DepBlockHelpers.getDepBlockPromise(depBlock, outputOptions, requestShortener, "import()");
  22. const comment = this.getOptionalComment(outputOptions.pathinfo, requestShortener.shorten(dep.request));
  23. const content = this.getContent(promise, dep, comment);
  24. source.replace(depBlock.range[0], depBlock.range[1] - 1, content);
  25. }
  26. getOptionalComment(pathinfo, shortenedRequest) {
  27. if(!pathinfo) {
  28. return "";
  29. }
  30. return `/*! ${shortenedRequest} */ `;
  31. }
  32. getContent(promise, dep, comment) {
  33. if(promise && dep.module) {
  34. const stringifiedId = JSON.stringify(dep.module.id);
  35. return `${promise}.then(__webpack_require__.bind(null, ${comment}${stringifiedId}))`;
  36. }
  37. if(dep.module) {
  38. const stringifiedId = JSON.stringify(dep.module.id);
  39. return `new Promise(function(resolve) { resolve(__webpack_require__(${comment}${stringifiedId})); })`;
  40. }
  41. return webpackMissingPromiseModule(dep.request);
  42. }
  43. };
  44. module.exports = ImportDependency;