|
| 1 | +import test from "ava"; |
| 2 | +import InlineChunkManifestHtmlWebpackPlugin from "../src/"; |
| 3 | + |
| 4 | +const manifestFilename = "a.manifest"; |
| 5 | +const manifestFileContent = "source-content"; |
| 6 | +const manifestVariable = "manifest-variable"; |
| 7 | + |
| 8 | +test.cb("drop asset", t => { |
| 9 | + isDropped(true, asset => { |
| 10 | + t.is(asset, undefined); |
| 11 | + t.end(); |
| 12 | + }); |
| 13 | +}); |
| 14 | + |
| 15 | +test.cb("keep asset", t => { |
| 16 | + isDropped(false, asset => { |
| 17 | + t.is(asset.source(), manifestFileContent); |
| 18 | + t.end(); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +function isDropped(dropAsset, callback) { |
| 23 | + const assets = {}; |
| 24 | + assets[manifestFilename] = { |
| 25 | + source: () => manifestFileContent |
| 26 | + }; |
| 27 | + |
| 28 | + const compilationPluginEvent = (compilationEvent, alterAssets) => { |
| 29 | + if (compilationEvent === "html-webpack-plugin-alter-asset-tags") { |
| 30 | + const htmlPluginData = { |
| 31 | + head: [] |
| 32 | + }; |
| 33 | + |
| 34 | + alterAssets(htmlPluginData, (_, result) => { |
| 35 | + const asset = assets[manifestFilename]; |
| 36 | + callback(asset); |
| 37 | + }); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const pluginEvent = (compilerEvent, compile) => { |
| 42 | + const compilation = { |
| 43 | + plugin: compilationPluginEvent, |
| 44 | + assets: assets |
| 45 | + }; |
| 46 | + |
| 47 | + compile(compilation); |
| 48 | + }; |
| 49 | + |
| 50 | + const fakeCompiler = { plugin: pluginEvent }; |
| 51 | + |
| 52 | + const plugin = new InlineChunkManifestHtmlWebpackPlugin({ |
| 53 | + filename: manifestFilename, |
| 54 | + manifestVariable: manifestVariable, |
| 55 | + dropAsset: dropAsset |
| 56 | + }); |
| 57 | + |
| 58 | + plugin.plugins = [ |
| 59 | + { |
| 60 | + apply: () => {} |
| 61 | + } |
| 62 | + ]; |
| 63 | + |
| 64 | + plugin.apply(fakeCompiler); |
| 65 | +} |
0 commit comments