aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/create-manifest-webpack-plugin/index.js
blob: 5dac1b242f8ed2af1f56fdbf7a0ff4cd0d5d7317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const path = require("path");

const { version } = require(path.resolve(process.env.ROOT_DIR, "package.json"));

class CreateManifestPlugin {
	constructor(scripts) {
		this.scripts = {};
		this.manifest = require("./manifest-template.json");
		this.scripts.document_start = {
			...(scripts.before || {}),
			...(scripts.injected || {}),
			...(scripts.injected_and_loaded || {})
		};
		this.scripts.document_end = scripts.rendered || {};
		this.scripts.document_idle = scripts.loaded || {};
	}

	apply(compiler) {
		compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => {
			compilation.hooks.processAssets.tap(
				{
					name: "CreateManifestPlugin",
					stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
					additionalAssets: true
				},
				assets => {
					this.manifest.version = version;
					this.manifest.content_scripts = []

					for (const scriptType in this.scripts) {
						if (Object.keys(this.scripts[scriptType]).length > 0) {
							const currentScripts = this.scripts[scriptType];

							let paths = {};
							for (const scriptName in currentScripts) {
								const scriptPath = currentScripts[scriptName];
								if (!path[scriptPath]) {
									paths[scriptPath] = [];
								}
								paths[scriptPath].push(scriptName);
							}

							for (const scriptPath in paths) {
								this.manifest.content_scripts.push({
									matches: [`*://banglarbhumi.gov.in/${scriptPath}`],
									js: paths[scriptPath].map(scriptName => `${scriptName}.js`),
									run_at: scriptType
								});
							}
						}
					}

					const { RawSource } = compiler.webpack.sources;
					assets["manifest.json"] = new RawSource(Buffer.from(JSON.stringify(this.manifest)));
				}
			);
		});
	}
}

module.exports = CreateManifestPlugin;