aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack.config.js
blob: 1796aa8e73f9acc65ffc49b8592534a76f53ce45 (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
62
63
64
65
66
67
68
69
70
71
const fs = require("fs");
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");

process.env.ROOT_DIR = path.resolve(__dirname, "..");
process.env.SOURCE_DIR = path.resolve(process.env.ROOT_DIR, "src");
process.env.STATIC_DIR = path.resolve(process.env.ROOT_DIR, "static");

const InjectScriptPlugin = require("./webpack/plugins/inject-script-webpack-plugin");
const CreateManifestPlugin = require("./webpack/plugins/create-manifest-webpack-plugin");

const manifest = require(path.resolve(process.env.SOURCE_DIR, "manifest.json"));

const generateScriptBuildConfig = (script, scriptType) => {
	const scriptName = script.substring(0, script.length - 3);
	let entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.ts`);
	if (!fs.existsSync(entry)) {
		entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}/index.ts`);
		if (!fs.existsSync(entry)) {
			entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.js`);
		}
	}

	return {
		entry,
		mode: "production",
		output: {
			filename: `${scriptName}.js`
		},
		resolve: {
			extensions: ["", ".ts", ".js"],
			alias: {
				"@": process.env.SOURCE_DIR
			}
		},
		module: {
			rules: [
				{
					test: /\.tsx?$/,
					loader: "ts-loader",
					options: {
						configFile: "config/tsconfig.json"
					}
				}
			]
		},
		plugins: scriptType.startsWith("injected")
			? [new InjectScriptPlugin({ onload: scriptType === "injected_and_loaded" })]
			: undefined
	};
};

let scripts = [];

for (const scriptType in manifest) {
	for (const scriptName in manifest[scriptType]) {
		scripts.push(generateScriptBuildConfig(scriptName, scriptType));
	}
}

scripts[scripts.length - 1].plugins = (scripts[scripts.length - 1].plugins || []).concat([
	new CopyPlugin({
		patterns: [{
			from: process.env.STATIC_DIR,
			to: "./"
		}]
	}),
	new CreateManifestPlugin(manifest)
]);

module.exports = scripts;