|
| 1 | +const HTMLParser = require("./HTMLParser"); |
| 2 | +const HTMLGenerator = require("./HTMLGenerator"); |
| 3 | + |
| 4 | +const Template = require("../Template"); |
| 5 | +const { ConcatSource } = require("webpack-sources"); |
| 6 | + |
| 7 | +class HTMLModulesPlugin { |
| 8 | + constructor() { |
| 9 | + this.plugin = { |
| 10 | + name: "HTMLModulesPlugin" |
| 11 | + }; |
| 12 | + } |
| 13 | + |
| 14 | + apply(compiler) { |
| 15 | + const { plugin } = this; |
| 16 | + const { compilation } = compiler.hooks; |
| 17 | + |
| 18 | + compilation.tap(plugin, (compilation, { normalModuleFactory }) => { |
| 19 | + const { createParser, createGenerator } = normalModuleFactory.hooks; |
| 20 | + |
| 21 | + createParser.for("html/experimental").tap(plugin, () => { |
| 22 | + return new HTMLParser(); |
| 23 | + }); |
| 24 | + |
| 25 | + createGenerator.for("html/experimental").tap(plugin, () => { |
| 26 | + return new HTMLGenerator(); |
| 27 | + }); |
| 28 | + |
| 29 | + const { chunkTemplate } = compilation; |
| 30 | + |
| 31 | + chunkTemplate.hooks.renderManifest.tap(plugin, (result, options) => { |
| 32 | + const chunk = options.chunk; |
| 33 | + const output = options.outputOptions; |
| 34 | + |
| 35 | + const { moduleTemplates, dependencyTemplates } = options; |
| 36 | + |
| 37 | + for (const module of chunk.modulesIterable) { |
| 38 | + if (module.type && module.type.startsWith("html")) { |
| 39 | + const filenameTemplate = output.HTMLModuleFilename; |
| 40 | + |
| 41 | + result.push({ |
| 42 | + // render: () => this.renderHTMLModules( |
| 43 | + // module, |
| 44 | + // moduleTemplates.html, |
| 45 | + // dependencyTemplates |
| 46 | + // ), |
| 47 | + render: () => |
| 48 | + this.renderHTML( |
| 49 | + chunkTemplate, |
| 50 | + chunk, |
| 51 | + moduleTemplates.html, |
| 52 | + dependencyTemplates |
| 53 | + ), |
| 54 | + filenameTemplate, |
| 55 | + pathOptions: { |
| 56 | + module |
| 57 | + }, |
| 58 | + identifier: `HTMLModule ${module.id}`, |
| 59 | + hash: module.hash |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return result; |
| 65 | + }); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + renderHTMLModules(module, moduleTemplate, dependencyTemplates) { |
| 70 | + return moduleTemplate.render(module, dependencyTemplates, {}); |
| 71 | + } |
| 72 | + |
| 73 | + renderHTML(chunkTemplate, chunk, moduleTemplate, dependencyTemplates) { |
| 74 | + const { modules /* render */ } = chunkTemplate.hooks; |
| 75 | + |
| 76 | + const sources = Template.renderHTMLChunk( |
| 77 | + chunk, |
| 78 | + module => module.type.startsWith("html"), |
| 79 | + moduleTemplate, |
| 80 | + dependencyTemplates |
| 81 | + ); |
| 82 | + |
| 83 | + const core = modules.call( |
| 84 | + sources, |
| 85 | + chunk, |
| 86 | + moduleTemplate, |
| 87 | + dependencyTemplates |
| 88 | + ); |
| 89 | + |
| 90 | + // let source = render.call( |
| 91 | + // core, |
| 92 | + // chunk, |
| 93 | + // moduleTemplate, |
| 94 | + // dependencyTemplates |
| 95 | + // ); |
| 96 | + |
| 97 | + chunk.rendered = true; |
| 98 | + |
| 99 | + return new ConcatSource(core); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = HTMLModulesPlugin; |
0 commit comments