Skip to content

Commit 3f24533

Browse files
authored
fix(optimizer): handle more chars that will be sanitized (#22208)
1 parent e6e9fc9 commit 3f24533

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

packages/vite/src/node/__tests__/utils.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,36 @@ describe('flattenId', () => {
674674
const id = 'ravelinjs/core+track+encrypt+promise'
675675
const result = flattenId(id)
676676
expect(result).not.toContain('+')
677-
expect(result).toBe('ravelinjs_core_____track_____encrypt_____promise')
677+
expect(result).toBe('ravelinjs_core_02b_track_02b_encrypt_02b_promise')
678+
})
679+
680+
test('escape _', () => {
681+
expect(flattenId('foo_bar')).toMatchInlineSnapshot(`"foo___bar"`)
682+
expect(flattenId('foo__bar')).toMatchInlineSnapshot(`"foo____bar"`)
683+
expect(flattenId('foo___bar')).toMatchInlineSnapshot(`"foo_____bar"`)
684+
expect(flattenId('foo____bar')).toMatchInlineSnapshot(`"foo______bar"`)
685+
})
686+
687+
test('escape /', () => {
688+
expect(flattenId('foo/bar')).toMatchInlineSnapshot(`"foo_bar"`)
689+
})
690+
691+
test('escape .', () => {
692+
expect(flattenId('foo.bar')).toMatchInlineSnapshot(`"foo__bar"`)
693+
})
694+
695+
test('escape invalid URL path chars', () => {
696+
expect(flattenId('foo#bar')).toMatchInlineSnapshot(`"foo_023_bar"`)
697+
expect(flattenId('foo$bar')).toMatchInlineSnapshot(`"foo_024_bar"`)
698+
expect(flattenId('foo*bar')).toMatchInlineSnapshot(`"foo_02a_bar"`)
699+
expect(flattenId('foo+bar')).toMatchInlineSnapshot(`"foo_02b_bar"`)
700+
})
701+
702+
test('escape nested IDs', () => {
703+
expect(flattenId('foo>bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
704+
expect(flattenId('foo >bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
705+
expect(flattenId('foo> bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
706+
expect(flattenId('foo > bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
678707
})
679708
})
680709

packages/vite/src/node/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,21 @@ export const createFilter = _createFilter as (
6666

6767
export { withFilter } from 'rolldown/filter'
6868

69-
const replaceSlashOrColonRE = /[/:]/g
70-
const replaceDotRE = /\./g
69+
// eslint-disable-next-line no-control-regex
70+
const invalidUrlPathCharRE = /[\u0000-\u001F"#$%&*+,:;<=>?[\]^`{|}\u007F]/g
7171
const replaceNestedIdRE = /\s*>\s*/g
72-
const replaceHashRE = /#/g
73-
const replacePlusRE = /\+/g
7472
export const flattenId = (id: string): string => {
7573
const flatId = limitFlattenIdLength(
7674
id
77-
.replace(replaceSlashOrColonRE, '_')
78-
.replace(replaceDotRE, '__')
79-
.replace(replaceNestedIdRE, '___')
80-
.replace(replaceHashRE, '____')
81-
.replace(replacePlusRE, '_____'),
75+
.replaceAll(/_+/g, '$&__')
76+
.replaceAll('/', '_')
77+
.replaceAll('.', '__')
78+
.replace(replaceNestedIdRE, '_n_')
79+
// replace any characters that will be replaced by sanitizeFileName
80+
.replace(
81+
invalidUrlPathCharRE,
82+
(c) => '_0' + c.charCodeAt(0).toString(16) + '_',
83+
),
8284
)
8385
return flatId
8486
}

0 commit comments

Comments
 (0)