Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow consumers to access CssModule #703

Merged
merged 13 commits into from Feb 25, 2021
41 changes: 33 additions & 8 deletions src/index.js
Expand Up @@ -18,8 +18,23 @@ const CODE_GENERATION_RESULT = {
runtimeRequirements: new Set(),
};

/**
* @type WeakMap<webpack, CssModule>
*/
const cssModuleCache = new WeakMap();
/**
* @type WeakMap<webpack, CssDependency>
*/
const cssDependencyCache = new WeakMap();

class MiniCssExtractPlugin {
static getCssModule(webpack) {
/**
* Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
*/
if (cssModuleCache.has(webpack)) {
return cssModuleCache.get(webpack);
}
class CssModule extends webpack.Module {
constructor({
context,
Expand Down Expand Up @@ -133,7 +148,9 @@ class MiniCssExtractPlugin {
super.deserialize(context);
}
}


cssModuleCache.set(webpack, CssModule);

if (
webpack.util &&
webpack.util.serialization &&
Expand Down Expand Up @@ -181,6 +198,12 @@ class MiniCssExtractPlugin {
}

static getCssDependency(webpack) {
/**
* Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
*/
if (cssDependencyCache.has(webpack)) {
return cssDependencyCache.get(webpack);
}
// eslint-disable-next-line no-shadow
class CssDependency extends webpack.Dependency {
constructor(
Expand Down Expand Up @@ -231,6 +254,8 @@ class MiniCssExtractPlugin {
}
}

cssDependencyCache.set(webpack, CssDependency);

if (
webpack.util &&
webpack.util.serialization &&
Expand Down Expand Up @@ -1093,7 +1118,7 @@ class MiniCssExtractPlugin {
return usedModules;
}

modules = [...modules];
const modulesList = [...modules];

const [chunkGroup] = chunk.groupsIterable;
const moduleIndexFunctionName =
Expand All @@ -1103,16 +1128,16 @@ class MiniCssExtractPlugin {

if (typeof chunkGroup[moduleIndexFunctionName] === 'function') {
// Store dependencies for modules
const moduleDependencies = new Map(modules.map((m) => [m, new Set()]));
const moduleDependencies = new Map(modulesList.map((m) => [m, new Set()]));
const moduleDependenciesReasons = new Map(
modules.map((m) => [m, new Map()])
modulesList.map((m) => [m, new Map()])
);

// Get ordered list of modules per chunk group
// This loop also gathers dependencies from the ordered lists
// Lists are in reverse order to allow to use Array.pop()
const modulesByChunkGroup = Array.from(chunk.groupsIterable, (cg) => {
const sortedModules = modules
const sortedModules = modulesList
.map((m) => {
return {
module: m,
Expand Down Expand Up @@ -1145,7 +1170,7 @@ class MiniCssExtractPlugin {

const unusedModulesFilter = (m) => !usedModules.has(m);

while (usedModules.size < modules.length) {
while (usedModules.size < modulesList.length) {
let success = false;
let bestMatch;
let bestMatchDeps;
Expand Down Expand Up @@ -1227,8 +1252,8 @@ class MiniCssExtractPlugin {
// (to avoid a breaking change)
// TODO remove this in next major version
// and increase minimum webpack version to 4.12.0
modules.sort((a, b) => a.index2 - b.index2);
usedModules = modules;
modulesList.sort((a, b) => a.index2 - b.index2);
usedModules = modulesList;
}

this._sortedModulesCache.set(chunk, usedModules);
Expand Down
15 changes: 15 additions & 0 deletions test/api.test.js
@@ -0,0 +1,15 @@
import webpack from 'webpack';

import MiniCssExtractPlugin from '../src';

describe('API', () => {

it('should return the same CssModule when same webpack instance provided', () => {
expect(MiniCssExtractPlugin.getCssModule(webpack)).toEqual(MiniCssExtractPlugin.getCssModule(webpack));
});

it('should return the same CssDependency when same webpack instance provided', () => {
expect(MiniCssExtractPlugin.getCssDependency(webpack)).toEqual(MiniCssExtractPlugin.getCssDependency(webpack));
});

});