/**
 * @author Eoin Hennessy
 * @author Erik Desjardins
 * See LICENSE file in root directory for full license.
 */

'use strict';

var path = require('path');
var loaderUtils = require('loader-utils');
var SingleEntryPlugin = require('webpack').SingleEntryPlugin;
var InertEntryPlugin = require('inert-entry-webpack-plugin');

module.exports = function() {};

module.exports.pitch = function(request) {
	var callback = this.async();

	var options = loaderUtils.getOptions(this) || {};
	var filename = options.name || path.basename(this.resourcePath);
	var outputDir = options.path || '.';
	var plugins = options.inert ? [new InertEntryPlugin()] : [];

	// name of the entry and compiler (in logs)
	var debugName = loaderUtils.interpolateName(this, '[name]', {});

	// create a child compiler (hacky)
	var compiler = this._compilation.createChildCompiler(debugName, { filename: filename }, plugins);
	new SingleEntryPlugin(this.context, '!!' + request, debugName).apply(compiler);

	// add a dependency on the entry point of the child compiler, so watch mode works
	this.addDependency(request);

	// like compiler.runAsChild(), but remaps paths if necessary
	// https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L206
	compiler.compile(function(err, compilation) {
		if (err) return callback(err);

		this.parentCompilation.children.push(compilation);
		for (const name of Object.keys(compilation.assets)) {
			this.parentCompilation.assets[path.join(outputDir, name)] = compilation.assets[name];
		}

		// the first file in the first chunk of the first (should only be one) entry point is the real file
		// see https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L215
		var outputFilename = compilation.entrypoints.values().next().value.chunks[0].files[0];

		callback(null, 'module.exports = __webpack_public_path__ + ' + JSON.stringify(path.join(outputDir, outputFilename)) + ';')
	}.bind(compiler));
};

@@ -610,17 +610,60 @@
 ons.
-inert ? %5B
+plugins %7C%7C %5B%5D;%0A%09if (options.inert) %7B%0A%09%09plugins.push(
 new 
@@ -684,15 +684,13 @@
 in()
-%5D : %5B%5D;
+);%0A%09%7D
 %0A%0A%09/

Add plugin option.