apply()

in index.js [103:163]


	apply(compiler) {
		compiler.hooks.emit.tapAsync(this._name, (compilation, callback) => {
			logger.info("Start packaging...");
			this._emit(compilation)
				.then(() => {
					logger.info("Finish packaging");
					callback();
				})
				.catch((ex) => {
					[].concat(ex).map((ex) => logger.error(ex.message));
				});
		});

		compiler.hooks.thisCompilation.tap(this._name, (compilation) => {
			compilation.hooks.needAdditionalPass.tap(this._name, () => {
				if (!this.needAditionalRun) {
					logger.info("Need additional run");
					this.needAditionalRun = true;
					return true;
				}
				// if it is always true, will lead to infinite loop!
			});
			compilation.hooks.processAssets.tap(
				{
					name: this._name,
					stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
				},
				() => {
					if (this.needAditionalRun) {
						compilation.emitAsset(
							"pbiviz.json",
							new RawSource(this.additionalAssets.pbivizJSON)
						);
						compilation.emitAsset(
							"status",
							new RawSource(this.additionalAssets.status)
						);
						compilation.updateAsset(
							"visual.js",
							new RawSource(this.additionalAssets.jsFile)
						);
					}
				}
			);
		});

		compiler.hooks.beforeRun.tapAsync(
			this._name,
			(compilationParams, callback) => {
				this._beforeCompile(callback);
			}
		);

		compiler.hooks.watchRun.tapAsync(
			this._name,
			(compilation, callback) => {
				this.needAditionalRun = false;
				this._beforeCompile(callback);
			}
		);
	}