diff --git a/src/hooks.js b/src/hooks.js deleted file mode 100644 index 5b03cd72..00000000 --- a/src/hooks.js +++ /dev/null @@ -1,35 +0,0 @@ -const { SyncWaterfallHook } = require("tapable"); - -/** @typedef {import("webpack").Compilation} Compilation */ -/** - * @typedef {Object} VarNames - * @property {string} tag - * @property {string} chunkId - * @property {string} href - * @property {string} resolve - * @property {string} reject - */ - -/** - * @typedef {Object} MiniCssExtractPluginCompilationHooks - * @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert - */ - -/** @type {WeakMap} */ -const compilationHooksMap = new WeakMap(); - -/** - * - * @param {Compilation} compilation the compilation - * @returns {MiniCssExtractPluginCompilationHooks} the compilation hooks - */ -exports.getCompilationHooks = function getCompilationHooks(compilation) { - let hooks = compilationHooksMap.get(compilation); - if (!hooks) { - hooks = { - beforeTagInsert: new SyncWaterfallHook(["source", "varNames"], "string"), - }; - compilationHooksMap.set(compilation, hooks); - } - return hooks; -}; diff --git a/src/index.js b/src/index.js index 6d344585..d8fc28f8 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const path = require("path"); const { validate } = require("schema-utils"); +const { SyncWaterfallHook } = require("tapable"); const schema = require("./plugin-options.json"); const { @@ -15,7 +16,6 @@ const { getUndoPath, BASE_URI, } = require("./utils"); -const { getCompilationHooks } = require("./hooks"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -89,16 +89,23 @@ const CODE_GENERATION_RESULT = { }; /** @typedef {Module & { content: Buffer, media?: string, sourceMap?: Buffer, supports?: string, layer?: string, assets?: { [key: string]: TODO }, assetsInfo?: Map }} CssModule */ - /** @typedef {{ context: string | null, identifier: string, identifierIndex: number, content: Buffer, sourceMap?: Buffer, media?: string, supports?: string, layer?: TODO, assetsInfo?: Map, assets?: { [key: string]: TODO }}} CssModuleDependency */ - /** @typedef {{ new(dependency: CssModuleDependency): CssModule }} CssModuleConstructor */ - /** @typedef {Dependency & CssModuleDependency} CssDependency */ - /** @typedef {Omit} CssDependencyOptions */ - /** @typedef {{ new(loaderDependency: CssDependencyOptions, context: string | null, identifierIndex: number): CssDependency }} CssDependencyConstructor */ +/** + * @typedef {Object} VarNames + * @property {string} tag + * @property {string} chunkId + * @property {string} href + * @property {string} resolve + * @property {string} reject + */ +/** + * @typedef {Object} MiniCssExtractPluginCompilationHooks + * @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert + */ /** * @@ -114,6 +121,9 @@ const cssDependencyCache = new WeakMap(); */ const registered = new WeakSet(); +/** @type {WeakMap} */ +const compilationHooksMap = new WeakMap(); + class MiniCssExtractPlugin { /** * @param {Compiler["webpack"]} webpack @@ -519,7 +529,19 @@ class MiniCssExtractPlugin { * @param {Compilation} compilation */ static getCompilationHooks(compilation) { - return getCompilationHooks(compilation); + let hooks = compilationHooksMap.get(compilation); + + if (!hooks) { + hooks = { + beforeTagInsert: new SyncWaterfallHook( + ["source", "varNames"], + "string" + ), + }; + compilationHooksMap.set(compilation, hooks); + } + + return hooks; } /** diff --git a/test/cases/chunkFilename-fullhash/expected/webpack-5/0.04f5273a6b9819ed9e63.css b/test/cases/chunkFilename-fullhash/expected/webpack-5/0.32b536df514e34b610fa.css similarity index 100% rename from test/cases/chunkFilename-fullhash/expected/webpack-5/0.04f5273a6b9819ed9e63.css rename to test/cases/chunkFilename-fullhash/expected/webpack-5/0.32b536df514e34b610fa.css diff --git a/test/cases/chunkFilename-fullhash/expected/webpack-5/04f5273a6b9819ed9e63.css b/test/cases/chunkFilename-fullhash/expected/webpack-5/32b536df514e34b610fa.css similarity index 100% rename from test/cases/chunkFilename-fullhash/expected/webpack-5/04f5273a6b9819ed9e63.css rename to test/cases/chunkFilename-fullhash/expected/webpack-5/32b536df514e34b610fa.css diff --git a/test/cases/chunkFilename-fullhash/expected/webpack-5/main.js b/test/cases/chunkFilename-fullhash/expected/webpack-5/main.js index 84f5a0c2..11b3f48d 100644 --- a/test/cases/chunkFilename-fullhash/expected/webpack-5/main.js +++ b/test/cases/chunkFilename-fullhash/expected/webpack-5/main.js @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__); /******/ /******/ /* webpack/runtime/getFullHash */ /******/ (() => { -/******/ __webpack_require__.h = () => ("04f5273a6b9819ed9e63") +/******/ __webpack_require__.h = () => ("32b536df514e34b610fa") /******/ })(); /******/ /******/ /* webpack/runtime/global */ diff --git a/types/index.d.ts b/types/index.d.ts index 5aa18062..0765eb15 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -18,7 +18,7 @@ declare class MiniCssExtractPlugin { */ static getCompilationHooks( compilation: Compilation - ): import("./hooks").MiniCssExtractPluginCompilationHooks; + ): MiniCssExtractPluginCompilationHooks; /** * @param {PluginOptions} [options] */ @@ -101,6 +101,8 @@ declare namespace MiniCssExtractPlugin { CssDependency, CssDependencyOptions, CssDependencyConstructor, + VarNames, + MiniCssExtractPluginCompilationHooks, }; } type Compiler = import("webpack").Compiler; @@ -111,6 +113,12 @@ type CssDependencyConstructor = new ( identifierIndex: number ) => CssDependency; type Compilation = import("webpack").Compilation; +type MiniCssExtractPluginCompilationHooks = { + beforeTagInsert: import("tapable").SyncWaterfallHook< + [string, VarNames], + string + >; +}; type PluginOptions = { filename?: Required["output"]["filename"]; chunkFilename?: Required["output"]["chunkFilename"]; @@ -240,3 +248,10 @@ type CssModuleDependency = { }; type CssDependency = Dependency & CssModuleDependency; type CssDependencyOptions = Omit; +type VarNames = { + tag: string; + chunkId: string; + href: string; + resolve: string; + reject: string; +};