Skip to content

Commit aeb97fc

Browse files
refactor: code (#1077)
1 parent c6cab84 commit aeb97fc

File tree

6 files changed

+46
-44
lines changed

6 files changed

+46
-44
lines changed

Diff for: src/hooks.js

-35
This file was deleted.

Diff for: src/index.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const path = require("path");
44

55
const { validate } = require("schema-utils");
6+
const { SyncWaterfallHook } = require("tapable");
67

78
const schema = require("./plugin-options.json");
89
const {
@@ -15,7 +16,6 @@ const {
1516
getUndoPath,
1617
BASE_URI,
1718
} = require("./utils");
18-
const { getCompilationHooks } = require("./hooks");
1919

2020
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
2121
/** @typedef {import("webpack").Compiler} Compiler */
@@ -89,16 +89,23 @@ const CODE_GENERATION_RESULT = {
8989
};
9090

9191
/** @typedef {Module & { content: Buffer, media?: string, sourceMap?: Buffer, supports?: string, layer?: string, assets?: { [key: string]: TODO }, assetsInfo?: Map<string, AssetInfo> }} CssModule */
92-
9392
/** @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 */
94-
9593
/** @typedef {{ new(dependency: CssModuleDependency): CssModule }} CssModuleConstructor */
96-
9794
/** @typedef {Dependency & CssModuleDependency} CssDependency */
98-
9995
/** @typedef {Omit<LoaderDependency, "context">} CssDependencyOptions */
100-
10196
/** @typedef {{ new(loaderDependency: CssDependencyOptions, context: string | null, identifierIndex: number): CssDependency }} CssDependencyConstructor */
97+
/**
98+
* @typedef {Object} VarNames
99+
* @property {string} tag
100+
* @property {string} chunkId
101+
* @property {string} href
102+
* @property {string} resolve
103+
* @property {string} reject
104+
*/
105+
/**
106+
* @typedef {Object} MiniCssExtractPluginCompilationHooks
107+
* @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert
108+
*/
102109

103110
/**
104111
*
@@ -114,6 +121,9 @@ const cssDependencyCache = new WeakMap();
114121
*/
115122
const registered = new WeakSet();
116123

124+
/** @type {WeakMap<Compilation, MiniCssExtractPluginCompilationHooks>} */
125+
const compilationHooksMap = new WeakMap();
126+
117127
class MiniCssExtractPlugin {
118128
/**
119129
* @param {Compiler["webpack"]} webpack
@@ -519,7 +529,19 @@ class MiniCssExtractPlugin {
519529
* @param {Compilation} compilation
520530
*/
521531
static getCompilationHooks(compilation) {
522-
return getCompilationHooks(compilation);
532+
let hooks = compilationHooksMap.get(compilation);
533+
534+
if (!hooks) {
535+
hooks = {
536+
beforeTagInsert: new SyncWaterfallHook(
537+
["source", "varNames"],
538+
"string"
539+
),
540+
};
541+
compilationHooksMap.set(compilation, hooks);
542+
}
543+
544+
return hooks;
523545
}
524546

525547
/**

Diff for: test/cases/chunkFilename-fullhash/expected/webpack-5/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("04f5273a6b9819ed9e63")
76+
/******/ __webpack_require__.h = () => ("32b536df514e34b610fa")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */

Diff for: types/index.d.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare class MiniCssExtractPlugin {
1818
*/
1919
static getCompilationHooks(
2020
compilation: Compilation
21-
): import("./hooks").MiniCssExtractPluginCompilationHooks;
21+
): MiniCssExtractPluginCompilationHooks;
2222
/**
2323
* @param {PluginOptions} [options]
2424
*/
@@ -101,6 +101,8 @@ declare namespace MiniCssExtractPlugin {
101101
CssDependency,
102102
CssDependencyOptions,
103103
CssDependencyConstructor,
104+
VarNames,
105+
MiniCssExtractPluginCompilationHooks,
104106
};
105107
}
106108
type Compiler = import("webpack").Compiler;
@@ -111,6 +113,12 @@ type CssDependencyConstructor = new (
111113
identifierIndex: number
112114
) => CssDependency;
113115
type Compilation = import("webpack").Compilation;
116+
type MiniCssExtractPluginCompilationHooks = {
117+
beforeTagInsert: import("tapable").SyncWaterfallHook<
118+
[string, VarNames],
119+
string
120+
>;
121+
};
114122
type PluginOptions = {
115123
filename?: Required<Configuration>["output"]["filename"];
116124
chunkFilename?: Required<Configuration>["output"]["chunkFilename"];
@@ -240,3 +248,10 @@ type CssModuleDependency = {
240248
};
241249
type CssDependency = Dependency & CssModuleDependency;
242250
type CssDependencyOptions = Omit<LoaderDependency, "context">;
251+
type VarNames = {
252+
tag: string;
253+
chunkId: string;
254+
href: string;
255+
resolve: string;
256+
reject: string;
257+
};

0 commit comments

Comments
 (0)