Skip to content

Commit

Permalink
Merge pull request #108 from yoriiis/fix/generateChunksFiles-ignored
Browse files Browse the repository at this point in the history
Fix generateChunksFiles ignored
  • Loading branch information
yoriiis authored Jan 13, 2025
2 parents 07b37a9 + 70b31d1 commit ede0baf
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 116 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 12.0.1

### Fixes

- Fix `generateChunksFiles` ignored [#108](https://github.com/yoriiis/chunks-webpack-plugin/pull/108)

## 12.0.0

### ⚠️ Breaking changes
Expand Down
4 changes: 2 additions & 2 deletions example/dist/js/328.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chunks-webpack-plugin",
"version": "12.0.0",
"version": "12.0.1",
"description": "Create HTML files with entrypoints and chunks relations to serve your bundles",
"keywords": ["chunks", "chunks-webpack-plugin", "plugin", "split chunks", "webpack"],
"homepage": "https://github.com/yoriiis/chunks-webpack-plugin",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export default class ChunksWebpackPlugin {
await cacheItem.storePromise(output);
}

compilation.emitAsset(output.filename, output.source);
if (this.options.generateChunksFiles) {
compilation.emitAsset(output.filename, output.source);
}

entryCssData.push({
entryName,
Expand Down Expand Up @@ -164,7 +166,9 @@ export default class ChunksWebpackPlugin {
await cacheItem.storePromise(output);
}

compilation.emitAsset(output.filename, output.source);
if (this.options.generateChunksFiles) {
compilation.emitAsset(output.filename, output.source);
}

entryJsData.push({
entryName,
Expand Down
259 changes: 150 additions & 109 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ describe('ChunksWebpackPlugin', () => {
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
filename: 'templates/home-styles.html'
});
expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
1,
'templates/home-styles.html',
{
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
}
);

// JS
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -335,6 +343,14 @@ describe('ChunksWebpackPlugin', () => {
'<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>',
filename: 'templates/home-scripts.html'
});
expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
2,
'templates/home-scripts.html',
{
source:
'<script defer src="https://cdn.domain.com/dist/abc.js"></script><script defer src="https://cdn.domain.com/dist/def.js"></script>'
}
);

expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(7, {
source:
Expand Down Expand Up @@ -363,128 +379,153 @@ describe('ChunksWebpackPlugin', () => {
});
});

it('Should call the addAssets function with dependencies and without cache with CSS only', async () => {
chunksWebpackPlugin.getFilesDependenciesByEntrypoint = jest.fn().mockReturnValue({
css: [
{ name: 'a.css', source: 'module css a' },
{ name: 'b.css', source: 'module css b' },
{ name: 'c.css', source: 'module css c' }
],
js: []
});
chunksWebpackPlugin.getPublicPath = jest.fn().mockReturnValue('dist/');
chunksWebpackPlugin.getAssetData = jest
.fn()
.mockReturnValueOnce({
filePath: ['dist/abc.css', 'dist/def.css'],
htmlTags:
describe('Should call the addAssets function with dependencies and without cache with CSS only', () => {
beforeEach(() => {
chunksWebpackPlugin.getFilesDependenciesByEntrypoint = jest.fn().mockReturnValue({
css: [
{ name: 'a.css', source: 'module css a' },
{ name: 'b.css', source: 'module css b' },
{ name: 'c.css', source: 'module css c' }
],
js: []
});
chunksWebpackPlugin.getPublicPath = jest.fn().mockReturnValue('dist/');
chunksWebpackPlugin.getAssetData = jest
.fn()
.mockReturnValueOnce({
filePath: ['dist/abc.css', 'dist/def.css'],
htmlTags:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
})
.mockReturnValueOnce({
filePath: [],
htmlTags: ''
});
chunksWebpackPlugin.createChunksManifestFile = jest.fn();
compilationWebpack.compiler.webpack.sources.RawSource.mockReturnValueOnce({
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
})
.mockReturnValueOnce({
filePath: [],
htmlTags: ''
});
chunksWebpackPlugin.createChunksManifestFile = jest.fn();
compilationWebpack.compiler.webpack.sources.RawSource.mockReturnValueOnce({
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
});

compilationWebpack.entrypoints.keys.mockReturnValue(['home']);
compilationWebpack.getCache.mockReturnValue({
getLazyHashedEtag: jest
.fn()
.mockReturnValueOnce('module css a')
.mockReturnValueOnce('module css b')
.mockReturnValueOnce('module css c')
.mockReturnValueOnce('css home'),
mergeEtags: jest
.fn()
.mockReturnValueOnce('123456789123')
.mockReturnValueOnce('123456789123'),
getItemCache: jest.fn().mockReturnValue({
getPromise: jest.fn(),
storePromise: jest.fn()
})
compilationWebpack.entrypoints.keys.mockReturnValue(['home']);
compilationWebpack.getCache.mockReturnValue({
getLazyHashedEtag: jest
.fn()
.mockReturnValueOnce('module css a')
.mockReturnValueOnce('module css b')
.mockReturnValueOnce('module css c')
.mockReturnValueOnce('css home'),
mergeEtags: jest
.fn()
.mockReturnValueOnce('123456789123')
.mockReturnValueOnce('123456789123'),
getItemCache: jest.fn().mockReturnValue({
getPromise: jest.fn(),
storePromise: jest.fn()
})
});
});

await chunksWebpackPlugin.addAssets(compilationWebpack);

expect(chunksWebpackPlugin.getFilesDependenciesByEntrypoint).toHaveBeenCalledWith({
compilation: compilationWebpack,
entryName: 'home'
});
expect(chunksWebpackPlugin.getPublicPath).toHaveBeenCalledWith(compilationWebpack, 'home');
afterEach(() => {
expect(chunksWebpackPlugin.getFilesDependenciesByEntrypoint).toHaveBeenCalledWith({
compilation: compilationWebpack,
entryName: 'home'
});
expect(chunksWebpackPlugin.getPublicPath).toHaveBeenCalledWith(compilationWebpack, 'home');

// CSS
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
1,
'module css a'
);
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
2,
'module css b'
);
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
3,
'module css c'
);
expect(compilationWebpack.getCache().getItemCache).toHaveBeenNthCalledWith(
1,
'css|home',
'123456789123'
);
expect(compilationWebpack.getCache().getItemCache().getPromise).toHaveBeenNthCalledWith(1);
expect(chunksWebpackPlugin.getAssetData).toHaveBeenNthCalledWith(1, {
templateFunction: chunksWebpackPlugin.options.templateStyle,
assets: [
{
name: 'a.css',
source: 'module css a'
},
{
name: 'b.css',
source: 'module css b'
},
// CSS
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
1,
'module css a'
);
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
2,
'module css b'
);
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(
3,
'module css c'
);
expect(compilationWebpack.getCache().getItemCache).toHaveBeenNthCalledWith(
1,
'css|home',
'123456789123'
);
expect(compilationWebpack.getCache().getItemCache().getPromise).toHaveBeenNthCalledWith(1);
expect(chunksWebpackPlugin.getAssetData).toHaveBeenNthCalledWith(1, {
templateFunction: chunksWebpackPlugin.options.templateStyle,
assets: [
{
name: 'a.css',
source: 'module css a'
},
{
name: 'b.css',
source: 'module css b'
},
{
name: 'c.css',
source: 'module css c'
}
],
entryName: 'home',
publicPath: 'dist/'
});
expect(compilationWebpack.getCache().getItemCache().storePromise).toHaveBeenNthCalledWith(
1,
{
name: 'c.css',
source: 'module css c'
source: {
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
},
filePath: ['dist/abc.css', 'dist/def.css'],
htmlTags:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
filename: 'templates/home-styles.html'
}
],
entryName: 'home',
publicPath: 'dist/'
});
expect(compilationWebpack.getCache().getItemCache().storePromise).toHaveBeenNthCalledWith(1, {
source: {
);

expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(4, {
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
},
filePath: ['dist/abc.css', 'dist/def.css'],
htmlTags:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />',
filename: 'templates/home-styles.html'
});
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenCalledTimes(4);
expect(compilationWebpack.getCache().mergeEtags).toHaveBeenCalledTimes(2);
expect(chunksWebpackPlugin.createChunksManifestFile).toHaveBeenCalledWith({
compilation: compilationWebpack,
cache: {
getLazyHashedEtag: expect.any(Function),
mergeEtags: expect.any(Function),
getItemCache: expect.any(Function)
},
eTag: 'css home', // Because reduce is not executed when array contains only one item
manifest: {
home: {
scripts: [],
styles: ['dist/abc.css', 'dist/def.css']
}
}
});
});

expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenNthCalledWith(4, {
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
});
expect(compilationWebpack.getCache().getLazyHashedEtag).toHaveBeenCalledTimes(4);
expect(compilationWebpack.getCache().mergeEtags).toHaveBeenCalledTimes(2);
expect(chunksWebpackPlugin.createChunksManifestFile).toHaveBeenCalledWith({
compilation: compilationWebpack,
cache: {
getLazyHashedEtag: expect.any(Function),
mergeEtags: expect.any(Function),
getItemCache: expect.any(Function)
},
eTag: 'css home', // Because reduce is not executed when array contains only one item
manifest: {
home: {
scripts: [],
styles: ['dist/abc.css', 'dist/def.css']
it('With generateChunksFiles true', async () => {
await chunksWebpackPlugin.addAssets(compilationWebpack);

expect(compilationWebpack.emitAsset).toHaveBeenNthCalledWith(
1,
'templates/home-styles.html',
{
source:
'<link rel="stylesheet" href="https://cdn.domain.com/dist/abc.css" /><link rel="stylesheet" href="https://cdn.domain.com/dist/def.css" />'
}
}
);
});

it('With generateChunksFiles false', async () => {
chunksWebpackPlugin.options.generateChunksFiles = false;
await chunksWebpackPlugin.addAssets(compilationWebpack);

expect(compilationWebpack.emitAsset).not.toHaveBeenCalled();
});
});
});
Expand Down

0 comments on commit ede0baf

Please sign in to comment.