webpack/lib/PrefetchPlugin.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2013-05-13 19:34:00 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-07-30 23:08:51 +08:00
const PrefetchDependency = require("./dependencies/PrefetchDependency");
2013-05-13 19:34:00 +08:00
2018-11-03 04:05:46 +08:00
/** @typedef {import("./Compiler")} Compiler */
class PrefetchPlugin {
2023-05-22 02:58:37 +08:00
/**
* @param {string} context context or request if context is not set
* @param {string} [request] request
*/
constructor(context, request) {
2018-11-03 04:05:46 +08:00
if (request) {
this.context = context;
this.request = request;
2018-11-03 04:05:46 +08:00
} else {
this.context = null;
this.request = context;
}
}
2018-11-03 04:05:46 +08:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
"PrefetchPlugin",
(compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(
PrefetchDependency,
normalModuleFactory
);
}
);
2017-12-06 22:01:25 +08:00
compiler.hooks.make.tapAsync("PrefetchPlugin", (compilation, callback) => {
compilation.addModuleChain(
2018-02-25 09:00:20 +08:00
this.context || compiler.context,
new PrefetchDependency(this.request),
2018-12-18 04:35:39 +08:00
err => {
callback(err);
}
2018-02-25 09:00:20 +08:00
);
});
}
2013-05-13 19:34:00 +08:00
}
2018-12-18 04:35:39 +08:00
2013-05-13 19:34:00 +08:00
module.exports = PrefetchPlugin;