"use strict";

const fs = require("fs");
const path = require("path");

module.exports = {
	afterExecute(options) {
		const outputPath = options.output.path;
		const files = fs.readdirSync(outputPath);

		const expected = ["bundle0.js", "worker.mjs", "worker-worker.mjs"];
		const intersection = expected.filter((value) => files.includes(value));

		if (intersection.length !== 3) {
			throw new Error(
				`Not all files were generated by webpack, expected ${JSON.stringify(expected)}, got ${JSON.stringify(files)}`
			);
		}

		for (const file of files) {
			const filename = path.resolve(outputPath, file);
			const source = fs.readFileSync(filename, "utf8");

			if (file === "bundle0.js" && source.includes("worker import")) {
				throw new Error(`Found an worker import in ${filename}.`);
			}
		}
	}
};
