Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 1, 2024
1 parent c6cab84 commit 766ce6d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
35 changes: 0 additions & 35 deletions src/hooks.js

This file was deleted.

36 changes: 29 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require("path");

const { validate } = require("schema-utils");
const { SyncWaterfallHook } = require("tapable");

const schema = require("./plugin-options.json");
const {
Expand All @@ -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 */
Expand Down Expand Up @@ -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<string, AssetInfo> }} CssModule */

/** @typedef {{ context: string | null, identifier: string, identifierIndex: number, content: Buffer, sourceMap?: Buffer, media?: string, supports?: string, layer?: TODO, assetsInfo?: Map<string, AssetInfo>, assets?: { [key: string]: TODO }}} CssModuleDependency */

/** @typedef {{ new(dependency: CssModuleDependency): CssModule }} CssModuleConstructor */

/** @typedef {Dependency & CssModuleDependency} CssDependency */

/** @typedef {Omit<LoaderDependency, "context">} 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
*/

/**
*
Expand All @@ -114,6 +121,9 @@ const cssDependencyCache = new WeakMap();
*/
const registered = new WeakSet();

/** @type {WeakMap<Compilation, MiniCssExtractPluginCompilationHooks>} */
const compilationHooksMap = new WeakMap();

class MiniCssExtractPlugin {
/**
* @param {Compiler["webpack"]} webpack
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
/******/
/******/ /* webpack/runtime/getFullHash */
/******/ (() => {
/******/ __webpack_require__.h = () => ("04f5273a6b9819ed9e63")
/******/ __webpack_require__.h = () => ("32b536df514e34b610fa")
/******/ })();
/******/
/******/ /* webpack/runtime/global */
Expand Down
17 changes: 16 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare class MiniCssExtractPlugin {
*/
static getCompilationHooks(
compilation: Compilation
): import("./hooks").MiniCssExtractPluginCompilationHooks;
): MiniCssExtractPluginCompilationHooks;
/**
* @param {PluginOptions} [options]
*/
Expand Down Expand Up @@ -101,6 +101,8 @@ declare namespace MiniCssExtractPlugin {
CssDependency,
CssDependencyOptions,
CssDependencyConstructor,
VarNames,
MiniCssExtractPluginCompilationHooks,
};
}
type Compiler = import("webpack").Compiler;
Expand All @@ -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<Configuration>["output"]["filename"];
chunkFilename?: Required<Configuration>["output"]["chunkFilename"];
Expand Down Expand Up @@ -240,3 +248,10 @@ type CssModuleDependency = {
};
type CssDependency = Dependency & CssModuleDependency;
type CssDependencyOptions = Omit<LoaderDependency, "context">;
type VarNames = {
tag: string;
chunkId: string;
href: string;
resolve: string;
reject: string;
};

0 comments on commit 766ce6d

Please sign in to comment.