Type 'apply' method of plugins

This commit is contained in:
Florent Cailhol 2018-11-08 22:59:19 +01:00
parent 63e15dac11
commit 8a10ea3c92
26 changed files with 189 additions and 15 deletions

View File

@ -10,6 +10,8 @@ const NullFactory = require("./NullFactory");
const CachedConstDependency = require("./dependencies/CachedConstDependency");
const ConstDependency = require("./dependencies/ConstDependency");
/** @typedef {import("./Compiler")} Compiler */
const getQuery = request => {
const i = request.indexOf("?");
return i !== -1 ? request.substr(i) : "";
@ -110,6 +112,10 @@ const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
};
class ConstPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"ConstPlugin",

View File

@ -7,6 +7,8 @@
const EvalDevToolModuleTemplatePlugin = require("./EvalDevToolModuleTemplatePlugin");
/** @typedef {import("./Compiler")} Compiler */
class EvalDevToolModulePlugin {
constructor(options) {
this.sourceUrlComment = options.sourceUrlComment;
@ -14,6 +16,10 @@ class EvalDevToolModulePlugin {
this.namespace = options.namespace;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("EvalDevToolModulePlugin", compilation => {
new EvalDevToolModuleTemplatePlugin({

View File

@ -8,6 +8,10 @@
const { RawSource } = require("webpack-sources");
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
/** @type {WeakMap<Source, Source>} */
const cache = new WeakMap();
class EvalDevToolModuleTemplatePlugin {
@ -20,6 +24,10 @@ class EvalDevToolModuleTemplatePlugin {
this.namespace = options.namespace || "";
}
/**
* @param {ModuleTemplate} moduleTemplate the module template
* @returns {void}
*/
apply(moduleTemplate) {
moduleTemplate.hooks.module.tap(
"EvalDevToolModuleTemplatePlugin",

View File

@ -8,6 +8,10 @@
const { RawSource } = require("webpack-sources");
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
/** @type {WeakMap<Source, Source>} */
const cache = new WeakMap();
class EvalSourceMapDevToolModuleTemplatePlugin {
@ -22,6 +26,10 @@ class EvalSourceMapDevToolModuleTemplatePlugin {
this.options = options;
}
/**
* @param {ModuleTemplate} moduleTemplate the module template
* @returns {void}
*/
apply(moduleTemplate) {
const options = this.options;
const matchModule = ModuleFilenameHelpers.matchObject.bind(

View File

@ -11,6 +11,7 @@ const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/LoaderOptionsPlugin.json");
/** @typedef {import("../declarations/plugins/LoaderOptionsPlugin").LoaderOptionsPluginOptions} LoaderOptionsPluginOptions */
/** @typedef {import("./Compiler")} Compiler */
class LoaderOptionsPlugin {
/**
@ -28,6 +29,10 @@ class LoaderOptionsPlugin {
this.options = options;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const options = this.options;
compiler.hooks.compilation.tap("LoaderOptionsPlugin", compilation => {

View File

@ -5,11 +5,20 @@
"use strict";
/** @typedef {import("./Compiler")} Compiler */
class LoaderTargetPlugin {
/**
* @param {string} target the target
*/
constructor(target) {
this.target = target;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("LoaderTargetPlugin", compilation => {
compilation.hooks.normalModuleLoader.tap(

View File

@ -101,8 +101,13 @@ class Module extends DependenciesBlock {
/** @type {object} */
this.buildInfo = undefined;
/** @type {boolean} */
// TODO refactor this -> options object filled from Factory
this.useSourceMap = false;
this.lineToLine = false;
// TODO figure out if this should be defined here instead of `NormalModule`.
// Without this, type checking fails because the hooks use `Module`.
this.resource = undefined;
}
// TODO remove in webpack 6

View File

@ -115,11 +115,6 @@ class NormalModule extends Module {
/** @private @type {Map<string, CachedSourceEntry>} */
this._cachedSources = new Map();
// Options for the NormalModule set by plugins
// TODO refactor this -> options object filled from Factory
this.useSourceMap = false;
this.lineToLine = false;
// Cache
this._lastSuccessfulBuildMeta = {};
this._forceBuild = true;

View File

@ -10,11 +10,20 @@ const NullFactory = require("./NullFactory");
const ConstDependency = require("./dependencies/ConstDependency");
const ProvidedDependency = require("./dependencies/ProvidedDependency");
/** @typedef {import("./Compiler")} Compiler */
class ProvidePlugin {
/**
* @param {Record<string, string | string[]>} definitions the provided identifiers
*/
constructor(definitions) {
this.definitions = definitions;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const definitions = this.definitions;
compiler.hooks.compilation.tap(

View File

@ -5,6 +5,10 @@
"use strict";
/**
* @param {number} size the size in bytes
* @returns {string} the formatted size
*/
exports.formatSize = size => {
if (typeof size !== "number" || Number.isNaN(size) === true) {
return "unknown size";

View File

@ -7,11 +7,17 @@
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
/** @typedef {import("./Compilation")} Compilation */
class SourceMapDevToolModuleOptionsPlugin {
constructor(options) {
this.options = options;
}
/**
* @param {Compilation} compilation the compiler instance
* @returns {void}
*/
apply(compilation) {
const options = this.options;
if (options.module !== false) {

View File

@ -9,6 +9,7 @@ const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/WatchIgnorePlugin.json");
/** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
/** @typedef {import("./Compiler")} Compiler */
class IgnoringWatchFileSystem {
constructor(wfs, paths) {
@ -94,6 +95,10 @@ class WatchIgnorePlugin {
this.paths = paths;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => {
compiler.watchFileSystem = new IgnoringWatchFileSystem(

View File

@ -5,7 +5,15 @@
"use strict";
module.exports = (chunks, compilation) => {
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compilation")} Compilation */
/**
* @param {Iterable<Chunk>} chunks the chunks
* @param {Compilation} compilation the compilation
* @returns {void}
*/
const assignAscendingChunkIds = (chunks, compilation) => {
const usedIds = new Set();
if (compilation.usedChunkIds) {
for (const id of compilation.usedChunkIds) {
@ -40,3 +48,5 @@ module.exports = (chunks, compilation) => {
}
}
};
module.exports = assignAscendingChunkIds;

View File

@ -5,7 +5,15 @@
"use strict";
module.exports = (modules, compilation) => {
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module")} Module */
/**
* @param {Iterable<Module>} modules the modules
* @param {Compilation} compilation the compilation
* @returns {void}
*/
const assignAscendingModuleIds = (modules, compilation) => {
const chunkGraph = compilation.chunkGraph;
const usedIds = new Set();
@ -38,3 +46,5 @@ module.exports = (modules, compilation) => {
}
}
};
module.exports = assignAscendingModuleIds;

View File

@ -10,7 +10,13 @@ const NodeJsInputFileSystem = require("enhanced-resolve/lib/NodeJsInputFileSyste
const NodeOutputFileSystem = require("./NodeOutputFileSystem");
const NodeWatchFileSystem = require("./NodeWatchFileSystem");
/** @typedef {import("../Compiler")} Compiler */
class NodeEnvironmentPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.inputFileSystem = new CachedInputFileSystem(
new NodeJsInputFileSystem(),
@ -22,8 +28,11 @@ class NodeEnvironmentPlugin {
compiler.inputFileSystem
);
compiler.hooks.beforeRun.tap("NodeEnvironmentPlugin", compiler => {
if (compiler.inputFileSystem === inputFileSystem) inputFileSystem.purge();
if (compiler.inputFileSystem === inputFileSystem) {
inputFileSystem.purge();
}
});
}
}
module.exports = NodeEnvironmentPlugin;

View File

@ -10,10 +10,17 @@ const nodeLibsBrowser = require("node-libs-browser");
const { getModulePath } = require("../JavascriptParserHelpers");
const ProvidedDependency = require("../dependencies/ProvidedDependency");
/** @typedef {import("../Compiler")} Compiler */
module.exports = class NodeSourcePlugin {
constructor(options) {
this.options = options;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const options = this.options;
if (options === false) {

View File

@ -7,11 +7,17 @@
const ExternalsPlugin = require("../ExternalsPlugin");
/** @typedef {import("../Compiler")} Compiler */
const builtins =
// eslint-disable-next-line node/no-unsupported-features/node-builtins,node/no-deprecated-api
require("module").builtinModules || Object.keys(process.binding("natives"));
class NodeTargetPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
new ExternalsPlugin("commonjs", builtins).apply(compiler);
}

View File

@ -9,12 +9,18 @@ const NodeChunkTemplatePlugin = require("./NodeChunkTemplatePlugin");
const NodeHotUpdateChunkTemplatePlugin = require("./NodeHotUpdateChunkTemplatePlugin");
const NodeMainTemplatePlugin = require("./NodeMainTemplatePlugin");
/** @typedef {import("../Compiler")} Compiler */
class NodeTemplatePlugin {
constructor(options) {
options = options || {};
this.asyncChunkLoading = options.asyncChunkLoading;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap("NodeTemplatePlugin", compilation => {
new NodeMainTemplatePlugin(this.asyncChunkLoading).apply(

View File

@ -8,11 +8,17 @@
const Template = require("../Template");
const WasmMainTemplatePlugin = require("../wasm/WasmMainTemplatePlugin");
/** @typedef {import("../Compiler")} Compiler */
class ReadFileCompileWasmTemplatePlugin {
constructor(options) {
this.options = options || {};
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"ReadFileCompileWasmTemplatePlugin",

View File

@ -7,7 +7,15 @@
const { STAGE_BASIC } = require("../OptimizationStages");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGroup")} ChunkGroup */
/** @typedef {import("../Compiler")} Compiler */
class EnsureChunkConditionsPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"EnsureChunkConditionsPlugin",
@ -17,7 +25,9 @@ class EnsureChunkConditionsPlugin {
let changed = false;
// These sets are hoisted here to save memory
// They are cleared at the end of every loop
/** @type {Set<Chunk>} */
const sourceChunks = new Set();
/** @type {Set<ChunkGroup>} */
const chunkGroups = new Set();
for (const module of compilation.modules) {
for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
@ -29,6 +39,7 @@ class EnsureChunkConditionsPlugin {
}
}
if (sourceChunks.size === 0) continue;
/** @type {Set<Chunk>} */
const targetChunks = new Set();
chunkGroupLoop: for (const chunkGroup of chunkGroups) {
// Can module be placed in a chunk of this group?

View File

@ -5,7 +5,15 @@
"use strict";
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class FlagIncludedChunksPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("FlagIncludedChunksPlugin", compilation => {
compilation.hooks.optimizeChunkIds.tap(
@ -24,6 +32,7 @@ class FlagIncludedChunksPlugin {
// so 1 / modulesCount == p^31
// <=> p = sqrt31(1 / modulesCount)
// so we use a modulo of 1 / sqrt31(1 / modulesCount)
/** @type {WeakMap<Module, number>} */
const moduleBits = new WeakMap();
const modulesCount = compilation.modules.size;
@ -46,6 +55,7 @@ class FlagIncludedChunksPlugin {
}
// interate all chunks to generate bitmaps
/** @type {WeakMap<Chunk, number>} */
const chunkModulesHash = new WeakMap();
for (const chunk of chunks) {
let hash = 0;

View File

@ -7,6 +7,8 @@
const { STAGE_ADVANCED } = require("../OptimizationStages");
/** @typedef {import("../Compiler")} Compiler */
class RuntimeChunkPlugin {
constructor(options) {
this.options = Object.assign(
@ -17,6 +19,10 @@ class RuntimeChunkPlugin {
);
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
compilation.hooks.optimizeChunks.tap(

View File

@ -8,11 +8,17 @@
const RuntimeGlobals = require("../RuntimeGlobals");
const WasmMainTemplatePlugin = require("../wasm/WasmMainTemplatePlugin");
/** @typedef {import("../Compiler")} Compiler */
class FetchCompileWasmTemplatePlugin {
constructor(options) {
this.options = options || {};
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"FetchCompileWasmTemplatePlugin",

View File

@ -7,6 +7,8 @@
const { ConcatSource } = require("webpack-sources");
/** @typedef {import("../Compilation")} Compilation */
class JsonpExportMainTemplatePlugin {
/**
* @param {string} name jsonp function name
@ -15,6 +17,10 @@ class JsonpExportMainTemplatePlugin {
this.name = name;
}
/**
* @param {Compilation} compilation the compilation instance
* @returns {void}
*/
apply(compilation) {
const { mainTemplate, chunkTemplate } = compilation;
@ -26,12 +32,15 @@ class JsonpExportMainTemplatePlugin {
return new ConcatSource(`${name}(`, source, ");");
};
for (const template of [mainTemplate, chunkTemplate]) {
template.hooks.renderWithEntry.tap(
"JsonpExportMainTemplatePlugin",
onRenderWithEntry
);
}
mainTemplate.hooks.renderWithEntry.tap(
"JsonpExportMainTemplatePlugin",
onRenderWithEntry
);
chunkTemplate.hooks.renderWithEntry.tap(
"JsonpExportMainTemplatePlugin",
onRenderWithEntry
);
mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => {
hash.update("jsonp export");

View File

@ -9,7 +9,13 @@ const JsonpChunkTemplatePlugin = require("./JsonpChunkTemplatePlugin");
const JsonpHotUpdateChunkTemplatePlugin = require("./JsonpHotUpdateChunkTemplatePlugin");
const JsonpMainTemplatePlugin = require("./JsonpMainTemplatePlugin");
/** @typedef {import("../Compiler")} Compiler */
class JsonpTemplatePlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap("JsonpTemplatePlugin", compilation => {
new JsonpMainTemplatePlugin(compilation).apply(compilation.mainTemplate);

View File

@ -9,7 +9,13 @@ const WebWorkerChunkTemplatePlugin = require("./WebWorkerChunkTemplatePlugin");
const WebWorkerHotUpdateChunkTemplatePlugin = require("./WebWorkerHotUpdateChunkTemplatePlugin");
const WebWorkerMainTemplatePlugin = require("./WebWorkerMainTemplatePlugin");
/** @typedef {import("../Compiler")} Compiler */
class WebWorkerTemplatePlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"WebWorkerTemplatePlugin",