From 23bd3d7bd461c2ecec3fe275f5e41fa12a4ad6b5 Mon Sep 17 00:00:00 2001 From: Florent Cailhol Date: Mon, 12 Nov 2018 09:15:06 +0100 Subject: [PATCH] Add type guard for NormalModule --- lib/EvalSourceMapDevToolModuleTemplatePlugin.js | 9 ++++++++- lib/LoaderOptionsPlugin.js | 7 ++++++- lib/Module.js | 7 ------- lib/NormalModule.js | 3 +++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/EvalSourceMapDevToolModuleTemplatePlugin.js b/lib/EvalSourceMapDevToolModuleTemplatePlugin.js index b404ed8ca..b117ea3c3 100644 --- a/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +++ b/lib/EvalSourceMapDevToolModuleTemplatePlugin.js @@ -7,6 +7,7 @@ const { RawSource } = require("webpack-sources"); const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); +const NormalModule = require("./NormalModule"); /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("./ModuleTemplate")} ModuleTemplate */ @@ -38,12 +39,18 @@ class EvalSourceMapDevToolModuleTemplatePlugin { ); moduleTemplate.hooks.module.tap( "EvalSourceMapDevToolModuleTemplatePlugin", - (source, module) => { + (source, m) => { const cachedSource = cache.get(source); if (cachedSource !== undefined) { return cachedSource; } + if (!(m instanceof NormalModule)) { + return source; + } + + const module = /** @type {NormalModule} */ (m); + if (!matchModule(module.resource)) { return source; } diff --git a/lib/LoaderOptionsPlugin.js b/lib/LoaderOptionsPlugin.js index b2785dcad..ceb896f93 100644 --- a/lib/LoaderOptionsPlugin.js +++ b/lib/LoaderOptionsPlugin.js @@ -6,6 +6,7 @@ "use strict"; const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); +const NormalModule = require("./NormalModule"); const validateOptions = require("schema-utils"); const schema = require("../schemas/plugins/LoaderOptionsPlugin.json"); @@ -38,7 +39,11 @@ class LoaderOptionsPlugin { compiler.hooks.compilation.tap("LoaderOptionsPlugin", compilation => { compilation.hooks.normalModuleLoader.tap( "LoaderOptionsPlugin", - (context, module) => { + (context, m) => { + if (!(m instanceof NormalModule)) { + return; + } + const module = /** @type {NormalModule} */ m; const resource = module.resource; if (!resource) return; const i = resource.indexOf("?"); diff --git a/lib/Module.js b/lib/Module.js index 130710584..de887fe01 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -100,13 +100,6 @@ class Module extends DependenciesBlock { this.buildMeta = undefined; /** @type {object} */ this.buildInfo = undefined; - - // TODO refactor this -> options object filled from Factory - this.useSourceMap = 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 diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 91726718a..354d88b25 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -117,6 +117,9 @@ class NormalModule extends Module { // Cache this._lastSuccessfulBuildMeta = {}; this._forceBuild = true; + + // TODO refactor this -> options object filled from Factory + this.useSourceMap = false; } /**