Skip to content

Commit 05b188a

Browse files
fix: yarn pnp compatibility (#688)
1 parent 70ce174 commit 05b188a

File tree

2 files changed

+14
-75
lines changed

2 files changed

+14
-75
lines changed

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
2727
const REGEXP_NAME = /\[name\]/i;
2828
const DEFAULT_FILENAME = '[name].css';
2929

30+
if (webpack.util.serialization && webpack.util.serialization.registerLoader) {
31+
const pathLength = `${pluginName}/dist`.length;
32+
33+
webpack.util.serialization.registerLoader(
34+
/^mini-css-extract-plugin\//,
35+
(request) => {
36+
// eslint-disable-next-line global-require, import/no-dynamic-require
37+
require(`.${request.slice(pathLength)}`);
38+
39+
return true;
40+
}
41+
);
42+
}
43+
3044
class MiniCssExtractPlugin {
3145
constructor(options = {}) {
3246
validate(schema, options, {

test/TestCache.test.js

-75
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ describe('TestCache', () => {
1414

1515
it('should work without cache', async () => {
1616
if (webpack.version[0] !== '4') {
17-
const originalRegister = webpack.util.serialization.register;
18-
19-
webpack.util.serialization.register = jest
20-
.fn()
21-
.mockImplementation((...args) => {
22-
if (args[1].startsWith('mini-css-extract-plugin')) {
23-
// eslint-disable-next-line no-param-reassign
24-
args[1] = args[1].replace(/dist/, 'src');
25-
26-
return originalRegister(...args);
27-
}
28-
29-
return originalRegister(...args);
30-
});
31-
3217
const casesDirectory = path.resolve(__dirname, 'cases');
3318
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
3419
// eslint-disable-next-line import/no-dynamic-require, global-require
@@ -136,21 +121,6 @@ describe('TestCache', () => {
136121

137122
it('should work with the "memory" cache', async () => {
138123
if (webpack.version[0] !== '4') {
139-
const originalRegister = webpack.util.serialization.register;
140-
141-
webpack.util.serialization.register = jest
142-
.fn()
143-
.mockImplementation((...args) => {
144-
if (args[1].startsWith('mini-css-extract-plugin')) {
145-
// eslint-disable-next-line no-param-reassign
146-
args[1] = args[1].replace(/dist/, 'src');
147-
148-
return originalRegister(...args);
149-
}
150-
151-
return originalRegister(...args);
152-
});
153-
154124
const casesDirectory = path.resolve(__dirname, 'cases');
155125
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
156126
// eslint-disable-next-line import/no-dynamic-require, global-require
@@ -262,21 +232,6 @@ describe('TestCache', () => {
262232

263233
it('should work with the "filesystem" cache', async () => {
264234
if (webpack.version[0] !== '4') {
265-
const originalRegister = webpack.util.serialization.register;
266-
267-
webpack.util.serialization.register = jest
268-
.fn()
269-
.mockImplementation((...args) => {
270-
if (args[1].startsWith('mini-css-extract-plugin')) {
271-
// eslint-disable-next-line no-param-reassign
272-
args[1] = args[1].replace(/dist/, 'src');
273-
274-
return originalRegister(...args);
275-
}
276-
277-
return originalRegister(...args);
278-
});
279-
280235
const casesDirectory = path.resolve(__dirname, 'cases');
281236
const directoryForCase = path.resolve(casesDirectory, 'simple');
282237
// eslint-disable-next-line import/no-dynamic-require, global-require
@@ -392,21 +347,6 @@ describe('TestCache', () => {
392347

393348
it('should work with the "filesystem" cache and asset modules', async () => {
394349
if (webpack.version[0] !== '4') {
395-
const originalRegister = webpack.util.serialization.register;
396-
397-
webpack.util.serialization.register = jest
398-
.fn()
399-
.mockImplementation((...args) => {
400-
if (args[1].startsWith('mini-css-extract-plugin')) {
401-
// eslint-disable-next-line no-param-reassign
402-
args[1] = args[1].replace(/dist/, 'src');
403-
404-
return originalRegister(...args);
405-
}
406-
407-
return originalRegister(...args);
408-
});
409-
410350
const casesDirectory = path.resolve(__dirname, 'cases');
411351
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
412352
// eslint-disable-next-line import/no-dynamic-require, global-require
@@ -528,21 +468,6 @@ describe('TestCache', () => {
528468

529469
it('should work with the "filesystem" cache and file-loader', async () => {
530470
if (webpack.version[0] !== '4') {
531-
const originalRegister = webpack.util.serialization.register;
532-
533-
webpack.util.serialization.register = jest
534-
.fn()
535-
.mockImplementation((...args) => {
536-
if (args[1].startsWith('mini-css-extract-plugin')) {
537-
// eslint-disable-next-line no-param-reassign
538-
args[1] = args[1].replace(/dist/, 'src');
539-
540-
return originalRegister(...args);
541-
}
542-
543-
return originalRegister(...args);
544-
});
545-
546471
const casesDirectory = path.resolve(__dirname, 'cases');
547472
const directoryForCase = path.resolve(casesDirectory, 'file-loader');
548473
// eslint-disable-next-line import/no-dynamic-require, global-require

0 commit comments

Comments
 (0)