Skip to content

Commit 3e252e7

Browse files
fix: compatibility with asset modules
1 parent 3dce50d commit 3e252e7

16 files changed

+1107
-200
lines changed

Diff for: package-lock.json

+603-186
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"del": "^6.0.0",
6363
"del-cli": "^3.0.1",
6464
"es-check": "^5.1.2",
65-
"eslint": "^7.12.1",
65+
"eslint": "^7.13.0",
6666
"eslint-config-prettier": "^6.15.0",
6767
"eslint-plugin-import": "^2.22.1",
6868
"file-loader": "^6.2.0",
@@ -75,7 +75,7 @@
7575
"prettier": "^2.1.2",
7676
"standard-version": "^9.0.0",
7777
"webpack": "^5.4.0",
78-
"webpack-cli": "^4.1.0",
78+
"webpack-cli": "^4.2.0",
7979
"webpack-dev-server": "^3.7.2"
8080
},
8181
"keywords": [

Diff for: src/loader.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ export function pitch(request) {
8383
: compilation.hooks.normalModuleLoader;
8484

8585
normalModuleHook.tap(`${pluginName} loader`, (loaderContext, module) => {
86-
// eslint-disable-next-line no-param-reassign
87-
loaderContext.emitFile = this.emitFile;
88-
8986
if (module.request === request) {
9087
// eslint-disable-next-line no-param-reassign
9188
module.loaders = loaders.map((loader) => {
@@ -139,6 +136,21 @@ export function pitch(request) {
139136
const callback = this.async();
140137

141138
childCompiler.runAsChild((err, entries, compilation) => {
139+
for (const asset of compilation.getAssets()) {
140+
const { buildInfo } = this._module;
141+
142+
if (!buildInfo.assets) {
143+
buildInfo.assets = Object.create(null);
144+
}
145+
146+
if (!buildInfo.assetsInfo) {
147+
buildInfo.assetsInfo = new Map();
148+
}
149+
150+
buildInfo.assets[asset.name] = asset.source;
151+
buildInfo.assetsInfo.set(asset.name, asset.info);
152+
}
153+
142154
const addDependencies = (dependencies) => {
143155
if (!Array.isArray(dependencies) && dependencies != null) {
144156
throw new Error(

Diff for: test/TestCache.test.js

+262-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('TestCache', () => {
3030
});
3131

3232
const casesDirectory = path.resolve(__dirname, 'cases');
33-
const directoryForCase = path.resolve(casesDirectory, 'simple');
33+
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
3434
// eslint-disable-next-line import/no-dynamic-require, global-require
3535
const webpackConfig = require(path.resolve(
3636
directoryForCase,
@@ -58,7 +58,12 @@ describe('TestCache', () => {
5858
return;
5959
}
6060

61-
expect(stats.compilation.emittedAssets.size).toBe(2);
61+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
62+
'first'
63+
);
64+
expect(
65+
Array.from(stats.compilation.emittedAssets).sort()
66+
).toMatchSnapshot('first emitted');
6267
expect(stats.compilation.warnings).toHaveLength(0);
6368
expect(stats.compilation.errors).toHaveLength(0);
6469

@@ -86,8 +91,12 @@ describe('TestCache', () => {
8691
return;
8792
}
8893

89-
// Because webpack compare the source content before emitting
90-
expect(stats.compilation.emittedAssets.size).toBe(0);
94+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
95+
'second'
96+
);
97+
expect(
98+
Array.from(stats.compilation.emittedAssets).sort()
99+
).toMatchSnapshot('second emitted');
91100
expect(stats.compilation.warnings).toHaveLength(0);
92101
expect(stats.compilation.errors).toHaveLength(0);
93102

@@ -119,7 +128,7 @@ describe('TestCache', () => {
119128
});
120129

121130
const casesDirectory = path.resolve(__dirname, 'cases');
122-
const directoryForCase = path.resolve(casesDirectory, 'simple');
131+
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
123132
// eslint-disable-next-line import/no-dynamic-require, global-require
124133
const webpackConfig = require(path.resolve(
125134
directoryForCase,
@@ -149,7 +158,12 @@ describe('TestCache', () => {
149158
return;
150159
}
151160

152-
expect(stats.compilation.emittedAssets.size).toBe(2);
161+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
162+
'first'
163+
);
164+
expect(
165+
Array.from(stats.compilation.emittedAssets).sort()
166+
).toMatchSnapshot('first emitted');
153167
expect(stats.compilation.warnings).toHaveLength(0);
154168
expect(stats.compilation.errors).toHaveLength(0);
155169

@@ -179,7 +193,12 @@ describe('TestCache', () => {
179193
return;
180194
}
181195

182-
expect(stats.compilation.emittedAssets.size).toBe(0);
196+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
197+
'second'
198+
);
199+
expect(
200+
Array.from(stats.compilation.emittedAssets).sort()
201+
).toMatchSnapshot('second emitted');
183202
expect(stats.compilation.warnings).toHaveLength(0);
184203
expect(stats.compilation.errors).toHaveLength(0);
185204

@@ -248,7 +267,236 @@ describe('TestCache', () => {
248267
return;
249268
}
250269

251-
expect(stats.compilation.emittedAssets.size).toBe(2);
270+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
271+
'first'
272+
);
273+
expect(
274+
Array.from(stats.compilation.emittedAssets).sort()
275+
).toMatchSnapshot('first emitted');
276+
expect(stats.compilation.warnings).toHaveLength(0);
277+
expect(stats.compilation.errors).toHaveLength(0);
278+
279+
compiler1.close(() => {
280+
resolve();
281+
});
282+
});
283+
});
284+
285+
const compiler2 = webpack({
286+
...webpackConfig,
287+
mode: 'development',
288+
context: directoryForCase,
289+
cache: {
290+
type: 'filesystem',
291+
cacheDirectory: fileSystemCacheDirectory,
292+
idleTimeout: 0,
293+
idleTimeoutForInitialStore: 0,
294+
},
295+
output: {
296+
path: outputPath,
297+
},
298+
});
299+
300+
await new Promise((resolve, reject) => {
301+
compiler2.run((error, stats) => {
302+
if (error) {
303+
reject(error);
304+
305+
return;
306+
}
307+
308+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
309+
'second'
310+
);
311+
expect(
312+
Array.from(stats.compilation.emittedAssets).sort()
313+
).toMatchSnapshot('second emitted');
314+
expect(stats.compilation.warnings).toHaveLength(0);
315+
expect(stats.compilation.errors).toHaveLength(0);
316+
317+
compiler2.close(() => {
318+
resolve();
319+
});
320+
});
321+
});
322+
} else {
323+
expect(true).toBe(true);
324+
}
325+
});
326+
327+
it('should work with the "filesystem" cache and asset modules', async () => {
328+
if (webpack.version[0] !== '4') {
329+
const originalRegister = webpack.util.serialization.register;
330+
331+
webpack.util.serialization.register = jest
332+
.fn()
333+
.mockImplementation((...args) => {
334+
if (args[1].startsWith('mini-css-extract-plugin')) {
335+
// eslint-disable-next-line no-param-reassign
336+
args[1] = args[1].replace(/dist/, 'src');
337+
338+
return originalRegister(...args);
339+
}
340+
341+
return originalRegister(...args);
342+
});
343+
344+
const casesDirectory = path.resolve(__dirname, 'cases');
345+
const directoryForCase = path.resolve(casesDirectory, 'asset-modules');
346+
// eslint-disable-next-line import/no-dynamic-require, global-require
347+
const webpackConfig = require(path.resolve(
348+
directoryForCase,
349+
'webpack.config.js'
350+
));
351+
const outputPath = path.resolve(__dirname, 'js/cache-filesystem');
352+
const fileSystemCacheDirectory = path.resolve(
353+
__dirname,
354+
'./js/.cache/type-filesystem'
355+
);
356+
357+
await del([outputPath, fileSystemCacheDirectory]);
358+
359+
const compiler1 = webpack({
360+
...webpackConfig,
361+
mode: 'development',
362+
context: directoryForCase,
363+
cache: {
364+
type: 'filesystem',
365+
cacheDirectory: fileSystemCacheDirectory,
366+
idleTimeout: 0,
367+
idleTimeoutForInitialStore: 0,
368+
},
369+
output: {
370+
path: outputPath,
371+
},
372+
});
373+
374+
await new Promise((resolve, reject) => {
375+
compiler1.run((error, stats) => {
376+
if (error) {
377+
reject(error);
378+
379+
return;
380+
}
381+
382+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
383+
'first'
384+
);
385+
expect(
386+
Array.from(stats.compilation.emittedAssets).sort()
387+
).toMatchSnapshot('first emitted');
388+
expect(stats.compilation.warnings).toHaveLength(0);
389+
expect(stats.compilation.errors).toHaveLength(0);
390+
391+
compiler1.close(() => {
392+
resolve();
393+
});
394+
});
395+
});
396+
397+
const compiler2 = webpack({
398+
...webpackConfig,
399+
mode: 'development',
400+
context: directoryForCase,
401+
cache: {
402+
type: 'filesystem',
403+
cacheDirectory: fileSystemCacheDirectory,
404+
idleTimeout: 0,
405+
idleTimeoutForInitialStore: 0,
406+
},
407+
output: {
408+
path: outputPath,
409+
},
410+
});
411+
412+
await new Promise((resolve, reject) => {
413+
compiler2.run((error, stats) => {
414+
if (error) {
415+
reject(error);
416+
417+
return;
418+
}
419+
420+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
421+
'second'
422+
);
423+
expect(
424+
Array.from(stats.compilation.emittedAssets).sort()
425+
).toMatchSnapshot('second emitted');
426+
expect(stats.compilation.warnings).toHaveLength(0);
427+
expect(stats.compilation.errors).toHaveLength(0);
428+
429+
compiler2.close(() => {
430+
resolve();
431+
});
432+
});
433+
});
434+
} else {
435+
expect(true).toBe(true);
436+
}
437+
});
438+
439+
it('should work with the "filesystem" cache and file-loader', async () => {
440+
if (webpack.version[0] !== '4') {
441+
const originalRegister = webpack.util.serialization.register;
442+
443+
webpack.util.serialization.register = jest
444+
.fn()
445+
.mockImplementation((...args) => {
446+
if (args[1].startsWith('mini-css-extract-plugin')) {
447+
// eslint-disable-next-line no-param-reassign
448+
args[1] = args[1].replace(/dist/, 'src');
449+
450+
return originalRegister(...args);
451+
}
452+
453+
return originalRegister(...args);
454+
});
455+
456+
const casesDirectory = path.resolve(__dirname, 'cases');
457+
const directoryForCase = path.resolve(casesDirectory, 'file-loader');
458+
// eslint-disable-next-line import/no-dynamic-require, global-require
459+
const webpackConfig = require(path.resolve(
460+
directoryForCase,
461+
'webpack.config.js'
462+
));
463+
const outputPath = path.resolve(__dirname, 'js/cache-filesystem');
464+
const fileSystemCacheDirectory = path.resolve(
465+
__dirname,
466+
'./js/.cache/type-filesystem'
467+
);
468+
469+
await del([outputPath, fileSystemCacheDirectory]);
470+
471+
const compiler1 = webpack({
472+
...webpackConfig,
473+
mode: 'development',
474+
context: directoryForCase,
475+
cache: {
476+
type: 'filesystem',
477+
cacheDirectory: fileSystemCacheDirectory,
478+
idleTimeout: 0,
479+
idleTimeoutForInitialStore: 0,
480+
},
481+
output: {
482+
path: outputPath,
483+
},
484+
});
485+
486+
await new Promise((resolve, reject) => {
487+
compiler1.run((error, stats) => {
488+
if (error) {
489+
reject(error);
490+
491+
return;
492+
}
493+
494+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
495+
'first'
496+
);
497+
expect(
498+
Array.from(stats.compilation.emittedAssets).sort()
499+
).toMatchSnapshot('first emitted');
252500
expect(stats.compilation.warnings).toHaveLength(0);
253501
expect(stats.compilation.errors).toHaveLength(0);
254502

@@ -281,7 +529,12 @@ describe('TestCache', () => {
281529
return;
282530
}
283531

284-
expect(stats.compilation.emittedAssets.size).toBe(0);
532+
expect(Object.keys(stats.compilation.assets).sort()).toMatchSnapshot(
533+
'second'
534+
);
535+
expect(
536+
Array.from(stats.compilation.emittedAssets).sort()
537+
).toMatchSnapshot('second emitted');
285538
expect(stats.compilation.warnings).toHaveLength(0);
286539
expect(stats.compilation.errors).toHaveLength(0);
287540

0 commit comments

Comments
 (0)