aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/create-manifest-webpack-plugin/index.js
blob: fade66dc0d9e7b14aeeb2a7ca58c01265fed8632 (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
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) {
							this.manifest.content_scripts.push({
								matches: ["https://banglarbhumi.gov.in/*", "http://banglarbhumi.gov.in/*"],
								js: Object.keys(this.scripts[scriptType]).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;