<commit_before>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
"use strict";
let nextIdent = 0;
class CommonsChunkPlugin {
	constructor(options) {
		if(arguments.length > 1) {
			throw new Error(`Deprecation notice: CommonsChunkPlugin now only takes a single argument. Either an options
object *or* the name of the chunk.
Example: if your old code looked like this:
	new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js')
You would change it to:
	new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' })
The available options are:
	name: string
	names: string[]
	filename: string
	minChunks: number
	chunks: string[]
	children: boolean
	async: boolean
	minSize: number`);
		}

		const normalizedOptions = this.normalizeOptions(options);

		this.chunkNames = normalizedOptions.chunkNames;
		this.filenameTemplate = this.filenameTemplate;
		this.minChunks = normalizedOptions.minChunks;
		this.selectedChunks = normalizedOptions.selectedChunks;
		this.async = normalizedOptions.async;
		this.minSize = normalizedOptions.minSize;
		this.ident = normalizedOptions.ident;
	}

	normalizeOptions(options) {
		if(Array.isArray(options)) {
			return {
				chunkNames: options,
			};
		}

		if(typeof options === "string") {
			return {
				chunkNames: [options],
			};
		}

		// if "children" is set, set selectedChunks to false instead of specified chunks
		// TODO: why? :P
		const selectedChunks = options.children ? false : options.chunks;
		const chunkNames = options.name ? [options.name] : options.names;
		return {
			chunkNames: chunkNames,
			filenameTemplate: options.filename,
			minChunks: options.minChunks,
			selectedChunks: selectedChunks,
			async: options.async,
			minSize: options.minSize,
			ident: __filename + (nextIdent++),
		};
	}

	getCommonChunks(allChunks, compilation) {
		const asyncOrNoSelectedChunk = this.selectedChunks === false || this.async;

		// we have specified chunk names
		if(this.chunkNames) {
			// map chunks by chunkName for quick access
			const optimizedChunkMap = allChunks.reduce((map, chunk) => {
				map.set(chunk.name, chunk);
				return map;
			}, new Map());

			// Ensure we have a chunk per specified chunk name.
			// Reuse existing chunks if possible
			return this.chunkNames.map(chunkName => {
				if(optimizedChunkMap.has(chunkName)) {
					return optimizedChunkMap.get(chunkName);
				}
				// add the filtered chunks to the compilation
				return compilation.addChunk(chunkName);
			});
		}

		// we dont have named chunks specified, so we just take all of them
		if(asyncOrNoSelectedChunk) {
			return allChunks;
		}

		// that is not supposed to happen, lets throw
		throw new Error("Invalid chunkNames argument");
	}

	getUsedChunks(compilation, allChunks, commonChunk, commonChunks, currentIndex, selectedChunks, isAsync) {
		const asyncOrNoSelectedChunk = selectedChunks === false || isAsync;

		if(Array.isArray(selectedChunks)) {
			return allChunks.filter(chunk => {
				const notCommmonChunk = chunk !== commonChunk;
				const isSelectedChunk = selectedChunks.indexOf(chunk.name) > -1;
				return notCommmonChunk && isSelectedChunk;
			});
		}

		if(asyncOrNoSelectedChunk) {
			// nothing to do here
			if(!commonChunk.chunks) {
				return [];
			}

			return commonChunk.chunks.filter((chunk) => {
				// we can only move modules from this chunk if the "commonChunk" is the only parent
				return isAsync || chunk.parents.length === 1;
			});
		}

		// this is an entry point - bad
		if(commonChunk.parents.length > 0) {
			compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
			return;
		}

		return allChunks.filter((chunk) => {
			const found = commonChunks.indexOf(chunk);
			if(found >= currentIndex) return false;
			return chunk.hasRuntime();
		});
	}

	createAsyncChunk(compilation, asyncOption, commonChunk) {
		const asyncChunk = compilation.addChunk(typeof asyncOption === "string" ? asyncOption : undefined);
		asyncChunk.chunkReason = "async commons chunk";
		asyncChunk.extraAsync = true;
		asyncChunk.addParent(commonChunk);
		return asyncChunk;
	}

	apply(compiler) {
		const filenameTemplate = this.filenameTemplate;
		const minChunks = this.minChunks;
		const selectedChunks = this.selectedChunks;
		const asyncOption = this.async;
		const minSize = this.minSize;
		const ident = this.ident;
		compiler.plugin("this-compilation", (compilation) => {
			compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], (chunks) => {
				// only optimize once
				if(compilation[ident]) return;
				compilation[ident] = true;

				const commonChunks = this.formCommonChunks(chunks, compilation);

				commonChunks.forEach((commonChunk, idx) => {
					const usedChunks = this.getUsedChunks(compilation, chunks, commonChunk, commonChunks, idx, this.selectedChunks, this.async);
					// bail as this is an erronous state
					if(!usedChunks) {
						return;
					}

					let asyncChunk;
					if(asyncOption) {
						asyncChunk = this.createAsyncChunk(compilation, this.async, commonChunk);
						commonChunk.addChunk(asyncChunk);
						commonChunk = asyncChunk;<commit_msg>cleanup unused variables<commit_after>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
"use strict";
let nextIdent = 0;
class CommonsChunkPlugin {
	constructor(options) {
		if(arguments.length > 1) {
			throw new Error(`Deprecation notice: CommonsChunkPlugin now only takes a single argument. Either an options
object *or* the name of the chunk.
Example: if your old code looked like this:
	new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js')
You would change it to:
	new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' })
The available options are:
	name: string
	names: string[]
	filename: string
	minChunks: number
	chunks: string[]
	children: boolean
	async: boolean
	minSize: number`);
		}

		const normalizedOptions = this.normalizeOptions(options);

		this.chunkNames = normalizedOptions.chunkNames;
		this.filenameTemplate = this.filenameTemplate;
		this.minChunks = normalizedOptions.minChunks;
		this.selectedChunks = normalizedOptions.selectedChunks;
		this.async = normalizedOptions.async;
		this.minSize = normalizedOptions.minSize;
		this.ident = normalizedOptions.ident;
	}

	normalizeOptions(options) {
		if(Array.isArray(options)) {
			return {
				chunkNames: options,
			};
		}

		if(typeof options === "string") {
			return {
				chunkNames: [options],
			};
		}

		// if "children" is set, set selectedChunks to false instead of specified chunks
		// TODO: why? :P
		const selectedChunks = options.children ? false : options.chunks;
		const chunkNames = options.name ? [options.name] : options.names;
		return {
			chunkNames: chunkNames,
			filenameTemplate: options.filename,
			minChunks: options.minChunks,
			selectedChunks: selectedChunks,
			async: options.async,
			minSize: options.minSize,
			ident: __filename + (nextIdent++),
		};
	}

	getCommonChunks(allChunks, compilation) {
		const asyncOrNoSelectedChunk = this.selectedChunks === false || this.async;

		// we have specified chunk names
		if(this.chunkNames) {
			// map chunks by chunkName for quick access
			const optimizedChunkMap = allChunks.reduce((map, chunk) => {
				map.set(chunk.name, chunk);
				return map;
			}, new Map());

			// Ensure we have a chunk per specified chunk name.
			// Reuse existing chunks if possible
			return this.chunkNames.map(chunkName => {
				if(optimizedChunkMap.has(chunkName)) {
					return optimizedChunkMap.get(chunkName);
				}
				// add the filtered chunks to the compilation
				return compilation.addChunk(chunkName);
			});
		}

		// we dont have named chunks specified, so we just take all of them
		if(asyncOrNoSelectedChunk) {
			return allChunks;
		}

		// that is not supposed to happen, lets throw
		throw new Error("Invalid chunkNames argument");
	}

	getUsedChunks(compilation, allChunks, commonChunk, commonChunks, currentIndex, selectedChunks, isAsync) {
		const asyncOrNoSelectedChunk = selectedChunks === false || isAsync;

		if(Array.isArray(selectedChunks)) {
			return allChunks.filter(chunk => {
				const notCommmonChunk = chunk !== commonChunk;
				const isSelectedChunk = selectedChunks.indexOf(chunk.name) > -1;
				return notCommmonChunk && isSelectedChunk;
			});
		}

		if(asyncOrNoSelectedChunk) {
			// nothing to do here
			if(!commonChunk.chunks) {
				return [];
			}

			return commonChunk.chunks.filter((chunk) => {
				// we can only move modules from this chunk if the "commonChunk" is the only parent
				return isAsync || chunk.parents.length === 1;
			});
		}

		// this is an entry point - bad
		if(commonChunk.parents.length > 0) {
			compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
			return;
		}

		return allChunks.filter((chunk) => {
			const found = commonChunks.indexOf(chunk);
			if(found >= currentIndex) return false;
			return chunk.hasRuntime();
		});
	}

	createAsyncChunk(compilation, asyncOption, commonChunk) {
		const asyncChunk = compilation.addChunk(typeof asyncOption === "string" ? asyncOption : undefined);
		asyncChunk.chunkReason = "async commons chunk";
		asyncChunk.extraAsync = true;
		asyncChunk.addParent(commonChunk);
		return asyncChunk;
	}

	apply(compiler) {
		const filenameTemplate = this.filenameTemplate;
		const asyncOption = this.async;
		const minSize = this.minSize;
		const ident = this.ident;
		compiler.plugin("this-compilation", (compilation) => {
			compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], (chunks) => {
				// only optimize once
				if(compilation[ident]) return;
				compilation[ident] = true;

				const commonChunks = this.formCommonChunks(chunks, compilation);

				commonChunks.forEach((commonChunk, idx) => {
					const usedChunks = this.getUsedChunks(compilation, chunks, commonChunk, commonChunks, idx, this.selectedChunks, this.async);
					// bail as this is an erronous state
					if(!usedChunks) {
						return;
					}

					let asyncChunk;
					if(asyncOption) {
						asyncChunk = this.createAsyncChunk(compilation, this.async, commonChunk);
						commonChunk.addChunk(asyncChunk);
						commonChunk = asyncChunk;
{'fraction_non_alphanumeric': 0.11056817093034396, 'fraction_numerical': 0.0009581297307655457, 'mean_word_length': 3.9728442115292997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1766278', 'n_tokens_mistral': 3577, 'n_tokens_neox': 3083, 'n_words': 815}