Skip to content

Commit 73ee1c8

Browse files
committed
add CssDependency cache
1 parent 88b9917 commit 73ee1c8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ const CODE_GENERATION_RESULT = {
2222
* @type WeakMap<webpack, CssModule>
2323
*/
2424
const cssModuleCache = new WeakMap();
25+
/**
26+
* @type WeakMap<webpack, CssDependency>
27+
*/
28+
const cssDependencyCache = new WeakMap();
2529

2630
class MiniCssExtractPlugin {
2731
static getCssModule(webpack) {
2832
/**
2933
* Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
3034
*/
31-
if(cssModuleCache.has(webpack)) {
35+
if (cssModuleCache.has(webpack)) {
3236
return cssModuleCache.get(webpack);
3337
}
3438
class CssModule extends webpack.Module {
@@ -194,6 +198,12 @@ class MiniCssExtractPlugin {
194198
}
195199

196200
static getCssDependency(webpack) {
201+
/**
202+
* Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
203+
*/
204+
if (cssDependencyCache.has(webpack)) {
205+
return cssDependencyCache.get(webpack);
206+
}
197207
// eslint-disable-next-line no-shadow
198208
class CssDependency extends webpack.Dependency {
199209
constructor(
@@ -244,6 +254,8 @@ class MiniCssExtractPlugin {
244254
}
245255
}
246256

257+
cssDependencyCache.set(webpack, CssDependency);
258+
247259
if (
248260
webpack.util &&
249261
webpack.util.serialization &&

test/api.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import webpack from 'webpack';
33
import MiniCssExtractPlugin from '../src';
44

55
describe('API', () => {
6+
67
it('should return the same CssModule when same webpack instance provided', () => {
78
expect(MiniCssExtractPlugin.getCssModule(webpack)).toEqual(MiniCssExtractPlugin.getCssModule(webpack));
89
});
10+
11+
it('should return the same CssDependency when same webpack instance provided', () => {
12+
expect(MiniCssExtractPlugin.getCssDependency(webpack)).toEqual(MiniCssExtractPlugin.getCssDependency(webpack));
13+
});
14+
915
});

0 commit comments

Comments
 (0)