EvalSourceMapDevToolModuleTemplatePlugin.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 EvalSourceMapDevToolModuleTemplatePlugin {
  9. constructor(compilation, options) {
  10. this.compilation = compilation;
  11. this.sourceMapComment = options.append || "//# sourceURL=[module]\n//# sourceMappingURL=[url]";
  12. this.moduleFilenameTemplate = options.moduleFilenameTemplate || "webpack:///[resource-path]?[hash]";
  13. this.options = options;
  14. }
  15. apply(moduleTemplate) {
  16. const self = this;
  17. const options = this.options;
  18. moduleTemplate.plugin("module", function(source, module) {
  19. if(source.__EvalSourceMapDevToolData)
  20. return source.__EvalSourceMapDevToolData;
  21. let sourceMap;
  22. let content;
  23. if(source.sourceAndMap) {
  24. const sourceAndMap = source.sourceAndMap(options);
  25. sourceMap = sourceAndMap.map;
  26. content = sourceAndMap.source;
  27. } else {
  28. sourceMap = source.map(options);
  29. content = source.source();
  30. }
  31. if(!sourceMap) {
  32. return source;
  33. }
  34. // Clone (flat) the sourcemap to ensure that the mutations below do not persist.
  35. sourceMap = Object.keys(sourceMap).reduce(function(obj, key) {
  36. obj[key] = sourceMap[key];
  37. return obj;
  38. }, {});
  39. const modules = sourceMap.sources.map(function(source) {
  40. const module = self.compilation.findModule(source);
  41. return module || source;
  42. });
  43. let moduleFilenames = modules.map(function(module) {
  44. return ModuleFilenameHelpers.createFilename(module, self.moduleFilenameTemplate, this.requestShortener);
  45. }, this);
  46. moduleFilenames = ModuleFilenameHelpers.replaceDuplicates(moduleFilenames, function(filename, i, n) {
  47. for(let j = 0; j < n; j++)
  48. filename += "*";
  49. return filename;
  50. });
  51. sourceMap.sources = moduleFilenames;
  52. if(sourceMap.sourcesContent) {
  53. sourceMap.sourcesContent = sourceMap.sourcesContent.map(function(content, i) {
  54. return `${content}\n\n\n${ModuleFilenameHelpers.createFooter(modules[i], this.requestShortener)}`;
  55. }, this);
  56. }
  57. sourceMap.sourceRoot = options.sourceRoot || "";
  58. sourceMap.file = `${module.id}.js`;
  59. const footer = self.sourceMapComment.replace(/\[url\]/g, `data:application/json;charset=utf-8;base64,${new Buffer(JSON.stringify(sourceMap), "utf8").toString("base64")}`) + //eslint-disable-line
  60. `\n//# sourceURL=webpack-internal:///${module.id}\n`; // workaround for chrome bug
  61. source.__EvalSourceMapDevToolData = new RawSource(`eval(${JSON.stringify(content + footer)});`);
  62. return source.__EvalSourceMapDevToolData;
  63. });
  64. moduleTemplate.plugin("hash", function(hash) {
  65. hash.update("eval-source-map");
  66. hash.update("2");
  67. });
  68. }
  69. }
  70. module.exports = EvalSourceMapDevToolModuleTemplatePlugin;