aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/create-manifest-webpack-plugin/index.js
blob: 3a16770bf202ab776fae90b7da310aa7cf9e2c9c (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
const { sources } = require("webpack");
const path = require("path");

const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime");
const { getFileNameHash } = webpackRequire("utils/file-name-hash");
const { formatScripts } = webpackRequire("utils/format-scripts");

class CreateManifestPlugin {
	apply(compiler) {
		compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => {
			compilation.hooks.processAssets.tap(
				{
					name: "CreateManifestPlugin",
					stage: compiler.webpack.Compilation.PROCESS_ASSETS_ADDITIONAL,
					additionalAssets: true
				},
				() => {
					const scripts = formatScripts(require(path.resolve(SOURCE_DIR, "scripts.json")));
					const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json");
					manifest.version = require(path.resolve(ROOT_DIR, "package.json")).version;
					manifest.content_scripts = []

					for (const scriptPath in scripts) {
						for (const scriptType in scripts[scriptPath]) {
							manifest.content_scripts.push({
								matches: [`*://banglarbhumi.gov.in/BanglarBhumi/${scriptPath}`],
								js: [`scripts/${getFileNameHash(scriptPath)}/${scriptType}.js`],
								run_at: getScriptRuntimeFromType(scriptType)
							});
						}
					}

					compilation.emitAsset(
						"manifest.json",
						new sources.RawSource(JSON.stringify(manifest))
					);
				}
			);
		});
	}
}

module.exports = CreateManifestPlugin;