Move normalModuleLoader hook
This commit is contained in:
parent
23bd3d7bd4
commit
3766869c48
|
@ -354,12 +354,7 @@ class Compilation {
|
|||
"childCompiler",
|
||||
"compilerName",
|
||||
"compilerIndex"
|
||||
]),
|
||||
|
||||
// TODO the following hooks are weirdly located here
|
||||
// TODO move them for webpack 5
|
||||
/** @type {SyncHook<object, Module>} */
|
||||
normalModuleLoader: new SyncHook(["loaderContext", "module"])
|
||||
])
|
||||
});
|
||||
/** @type {string=} */
|
||||
this.name = undefined;
|
||||
|
|
|
@ -16,6 +16,7 @@ const {
|
|||
toConstantDependencyWithWebpackRequire
|
||||
} = require("./JavascriptParserHelpers");
|
||||
const MainTemplate = require("./MainTemplate");
|
||||
const NormalModule = require("./NormalModule");
|
||||
const NullFactory = require("./NullFactory");
|
||||
const RuntimeGlobals = require("./RuntimeGlobals");
|
||||
const Template = require("./Template");
|
||||
|
@ -461,7 +462,7 @@ class HotModuleReplacementPlugin {
|
|||
.for("javascript/dynamic")
|
||||
.tap("HotModuleReplacementPlugin", addParserPlugins);
|
||||
|
||||
compilation.hooks.normalModuleLoader.tap(
|
||||
NormalModule.getCompilationHooks(compilation).loader.tap(
|
||||
"HotModuleReplacementPlugin",
|
||||
context => {
|
||||
context.hot = true;
|
||||
|
|
|
@ -37,13 +37,9 @@ class LoaderOptionsPlugin {
|
|||
apply(compiler) {
|
||||
const options = this.options;
|
||||
compiler.hooks.compilation.tap("LoaderOptionsPlugin", compilation => {
|
||||
compilation.hooks.normalModuleLoader.tap(
|
||||
NormalModule.getCompilationHooks(compilation).loader.tap(
|
||||
"LoaderOptionsPlugin",
|
||||
(context, m) => {
|
||||
if (!(m instanceof NormalModule)) {
|
||||
return;
|
||||
}
|
||||
const module = /** @type {NormalModule} */ m;
|
||||
(context, module) => {
|
||||
const resource = module.resource;
|
||||
if (!resource) return;
|
||||
const i = resource.indexOf("?");
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const NormalModule = require("./NormalModule");
|
||||
|
||||
/** @typedef {import("./Compiler")} Compiler */
|
||||
|
||||
class LoaderTargetPlugin {
|
||||
|
@ -21,7 +23,7 @@ class LoaderTargetPlugin {
|
|||
*/
|
||||
apply(compiler) {
|
||||
compiler.hooks.compilation.tap("LoaderTargetPlugin", compilation => {
|
||||
compilation.hooks.normalModuleLoader.tap(
|
||||
NormalModule.getCompilationHooks(compilation).loader.tap(
|
||||
"LoaderTargetPlugin",
|
||||
loaderContext => {
|
||||
loaderContext.target = this.target;
|
||||
|
|
|
@ -7,12 +7,14 @@
|
|||
|
||||
const { getContext, runLoaders } = require("loader-runner");
|
||||
const asyncLib = require("neo-async");
|
||||
const { SyncHook } = require("tapable");
|
||||
const {
|
||||
CachedSource,
|
||||
OriginalSource,
|
||||
RawSource,
|
||||
SourceMapSource
|
||||
} = require("webpack-sources");
|
||||
const Compilation = require("./Compilation");
|
||||
const Module = require("./Module");
|
||||
const ModuleBuildError = require("./ModuleBuildError");
|
||||
const ModuleError = require("./ModuleError");
|
||||
|
@ -79,7 +81,35 @@ makeSerializable(
|
|||
* @property {string} hash the hash value
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} NormalModuleCompilationHooks
|
||||
* @property {SyncHook<object, NormalModule>} loader
|
||||
*/
|
||||
|
||||
/** @type {WeakMap<Compilation, NormalModuleCompilationHooks>} */
|
||||
const compilationHooksMap = new WeakMap();
|
||||
|
||||
class NormalModule extends Module {
|
||||
/**
|
||||
* @param {Compilation} compilation the compilation
|
||||
* @returns {NormalModuleCompilationHooks} the attached hooks
|
||||
*/
|
||||
static getCompilationHooks(compilation) {
|
||||
if (!(compilation instanceof Compilation)) {
|
||||
throw new TypeError(
|
||||
"The 'compilation' argument must be an instance of Compilation"
|
||||
);
|
||||
}
|
||||
let hooks = compilationHooksMap.get(compilation);
|
||||
if (hooks === undefined) {
|
||||
hooks = {
|
||||
loader: new SyncHook(["loaderContext", "module"])
|
||||
};
|
||||
compilationHooksMap.set(compilation, hooks);
|
||||
}
|
||||
return hooks;
|
||||
}
|
||||
|
||||
constructor({
|
||||
type,
|
||||
request,
|
||||
|
@ -235,7 +265,11 @@ class NormalModule extends Module {
|
|||
fs: fs
|
||||
};
|
||||
|
||||
compilation.hooks.normalModuleLoader.call(loaderContext, this);
|
||||
NormalModule.getCompilationHooks(compilation).loader.call(
|
||||
loaderContext,
|
||||
this
|
||||
);
|
||||
|
||||
if (options.loader) {
|
||||
Object.assign(loaderContext, options.loader);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const NormalModule = require("../NormalModule");
|
||||
const LoaderDependency = require("./LoaderDependency");
|
||||
|
||||
/** @typedef {import("../Module")} Module */
|
||||
|
@ -31,9 +32,9 @@ class LoaderPlugin {
|
|||
|
||||
compiler.hooks.compilation.tap("LoaderPlugin", compilation => {
|
||||
const moduleGraph = compilation.moduleGraph;
|
||||
compilation.hooks.normalModuleLoader.tap(
|
||||
NormalModule.getCompilationHooks(compilation).loader.tap(
|
||||
"LoaderPlugin",
|
||||
(loaderContext, module) => {
|
||||
loaderContext => {
|
||||
/**
|
||||
* @param {string} request the request string to load the module from
|
||||
* @param {LoadModuleCallback} callback callback returning the loaded module or error
|
||||
|
|
Loading…
Reference in New Issue