ImportWeakDependency.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 ImportWeakDependency extends ModuleDependency {
  9. constructor(request, range) {
  10. super(request);
  11. this.range = range;
  12. this.weak = true;
  13. }
  14. get type() {
  15. return "import() weak";
  16. }
  17. }
  18. ImportWeakDependency.Template = class ImportDependencyTemplate {
  19. apply(dep, source, outputOptions, requestShortener) {
  20. const comment = this.getOptionalComment(outputOptions.pathinfo, requestShortener.shorten(dep.request));
  21. const content = this.getContent(dep, comment);
  22. source.replace(dep.range[0], dep.range[1] - 1, content);
  23. }
  24. getOptionalComment(pathinfo, shortenedRequest) {
  25. if(!pathinfo) {
  26. return "";
  27. }
  28. return `/*! ${shortenedRequest} */ `;
  29. }
  30. getContent(dep, comment) {
  31. if(dep.module) {
  32. const stringifiedId = JSON.stringify(dep.module.id);
  33. return `Promise.resolve(${comment}${stringifiedId}).then(function(id) { if(!__webpack_require__.m[id]) throw new Error("Module '" + id + "' is not available (weak dependency)"); return __webpack_require__(id); })`;
  34. }
  35. return webpackMissingPromiseModule(dep.request);
  36. }
  37. };
  38. module.exports = ImportWeakDependency;