EvalDevToolModuleTemplatePlugin.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RawSource = require("webpack-sources").RawSource;
  7. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  8. class EvalDevToolModuleTemplatePlugin {
  9. constructor(sourceUrlComment, moduleFilenameTemplate) {
  10. this.sourceUrlComment = sourceUrlComment || "\n//# sourceURL=[url]";
  11. this.moduleFilenameTemplate = moduleFilenameTemplate || "webpack:///[resourcePath]?[loaders]";
  12. }
  13. apply(moduleTemplate) {
  14. moduleTemplate.plugin("module", (source, module) => {
  15. const content = source.source();
  16. const str = ModuleFilenameHelpers.createFilename(module, this.moduleFilenameTemplate, moduleTemplate.requestShortener);
  17. const footer = ["\n",
  18. ModuleFilenameHelpers.createFooter(module, moduleTemplate.requestShortener),
  19. this.sourceUrlComment.replace(/\[url\]/g, encodeURI(str).replace(/%2F/g, "/").replace(/%20/g, "_").replace(/%5E/g, "^").replace(/%5C/g, "\\").replace(/^\//, ""))
  20. ].join("\n");
  21. return new RawSource(`eval(${JSON.stringify(content + footer)});`);
  22. });
  23. moduleTemplate.plugin("hash", hash => {
  24. hash.update("EvalDevToolModuleTemplatePlugin");
  25. hash.update("2");
  26. });
  27. }
  28. }
  29. module.exports = EvalDevToolModuleTemplatePlugin;