"use strict";

const fs = require("fs");
const path = require("path");
const webpack = require("../../../../");

/** @type {import("../../../../").WebpackPluginInstance} */
const copyTest = {
	apply(compiler) {
		compiler.hooks.compilation.tap("Test", (compilation) => {
			compilation.hooks.processAssets.tap(
				{
					name: "copy-test",
					stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
				},
				() => {
					compilation.emitAsset(
						"test.js",
						new webpack.sources.RawSource(
							fs.readFileSync(path.resolve(__dirname, "test.js"))
						)
					);
				}
			);
		});
	}
};

// `output.crossOriginLoading` must be mirrored onto the injected JS script and
// the CSS `<link>` extracted from the JS import.
/** @type {import("../../../../").Configuration} */
module.exports = {
	target: "web",
	entry: { main: "./src/main.js" },
	output: {
		filename: "[name].js",
		html: true,
		crossOriginLoading: "anonymous"
	},
	optimization: { minimize: false },
	experiments: { html: true, css: true },
	plugins: [copyTest]
};
