aboutsummaryrefslogtreecommitdiffstats
path: root/webpack.config.js
blob: 2115cb6d77348d98cf1f674eef17b1ed5420b8ff (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * Webpack configuration for the Banglar Bhumi Utils Chrome extension.
 *
 * Produces three separate bundles:
 *  1. **Uninjected scripts** – content scripts that run as-is.
 *  2. **Injected scripts**   – content scripts that are further processed
 *     so they can be injected into the page DOM.
 *  3. **Shared modules**     – libraries compiled as standalone files and
 *     referenced via global variables (`$<hash>`).
 *
 * Custom loaders and plugins handle dependency ordering, import counting,
 * asset-URL rewriting, manifest generation, and declarative-net-rule creation.
 */
import path from "path";
import { fileURLToPath } from "url";

import CopyPlugin from "copy-webpack-plugin";
import webpack from "webpack";
import { merge } from "webpack-merge";

import CreateInjectedSharedModulesPlugin from "./config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js";
import CreateManifestPlugin from "./config/webpack/plugins/create-manifest-webpack-plugin/index.js";
import CreateRulesPlugin from "./config/webpack/plugins/create-rules-webpack-plugin.js";
import InjectScriptPlugin from "./config/webpack/plugins/inject-script-webpack-plugin.js";
import { getFileName } from "./config/webpack/utils/build-file.js";
import { inlineJavascript } from "./config/webpack/utils/inline-javascript.js";
import scripts from "./config/webpack/utils/scripts.js";
import sharedModules from "./config/webpack/utils/shared-modules.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

global.ROOT_DIR = path.resolve(__dirname);
global.CONFIG_DIR = path.resolve(ROOT_DIR, "config");
global.SOURCE_DIR = path.resolve(ROOT_DIR, "src");
global.production = process.env.NODE_ENV === "production";

const sharedModulesImportedCount = {};

for (const sharedModule of sharedModules) {
	sharedModulesImportedCount[sharedModule] = 0;
}

const injectedSharedModulesImportedCount = {};

for (const sharedModule of sharedModules) {
	injectedSharedModulesImportedCount[sharedModule] = 0;
}

const uninjectedScriptEntries = {};
const injectedAfterScriptEntries = {};
const sharedModuleEntries = {};
const sharedModuleExternals = {};

for (const scriptPath in scripts) {
	for (const scriptType in scripts[scriptPath]) {
		const currentScripts = scripts[scriptPath][scriptType];
		let scriptEntries;
		let destinationFolder = "scripts";

		switch (scriptType) {
			case "injected":
				scriptEntries = injectedAfterScriptEntries;
				destinationFolder = "scripts/injected";
				break;

			default:
				scriptEntries = uninjectedScriptEntries;
		}

		scriptEntries[`${scriptType} - "${scriptPath}"`] = {
			import: inlineJavascript(currentScripts.map((script) => `import "${path.resolve(SOURCE_DIR, "scripts", script)}";`).join("\n")),
			filename: `${destinationFolder}/${getFileName(scriptType, scriptPath)}.js`
		};
	}
}

for (const sharedModule of sharedModules) {
	const sharedModuleNameHash = getFileName(sharedModule, "shared");
	const sharedModuleGlobalVariableName = `$${sharedModuleNameHash.substring(sharedModuleNameHash.length - 16)}`;
	sharedModuleEntries[sharedModule] = {
		import: path.resolve(SOURCE_DIR, "shared", sharedModule),
		filename: `shared/${sharedModuleNameHash}.js`,
		library: {
			name: sharedModuleGlobalVariableName,
			type: "var"
		}
	};
	sharedModuleExternals[`@/shared/${sharedModule}`] = sharedModuleGlobalVariableName;
}

const commonOptions = {
	mode: "production",
	target: "web",
	resolve: {
		extensions: [".ts", ".js", ".html"],
		alias: {
			"@": global.SOURCE_DIR
		}
	},
	module: {
		rules: [
			{
				test: /\.ts$/,
				loader: "ts-loader",
				options: {
					configFile: "tsconfig.json"
				}
			},
			{
				test: /\.html$/,
				loader: path.resolve(CONFIG_DIR, "webpack/loader/to-string-loader.js")
			}
		]
	},
	optimization: {
		minimize: process.env.NODE_ENV === "production",
		runtimeChunk: false
	},
	output: {
		iife: true
	},
	plugins: [
		new webpack.optimize.LimitChunkCountPlugin({
			maxChunks: 1
		})
	]
};

const sharedModuleOptions = {
	externals: sharedModuleExternals,
	externalsType: "var",
	module: {
		rules: [
			{
				test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/,
				loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"),
				options: {
					sharedModulesImportedCount
				},
				enforce: "post"
			}
		]
	}
};

const uninjectedScriptConfiguration = merge(
	commonOptions,
	sharedModuleOptions,
	{
		entry: uninjectedScriptEntries
	}
);

const injectedScriptConfiguration = merge(
	commonOptions,
	{
		externals: sharedModuleExternals,
		externalsType: "var",
		module: {
			rules: [
				{
					test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/,
					loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"),
					options: {
						sharedModulesImportedCount: injectedSharedModulesImportedCount
					},
					enforce: "post"
				}
			]
		},
		entry: injectedAfterScriptEntries,
		plugins: [
			new InjectScriptPlugin()
		]
	}
);

const sortedSharedModules = [];

const sharedModulesConfiguration = merge(
	commonOptions,
	sharedModuleOptions,
	{
		entry: sharedModuleEntries,
		plugins: [
			new CopyPlugin({
				patterns: [
					{
						from: path.resolve("static"),
						to: "./"
					}
				]
			}),
			new CreateInjectedSharedModulesPlugin({
				injectedSharedModulesImportedCount,
				sortedSharedModules
			}),
			new CreateRulesPlugin(),
			new CreateManifestPlugin({
				sharedModulesImportedCount,
				injectedSharedModulesImportedCount,
				sortedSharedModules
			})
		],
		module: {
			rules: [
				{
					test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/,
					loader: path.resolve(CONFIG_DIR, "webpack/loader/arrange-shared-module-loader.js"),
					options: {
						sortedSharedModules
					},
					enforce: "post"
				}
			]
		}
	}
);

export default [
	uninjectedScriptConfiguration,
	injectedScriptConfiguration,
	sharedModulesConfiguration
];