aboutsummaryrefslogtreecommitdiffstats
path: root/webpack.config.js
blob: d98d83fc4e3ea5a046bc15777a365d80b6326680 (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
const fs = require("fs");
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const manifest = require("./static/manifest.json");

const SOURCE_DIR = path.resolve(__dirname, "src");
const STATIC_DIR = path.resolve(__dirname, "static");

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

	return {
		entry,
		mode: "production",
		output: {
			filename: `${scriptName}.js`
		},
		resolve: {
			extensions: ["", ".ts", ".js", ".sjs"],
			alias: {
				"@": SOURCE_DIR
			}
		},
		module: {
			rules: [
				{ test: /\.sjs$/, loader: "raw-loader" },
				{ test: /\.tsx?$/, loader: "ts-loader" }
			]
		}
	};
};

let scripts = [];

for (const script of manifest.content_scripts) {
	scripts = scripts.concat(script.js.map(generateScriptBuildConfig));
}

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

module.exports = scripts;