|
| 1 | +import test from "ava"; |
| 2 | +import InlineChunkManifestHtmlWebpackPlugin from "../src/"; |
| 3 | + |
| 4 | +test.cb("create asset", t => { |
| 5 | + const manifestFilename = "a.manifest"; |
| 6 | + const manifestFileContent = "source-content"; |
| 7 | + const manifestVariable = "manifest-variable"; |
| 8 | + const chunkManifestVariable = "chunk-manifest-variable"; |
| 9 | + |
| 10 | + const compilationPluginEvent = (compilationEvent, alterAssets) => { |
| 11 | + if (compilationEvent === "html-webpack-plugin-before-html-generation") { |
| 12 | + const htmlPluginData = { |
| 13 | + assets: {} |
| 14 | + }; |
| 15 | + |
| 16 | + alterAssets(htmlPluginData, (_, result) => { |
| 17 | + const asset = htmlPluginData.assets[chunkManifestVariable]; |
| 18 | + |
| 19 | + t.is( |
| 20 | + asset, |
| 21 | + `<script type="text/javascript">window.${manifestVariable}=${manifestFileContent}</script>` |
| 22 | + ); |
| 23 | + |
| 24 | + t.end(); |
| 25 | + }); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + const pluginEvent = (compilerEvent, compile) => { |
| 30 | + t.is(compilerEvent, "compilation"); |
| 31 | + |
| 32 | + const assets = {}; |
| 33 | + assets[manifestFilename] = { |
| 34 | + source: () => manifestFileContent |
| 35 | + }; |
| 36 | + |
| 37 | + const compilation = { |
| 38 | + plugin: compilationPluginEvent, |
| 39 | + assets: assets |
| 40 | + }; |
| 41 | + |
| 42 | + compile(compilation); |
| 43 | + }; |
| 44 | + |
| 45 | + const fakeCompiler = { plugin: pluginEvent }; |
| 46 | + |
| 47 | + const plugin = new InlineChunkManifestHtmlWebpackPlugin({ |
| 48 | + filename: manifestFilename, |
| 49 | + manifestVariable: manifestVariable, |
| 50 | + chunkManifestVariable: chunkManifestVariable |
| 51 | + }); |
| 52 | + |
| 53 | + plugin.plugins = [{ apply: () => {} }]; |
| 54 | + plugin.apply(fakeCompiler); |
| 55 | +}); |
0 commit comments