FunctionModuleTemplatePlugin.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConcatSource = require("webpack-sources").ConcatSource;
  7. class FunctionModuleTemplatePlugin {
  8. apply(moduleTemplate) {
  9. moduleTemplate.plugin("render", function(moduleSource, module) {
  10. const source = new ConcatSource();
  11. const defaultArguments = [module.moduleArgument || "module", module.exportsArgument || "exports"];
  12. if((module.arguments && module.arguments.length !== 0) || module.hasDependencies(d => d.requireWebpackRequire !== false)) {
  13. defaultArguments.push("__webpack_require__");
  14. }
  15. source.add("/***/ (function(" + defaultArguments.concat(module.arguments || []).join(", ") + ") {\n\n");
  16. if(module.strict) source.add("\"use strict\";\n");
  17. source.add(moduleSource);
  18. source.add("\n\n/***/ })");
  19. return source;
  20. });
  21. moduleTemplate.plugin("package", function(moduleSource, module) {
  22. if(this.outputOptions.pathinfo) {
  23. const source = new ConcatSource();
  24. const req = module.readableIdentifier(this.requestShortener);
  25. source.add("/*!****" + req.replace(/./g, "*") + "****!*\\\n");
  26. source.add(" !*** " + req.replace(/\*\//g, "*_/") + " ***!\n");
  27. source.add(" \\****" + req.replace(/./g, "*") + "****/\n");
  28. if(Array.isArray(module.providedExports) && module.providedExports.length === 0)
  29. source.add("/*! no exports provided */\n");
  30. else if(Array.isArray(module.providedExports))
  31. source.add("/*! exports provided: " + module.providedExports.join(", ") + " */\n");
  32. else if(module.providedExports)
  33. source.add("/*! dynamic exports provided */\n");
  34. if(Array.isArray(module.usedExports) && module.usedExports.length === 0)
  35. source.add("/*! no exports used */\n");
  36. else if(Array.isArray(module.usedExports))
  37. source.add("/*! exports used: " + module.usedExports.join(", ") + " */\n");
  38. else if(module.usedExports)
  39. source.add("/*! all exports used */\n");
  40. if(module.optimizationBailout) {
  41. module.optimizationBailout.forEach(text => {
  42. if(typeof text === "function") text = text(this.requestShortener);
  43. source.add(`/*! ${text} */\n`);
  44. });
  45. }
  46. source.add(moduleSource);
  47. return source;
  48. }
  49. return moduleSource;
  50. });
  51. moduleTemplate.plugin("hash", function(hash) {
  52. hash.update("FunctionModuleTemplatePlugin");
  53. hash.update("2");
  54. });
  55. }
  56. }
  57. module.exports = FunctionModuleTemplatePlugin;