UNPKG

1.05 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const PrefetchDependency = require("./dependencies/PrefetchDependency");
9
10/** @typedef {import("./Compiler")} Compiler */
11
12class PrefetchPlugin {
13 constructor(context, request) {
14 if (request) {
15 this.context = context;
16 this.request = request;
17 } else {
18 this.context = null;
19 this.request = context;
20 }
21 }
22
23 /**
24 * Apply the plugin
25 * @param {Compiler} compiler the compiler instance
26 * @returns {void}
27 */
28 apply(compiler) {
29 compiler.hooks.compilation.tap(
30 "PrefetchPlugin",
31 (compilation, { normalModuleFactory }) => {
32 compilation.dependencyFactories.set(
33 PrefetchDependency,
34 normalModuleFactory
35 );
36 }
37 );
38 compiler.hooks.make.tapAsync("PrefetchPlugin", (compilation, callback) => {
39 compilation.addModuleChain(
40 this.context || compiler.context,
41 new PrefetchDependency(this.request),
42 err => {
43 callback(err);
44 }
45 );
46 });
47 }
48}
49
50module.exports = PrefetchPlugin;