You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FunctionModuleTemplatePlugin.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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");
  7. const Template = require("./Template");
  8. class FunctionModuleTemplatePlugin {
  9. apply(moduleTemplate) {
  10. moduleTemplate.hooks.render.tap(
  11. "FunctionModuleTemplatePlugin",
  12. (moduleSource, module) => {
  13. const source = new ConcatSource();
  14. const args = [module.moduleArgument];
  15. // TODO remove HACK checking type for javascript
  16. if (module.type && module.type.startsWith("javascript")) {
  17. args.push(module.exportsArgument);
  18. if (module.hasDependencies(d => d.requireWebpackRequire !== false)) {
  19. args.push("__webpack_require__");
  20. }
  21. } else if (module.type && module.type.startsWith("json")) {
  22. // no additional arguments needed
  23. } else {
  24. args.push(module.exportsArgument, "__webpack_require__");
  25. }
  26. source.add("/***/ (function(" + args.join(", ") + ") {\n\n");
  27. if (module.buildInfo.strict) source.add('"use strict";\n');
  28. source.add(moduleSource);
  29. source.add("\n\n/***/ })");
  30. return source;
  31. }
  32. );
  33. moduleTemplate.hooks.package.tap(
  34. "FunctionModuleTemplatePlugin",
  35. (moduleSource, module) => {
  36. if (moduleTemplate.runtimeTemplate.outputOptions.pathinfo) {
  37. const source = new ConcatSource();
  38. const req = module.readableIdentifier(
  39. moduleTemplate.runtimeTemplate.requestShortener
  40. );
  41. const reqStr = req.replace(/\*\//g, "*_/");
  42. const reqStrStar = "*".repeat(reqStr.length);
  43. source.add("/*!****" + reqStrStar + "****!*\\\n");
  44. source.add(" !*** " + reqStr + " ***!\n");
  45. source.add(" \\****" + reqStrStar + "****/\n");
  46. if (
  47. Array.isArray(module.buildMeta.providedExports) &&
  48. module.buildMeta.providedExports.length === 0
  49. ) {
  50. source.add(Template.toComment("no exports provided") + "\n");
  51. } else if (Array.isArray(module.buildMeta.providedExports)) {
  52. source.add(
  53. Template.toComment(
  54. "exports provided: " +
  55. module.buildMeta.providedExports.join(", ")
  56. ) + "\n"
  57. );
  58. } else if (module.buildMeta.providedExports) {
  59. source.add(Template.toComment("no static exports found") + "\n");
  60. }
  61. if (
  62. Array.isArray(module.usedExports) &&
  63. module.usedExports.length === 0
  64. ) {
  65. source.add(Template.toComment("no exports used") + "\n");
  66. } else if (Array.isArray(module.usedExports)) {
  67. source.add(
  68. Template.toComment(
  69. "exports used: " + module.usedExports.join(", ")
  70. ) + "\n"
  71. );
  72. } else if (module.usedExports) {
  73. source.add(Template.toComment("all exports used") + "\n");
  74. }
  75. if (module.optimizationBailout) {
  76. for (const text of module.optimizationBailout) {
  77. let code;
  78. if (typeof text === "function") {
  79. code = text(moduleTemplate.runtimeTemplate.requestShortener);
  80. } else {
  81. code = text;
  82. }
  83. source.add(Template.toComment(`${code}`) + "\n");
  84. }
  85. }
  86. source.add(moduleSource);
  87. return source;
  88. }
  89. return moduleSource;
  90. }
  91. );
  92. moduleTemplate.hooks.hash.tap("FunctionModuleTemplatePlugin", hash => {
  93. hash.update("FunctionModuleTemplatePlugin");
  94. hash.update("2");
  95. });
  96. }
  97. }
  98. module.exports = FunctionModuleTemplatePlugin;