Skip to content

Commit 188572e

Browse files
committed
Fix sprite name equal to entry name when contains slashes
1 parent 3ab7268 commit 188572e

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,21 @@ class SvgChunkWebpackPlugin {
306306
entryName: string;
307307
sprite: string;
308308
}): string {
309-
let filename = compilation.getPath(this.options.filename, {
310-
filename: entryName
311-
});
309+
let filename = this.options.filename;
310+
311+
// Check if the filename option contains the placeholder [name]
312+
// [name] corresponds to the entrypoint name
313+
if (/\[name\]/i.test(filename)) {
314+
filename = filename.replace('[name]', entryName);
315+
}
316+
317+
// Check if the filename option contains the placeholder [fullhash]
318+
// [fullhash] corresponds to the Webpack compilation hash
319+
if (/\[fullhash\]/i.test(filename)) {
320+
const { hashDigestLength } = compilation.outputOptions;
321+
const hash = compilation.fullHash.substring(0, hashDigestLength);
322+
filename = filename.replace('[fullhash]', hash);
323+
}
312324

313325
// Check if the filename option contains the placeholder [contenthash]
314326
// [contenthash] corresponds to the sprite content hash

tests/index.test.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,20 +472,45 @@ describe('SvgChunkWebpackPlugin', () => {
472472

473473
describe('SvgChunkWebpackPlugin getFilename', () => {
474474
it('Should call the getFilename function with default name', () => {
475-
svgChunkWebpackPlugin.options.filename = 'sprite/[name].svg';
476-
compilationWebpack.getPath.mockReturnValue('sprite/home.svg');
475+
svgChunkWebpackPlugin.options.filename = 'sprites/[name].svg';
477476

478477
const result = svgChunkWebpackPlugin.getFilename({
479478
compilation: compilationWebpack,
480479
entryName: 'home',
481480
output: spritesFixture.home
482481
});
483482

484-
expect(compilationWebpack.getPath).toHaveBeenCalledWith('sprite/[name].svg', {
485-
filename: 'home'
483+
expect(compilationWebpack.compiler.webpack.util.createHash).not.toHaveBeenCalled();
484+
expect(result).toBe('sprites/home.svg');
485+
});
486+
487+
it('Should call the getFilename function with default name with slashes', () => {
488+
svgChunkWebpackPlugin.options.filename = 'sprites/[name].svg';
489+
490+
const result = svgChunkWebpackPlugin.getFilename({
491+
compilation: compilationWebpack,
492+
entryName: 'home/components/footer',
493+
output: spritesFixture.home
486494
});
495+
487496
expect(compilationWebpack.compiler.webpack.util.createHash).not.toHaveBeenCalled();
488-
expect(result).toBe('sprite/home.svg');
497+
expect(result).toBe('sprites/home/components/footer.svg');
498+
});
499+
500+
it('Should call the getFilename function with [fullhash]', () => {
501+
svgChunkWebpackPlugin.options.filename = 'sprites/[name].[fullhash].svg';
502+
compilationWebpack.outputOptions = {
503+
hashDigestLength: 20
504+
};
505+
compilationWebpack.fullHash = '117b8f68975f36a8c463a1b2c3d4e5f6';
506+
507+
const result = svgChunkWebpackPlugin.getFilename({
508+
compilation: compilationWebpack,
509+
entryName: 'home',
510+
sprite: spritesFixture.home
511+
});
512+
513+
expect(result).toStrictEqual('sprites/home.117b8f68975f36a8c463.svg');
489514
});
490515

491516
it('Should call the getFilename function with [contenthash]', () => {
@@ -495,19 +520,20 @@ describe('SvgChunkWebpackPlugin', () => {
495520
hashDigest: 'hex',
496521
hashDigestLength: 20
497522
};
498-
compilationWebpack.getPath.mockReturnValue('sprites/home.[contenthash].svg');
499523
compilationWebpack.compiler.webpack.util.createHash.mockReturnValue({
500524
update: jest.fn().mockReturnValue({
501525
digest: jest.fn().mockReturnValue({
502526
substring: jest.fn().mockReturnValue('a1b2c3d4e5f6')
503527
})
504528
})
505529
});
530+
506531
const result = svgChunkWebpackPlugin.getFilename({
507532
compilation: compilationWebpack,
508533
entryName: 'home',
509534
sprite: spritesFixture.home
510535
});
536+
511537
expect(compilationWebpack.compiler.webpack.util.createHash).toHaveBeenCalledWith('md4');
512538
expect(
513539
compilationWebpack.compiler.webpack.util.createHash().update

0 commit comments

Comments
 (0)