Skip to content

Commit ede0baf

Browse files
authored
Merge pull request #108 from yoriiis/fix/generateChunksFiles-ignored
Fix generateChunksFiles ignored
2 parents 07b37a9 + 70b31d1 commit ede0baf

File tree

6 files changed

+167
-116
lines changed

6 files changed

+167
-116
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 12.0.1
4+
5+
### Fixes
6+
7+
- Fix `generateChunksFiles` ignored [#108](https://github.com/yoriiis/chunks-webpack-plugin/pull/108)
8+
39
## 12.0.0
410

511
### ⚠️ Breaking changes

example/dist/js/328.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chunks-webpack-plugin",
3-
"version": "12.0.0",
3+
"version": "12.0.1",
44
"description": "Create HTML files with entrypoints and chunks relations to serve your bundles",
55
"keywords": ["chunks", "chunks-webpack-plugin", "plugin", "split chunks", "webpack"],
66
"homepage": "https://github.com/yoriiis/chunks-webpack-plugin",

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export default class ChunksWebpackPlugin {
127127
await cacheItem.storePromise(output);
128128
}
129129

130-
compilation.emitAsset(output.filename, output.source);
130+
if (this.options.generateChunksFiles) {
131+
compilation.emitAsset(output.filename, output.source);
132+
}
131133

132134
entryCssData.push({
133135
entryName,
@@ -164,7 +166,9 @@ export default class ChunksWebpackPlugin {
164166
await cacheItem.storePromise(output);
165167
}
166168

167-
compilation.emitAsset(output.filename, output.source);
169+
if (this.options.generateChunksFiles) {
170+
compilation.emitAsset(output.filename, output.source);
171+
}
168172

169173
entryJsData.push({
170174
entryName,

tests/index.test.js

Lines changed: 150 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ describe('ChunksWebpackPlugin', () => {
286286
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
287287
filename: 'templates/home-styles.html'
288288
});
289+
expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
290+
1,
291+
'templates/home-styles.html',
292+
{
293+
source:
294+
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
295+
}
296+
);
289297

290298
// JS
291299
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
@@ -335,6 +343,14 @@ describe('ChunksWebpackPlugin', () => {
335343
'<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>',
336344
filename: 'templates/home-scripts.html'
337345
});
346+
expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
347+
2,
348+
'templates/home-scripts.html',
349+
{
350+
source:
351+
'<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>'
352+
}
353+
);
338354

339355
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(7, {
340356
source:
@@ -363,128 +379,153 @@ describe('ChunksWebpackPlugin', () => {
363379
});
364380
});
365381

366-
it('Should call the addAssets function with dependencies and without cache with CSS only', async () => {
367-
chunksWebpackPlugin.getFilesDependenciesByEntrypoint = jest.fn().mockReturnValue({
368-
css: [
369-
{ name: 'a.css', source: 'module css a' },
370-
{ name: 'b.css', source: 'module css b' },
371-
{ name: 'c.css', source: 'module css c' }
372-
],
373-
js: []
374-
});
375-
chunksWebpackPlugin.getPublicPath = jest.fn().mockReturnValue('dist/');
376-
chunksWebpackPlugin.getAssetData = jest
377-
.fn()
378-
.mockReturnValueOnce({
379-
filePath: ['dist/abc.css', 'dist/def.css'],
380-
htmlTags:
382+
describe('Should call the addAssets function with dependencies and without cache with CSS only', () => {
383+
beforeEach(() => {
384+
chunksWebpackPlugin.getFilesDependenciesByEntrypoint = jest.fn().mockReturnValue({
385+
css: [
386+
{ name: 'a.css', source: 'module css a' },
387+
{ name: 'b.css', source: 'module css b' },
388+
{ name: 'c.css', source: 'module css c' }
389+
],
390+
js: []
391+
});
392+
chunksWebpackPlugin.getPublicPath = jest.fn().mockReturnValue('dist/');
393+
chunksWebpackPlugin.getAssetData = jest
394+
.fn()
395+
.mockReturnValueOnce({
396+
filePath: ['dist/abc.css', 'dist/def.css'],
397+
htmlTags:
398+
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
399+
})
400+
.mockReturnValueOnce({
401+
filePath: [],
402+
htmlTags: ''
403+
});
404+
chunksWebpackPlugin.createChunksManifestFile = jest.fn();
405+
compilationWebpack.compiler.webpack.sources.RawSource.mockReturnValueOnce({
406+
source:
381407
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
382-
})
383-
.mockReturnValueOnce({
384-
filePath: [],
385-
htmlTags: ''
386408
});
387-
chunksWebpackPlugin.createChunksManifestFile = jest.fn();
388-
compilationWebpack.compiler.webpack.sources.RawSource.mockReturnValueOnce({
389-
source:
390-
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
391-
});
392409

393-
compilationWebpack.entrypoints.keys.mockReturnValue(['home']);
394-
compilationWebpack.getCache.mockReturnValue({
395-
getLazyHashedEtag: jest
396-
.fn()
397-
.mockReturnValueOnce('module css a')
398-
.mockReturnValueOnce('module css b')
399-
.mockReturnValueOnce('module css c')
400-
.mockReturnValueOnce('css home'),
401-
mergeEtags: jest
402-
.fn()
403-
.mockReturnValueOnce('123456789123')
404-
.mockReturnValueOnce('123456789123'),
405-
getItemCache: jest.fn().mockReturnValue({
406-
getPromise: jest.fn(),
407-
storePromise: jest.fn()
408-
})
410+
compilationWebpack.entrypoints.keys.mockReturnValue(['home']);
411+
compilationWebpack.getCache.mockReturnValue({
412+
getLazyHashedEtag: jest
413+
.fn()
414+
.mockReturnValueOnce('module css a')
415+
.mockReturnValueOnce('module css b')
416+
.mockReturnValueOnce('module css c')
417+
.mockReturnValueOnce('css home'),
418+
mergeEtags: jest
419+
.fn()
420+
.mockReturnValueOnce('123456789123')
421+
.mockReturnValueOnce('123456789123'),
422+
getItemCache: jest.fn().mockReturnValue({
423+
getPromise: jest.fn(),
424+
storePromise: jest.fn()
425+
})
426+
});
409427
});
410428

411-
await chunksWebpackPlugin.addAssets(compilationWebpack);
412-
413-
expect(chunksWebpackPlugin.getFilesDependenciesByEntrypoint).toHaveBeenCalledWith({
414-
compilation: compilationWebpack,
415-
entryName: 'home'
416-
});
417-
expect(chunksWebpackPlugin.getPublicPath).toHaveBeenCalledWith(compilationWebpack, 'home');
429+
afterEach(() => {
430+
expect(chunksWebpackPlugin.getFilesDependenciesByEntrypoint).toHaveBeenCalledWith({
431+
compilation: compilationWebpack,
432+
entryName: 'home'
433+
});
434+
expect(chunksWebpackPlugin.getPublicPath).toHaveBeenCalledWith(compilationWebpack, 'home');
418435

419-
// CSS
420-
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
421-
1,
422-
'module css a'
423-
);
424-
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
425-
2,
426-
'module css b'
427-
);
428-
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
429-
3,
430-
'module css c'
431-
);
432-
expect(compilationWebpack.getCache().getItemCache).toHaveBeenNthCalledWith(
433-
1,
434-
'css|home',
435-
'123456789123'
436-
);
437-
expect(compilationWebpack.getCache().getItemCache().getPromise).toHaveBeenNthCalledWith(1);
438-
expect(chunksWebpackPlugin.getAssetData).toHaveBeenNthCalledWith(1, {
439-
templateFunction: chunksWebpackPlugin.options.templateStyle,
440-
assets: [
441-
{
442-
name: 'a.css',
443-
source: 'module css a'
444-
},
445-
{
446-
name: 'b.css',
447-
source: 'module css b'
448-
},
436+
// CSS
437+
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
438+
1,
439+
'module css a'
440+
);
441+
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
442+
2,
443+
'module css b'
444+
);
445+
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
446+
3,
447+
'module css c'
448+
);
449+
expect(compilationWebpack.getCache().getItemCache).toHaveBeenNthCalledWith(
450+
1,
451+
'css|home',
452+
'123456789123'
453+
);
454+
expect(compilationWebpack.getCache().getItemCache().getPromise).toHaveBeenNthCalledWith(1);
455+
expect(chunksWebpackPlugin.getAssetData).toHaveBeenNthCalledWith(1, {
456+
templateFunction: chunksWebpackPlugin.options.templateStyle,
457+
assets: [
458+
{
459+
name: 'a.css',
460+
source: 'module css a'
461+
},
462+
{
463+
name: 'b.css',
464+
source: 'module css b'
465+
},
466+
{
467+
name: 'c.css',
468+
source: 'module css c'
469+
}
470+
],
471+
entryName: 'home',
472+
publicPath: 'dist/'
473+
});
474+
expect(compilationWebpack.getCache().getItemCache().storePromise).toHaveBeenNthCalledWith(
475+
1,
449476
{
450-
name: 'c.css',
451-
source: 'module css c'
477+
source: {
478+
source:
479+
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
480+
},
481+
filePath: ['dist/abc.css', 'dist/def.css'],
482+
htmlTags:
483+
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
484+
filename: 'templates/home-styles.html'
452485
}
453-
],
454-
entryName: 'home',
455-
publicPath: 'dist/'
456-
});
457-
expect(compilationWebpack.getCache().getItemCache().storePromise).toHaveBeenNthCalledWith(1, {
458-
source: {
486+
);
487+
488+
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(4, {
459489
source:
460490
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
461-
},
462-
filePath: ['dist/abc.css', 'dist/def.css'],
463-
htmlTags:
464-
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
465-
filename: 'templates/home-styles.html'
491+
});
492+
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenCalledTimes(4);
493+
expect(compilationWebpack.getCache().mergeEtags).toHaveBeenCalledTimes(2);
494+
expect(chunksWebpackPlugin.createChunksManifestFile).toHaveBeenCalledWith({
495+
compilation: compilationWebpack,
496+
cache: {
497+
getLazyHashedEtag: expect.any(Function),
498+
mergeEtags: expect.any(Function),
499+
getItemCache: expect.any(Function)
500+
},
501+
eTag: 'css home', // Because reduce is not executed when array contains only one item
502+
manifest: {
503+
home: {
504+
scripts: [],
505+
styles: ['dist/abc.css', 'dist/def.css']
506+
}
507+
}
508+
});
466509
});
467510

468-
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(4, {
469-
source:
470-
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
471-
});
472-
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenCalledTimes(4);
473-
expect(compilationWebpack.getCache().mergeEtags).toHaveBeenCalledTimes(2);
474-
expect(chunksWebpackPlugin.createChunksManifestFile).toHaveBeenCalledWith({
475-
compilation: compilationWebpack,
476-
cache: {
477-
getLazyHashedEtag: expect.any(Function),
478-
mergeEtags: expect.any(Function),
479-
getItemCache: expect.any(Function)
480-
},
481-
eTag: 'css home', // Because reduce is not executed when array contains only one item
482-
manifest: {
483-
home: {
484-
scripts: [],
485-
styles: ['dist/abc.css', 'dist/def.css']
511+
it('With generateChunksFiles true', async () => {
512+
await chunksWebpackPlugin.addAssets(compilationWebpack);
513+
514+
expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
515+
1,
516+
'templates/home-styles.html',
517+
{
518+
source:
519+
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
486520
}
487-
}
521+
);
522+
});
523+
524+
it('With generateChunksFiles false', async () => {
525+
chunksWebpackPlugin.options.generateChunksFiles = false;
526+
await chunksWebpackPlugin.addAssets(compilationWebpack);
527+
528+
expect(compilationWebpack.emitAsset).not.toHaveBeenCalled();
488529
});
489530
});
490531
});

0 commit comments

Comments
 (0)