Skip to content

Commit

Permalink
[#129] fix: correctly remove multiple /* empty css */ multiple occurr…
Browse files Browse the repository at this point in the history
…ences (#130)
  • Loading branch information
sondreluc authored and marco-prontera committed Dec 20, 2023
1 parent 22dec44 commit 1f6c9ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export function buildOutputChunkWithCssInjectionCode(
cssInjectionCode: string,
topExecutionPriorityFlag: boolean
): string {
const appCode = jsAssetCode.replace(/\/\*.*empty css.*\*\//, '');
const appCode = jsAssetCode.replaceAll(/\/\*\s*empty css\s*\*\//g, '');

This comment has been minimized.

Copy link
@drbeat

drbeat Jan 11, 2024

Contributor

replaceAll breaks compatibility with nodeJs 14 as it is only available from NodeJs 15+, could you make it compatible with nodejs 14 again or would you accept a PR if I submit a fix?

edit: as you have a regex pattern with /g you dont need to use replaceAll and replace would do the same

jsAssetCode = topExecutionPriorityFlag ? '' : appCode;
jsAssetCode += cssInjectionCode;
jsAssetCode += !topExecutionPriorityFlag ? '' : appCode;
Expand Down
18 changes: 18 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ describe('utils', () => {

expect(bundle['a.js'].code).toEqual('a');
});

it('should remove occurrences of /* empty css */ from the bundled code', async () => {
bundle['a.js'] = generateJsChunk('a', ['a.css']);
bundle['a.js'].code = `/* empty css */${bundle['a.js'].code}/* empty css */`;
await relativeCssInjection(bundle, buildJsCssMap(bundle), buildCssCodeMock, true);

expect(bundle['a.js'].code).toEqual('aa');
});
});

describe('globalCssInjection', () => {
Expand Down Expand Up @@ -550,6 +558,16 @@ describe('utils', () => {
expect(bundle['a.js'].code).toEqual('abca');
});

it('should remove occurrences of /* empty css */ from the bundled code', async () => {
const cssAssets = ['a.css', 'b.css', 'c.css'];
bundle['a.js'] = generateJsChunk('a', cssAssets, true);
bundle['a.js'].code = `/* empty css */${bundle['a.js'].code}/* empty css */`;

await globalCssInjection(bundle, cssAssets, buildCssCodeMock, undefined, true);

expect(bundle['a.js'].code).toEqual('abca');
});

it('should inject all css should throw if no entry is available', async () => {
const cssAssets = ['a.css', 'b.css', 'c.css'];
bundle['a.js'] = generateJsChunk('a', cssAssets, false);
Expand Down

0 comments on commit 1f6c9ec

Please sign in to comment.