Sha256: 6af503a862bc052fb02b2570e2039467a0b0594bd040200740771876fcf40d1e

Contents?: true

Size: 1.69 KB

Versions: 28

Compression:

Stored size: 1.69 KB

Contents

/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
"use strict";

const { ConcatSource } = require("webpack-sources");

/** @typedef {import("./Compilation")} Compilation */

class SetVarMainTemplatePlugin {
	/**
	 * @param {string} varExpression the accessor where the library is exported
	 * @param {boolean} copyObject specify copying the exports
	 */
	constructor(varExpression, copyObject) {
		/** @type {string} */
		this.varExpression = varExpression;
		/** @type {boolean} */
		this.copyObject = copyObject;
	}

	/**
	 * @param {Compilation} compilation the compilation instance
	 * @returns {void}
	 */
	apply(compilation) {
		const { mainTemplate, chunkTemplate } = compilation;

		const onRenderWithEntry = (source, chunk, hash) => {
			const varExpression = mainTemplate.getAssetPath(this.varExpression, {
				hash,
				chunk
			});
			if (this.copyObject) {
				return new ConcatSource(
					`(function(e, a) { for(var i in a) e[i] = a[i]; }(${varExpression}, `,
					source,
					"))"
				);
			} else {
				const prefix = `${varExpression} =\n`;
				return new ConcatSource(prefix, source);
			}
		};

		for (const template of [mainTemplate, chunkTemplate]) {
			template.hooks.renderWithEntry.tap(
				"SetVarMainTemplatePlugin",
				onRenderWithEntry
			);
		}

		mainTemplate.hooks.globalHashPaths.tap(
			"SetVarMainTemplatePlugin",
			paths => {
				if (this.varExpression) paths.push(this.varExpression);
				return paths;
			}
		);
		mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => {
			hash.update("set var");
			hash.update(`${this.varExpression}`);
			hash.update(`${this.copyObject}`);
		});
	}
}

module.exports = SetVarMainTemplatePlugin;

Version data entries

28 entries across 27 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.18.0 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.18.2 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.16.1 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.15.2 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.18.4 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.18.1 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.14.0 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
tang-0.2.1 spec/tang_app/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
tang-0.2.0 spec/tang_app/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
tang-0.1.0 spec/tang_app/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
tang-0.0.9 spec/tang_app/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
enju_library-0.3.8 spec/dummy/node_modules/webpack/lib/SetVarMainTemplatePlugin.js
jester-data-8.0.0 node_modules/webpack/lib/SetVarMainTemplatePlugin.js