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
Prev Previous commit
Next Next commit
fix lint
  • Loading branch information
barak007 committed Feb 19, 2021
commit 6785e57157ebd202f07b7d5991e0b2a33af1cabe
35 changes: 23 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import { validate } from 'schema-utils';

import schema from './plugin-options.json';
import { MODULE_TYPE, compareModulesByIdentifier, provideLoaderContext } from './utils';
import {
MODULE_TYPE,
compareModulesByIdentifier,
provideLoaderContext,
} from './utils';

export const pluginName = 'mini-css-extract-plugin';
export const pluginSymbol = Symbol(pluginName);
Expand Down Expand Up @@ -31,8 +35,8 @@ const cssDependencyCache = new WeakMap();
class MiniCssExtractPlugin {
static getCssModule(webpack) {
/**
* Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
*/
* Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
*/
if (cssModuleCache.has(webpack)) {
return cssModuleCache.get(webpack);
}
Expand Down Expand Up @@ -149,9 +153,9 @@ class MiniCssExtractPlugin {
super.deserialize(context);
}
}

cssModuleCache.set(webpack, CssModule);

if (
webpack.util &&
webpack.util.serialization &&
Expand Down Expand Up @@ -200,8 +204,8 @@ class MiniCssExtractPlugin {

static getCssDependency(webpack) {
/**
* Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
*/
* Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
*/
if (cssDependencyCache.has(webpack)) {
return cssDependencyCache.get(webpack);
}
Expand Down Expand Up @@ -385,10 +389,15 @@ class MiniCssExtractPlugin {
const CssModule = MiniCssExtractPlugin.getCssModule(webpack);
const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);

provideLoaderContext(compiler, `${pluginName} loader context`, (loaderContext)=>{
// eslint-disable-next-line no-param-reassign
loaderContext[pluginSymbol] = true;
}, false)
provideLoaderContext(
compiler,
`${pluginName} loader context`,
(loaderContext) => {
// eslint-disable-next-line no-param-reassign
loaderContext[pluginSymbol] = true;
},
false
);

compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
class CssModuleFactory {
Expand Down Expand Up @@ -1126,7 +1135,9 @@ class MiniCssExtractPlugin {

if (typeof chunkGroup[moduleIndexFunctionName] === 'function') {
// Store dependencies for modules
const moduleDependencies = new Map(modulesList.map((m) => [m, new Set()]));
const moduleDependencies = new Map(
modulesList.map((m) => [m, new Set()])
);
const moduleDependenciesReasons = new Map(
modulesList.map((m) => [m, new Map()])
);
Expand Down
2 changes: 1 addition & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { validate } from 'schema-utils';
import { findModuleById, evalModuleCode, provideLoaderContext } from './utils';
import schema from './loader-options.json';

import MiniCssExtractPlugin, {pluginName, pluginSymbol} from './index';
import MiniCssExtractPlugin, { pluginName, pluginSymbol } from './index';

function hotLoader(content, context) {
const accept = context.locals
Expand Down
19 changes: 11 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ function compareModulesByIdentifier(a, b) {
return compareIds(a.identifier(), b.identifier());
}

function provideLoaderContext(compiler, name, handler, thisCompilation = true){
const NormalModule = compiler.webpack && compiler.webpack.NormalModule
? compiler.webpack.NormalModule
: // eslint-disable-next-line global-require
require('webpack/lib/NormalModule');

compiler.hooks[thisCompilation ? 'thisCompilation': 'compilation'].tap(
function provideLoaderContext(compiler, name, handler, thisCompilation = true) {
const NormalModule =
compiler.webpack && compiler.webpack.NormalModule
? compiler.webpack.NormalModule
: // eslint-disable-next-line global-require
require('webpack/lib/NormalModule');

compiler.hooks[thisCompilation ? 'thisCompilation' : 'compilation'].tap(
name,
(compilation) => {
const normalModuleHook =
typeof NormalModule.getCompilationHooks !== 'undefined'
? NormalModule.getCompilationHooks(compilation).loader
: compilation.hooks.normalModuleLoader;

normalModuleHook.tap(name, (loaderContext, module) => handler(loaderContext, module, compilation));
normalModuleHook.tap(name, (loaderContext, module) =>
handler(loaderContext, module, compilation)
);
}
);
}
Expand Down
10 changes: 6 additions & 4 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,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));
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));
expect(MiniCssExtractPlugin.getCssDependency(webpack)).toEqual(
MiniCssExtractPlugin.getCssDependency(webpack)
);
});

});