Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,36 @@ describe('flattenId', () => {
const id = 'ravelinjs/core+track+encrypt+promise'
const result = flattenId(id)
expect(result).not.toContain('+')
expect(result).toBe('ravelinjs_core_____track_____encrypt_____promise')
expect(result).toBe('ravelinjs_core_02b_track_02b_encrypt_02b_promise')
})

test('escape _', () => {
expect(flattenId('foo_bar')).toMatchInlineSnapshot(`"foo___bar"`)
expect(flattenId('foo__bar')).toMatchInlineSnapshot(`"foo____bar"`)
expect(flattenId('foo___bar')).toMatchInlineSnapshot(`"foo_____bar"`)
expect(flattenId('foo____bar')).toMatchInlineSnapshot(`"foo______bar"`)
})

test('escape /', () => {
expect(flattenId('foo/bar')).toMatchInlineSnapshot(`"foo_bar"`)
})

test('escape .', () => {
expect(flattenId('foo.bar')).toMatchInlineSnapshot(`"foo__bar"`)
})

test('escape invalid URL path chars', () => {
expect(flattenId('foo#bar')).toMatchInlineSnapshot(`"foo_023_bar"`)
expect(flattenId('foo$bar')).toMatchInlineSnapshot(`"foo_024_bar"`)
expect(flattenId('foo*bar')).toMatchInlineSnapshot(`"foo_02a_bar"`)
expect(flattenId('foo+bar')).toMatchInlineSnapshot(`"foo_02b_bar"`)
})

test('escape nested IDs', () => {
expect(flattenId('foo>bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
expect(flattenId('foo >bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
expect(flattenId('foo> bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
expect(flattenId('foo > bar')).toMatchInlineSnapshot(`"foo_n_bar"`)
})
})

Expand Down
20 changes: 11 additions & 9 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ export const createFilter = _createFilter as (

export { withFilter } from 'rolldown/filter'

const replaceSlashOrColonRE = /[/:]/g
const replaceDotRE = /\./g
// eslint-disable-next-line no-control-regex
const invalidUrlPathCharRE = /[\u0000-\u001F"#$%&*+,:;<=>?[\]^`{|}\u007F]/g
const replaceNestedIdRE = /\s*>\s*/g
const replaceHashRE = /#/g
const replacePlusRE = /\+/g
export const flattenId = (id: string): string => {
const flatId = limitFlattenIdLength(
id
.replace(replaceSlashOrColonRE, '_')
.replace(replaceDotRE, '__')
.replace(replaceNestedIdRE, '___')
.replace(replaceHashRE, '____')
.replace(replacePlusRE, '_____'),
.replaceAll(/_+/g, '$&__')
.replaceAll('/', '_')
.replaceAll('.', '__')
.replace(replaceNestedIdRE, '_n_')
// replace any characters that will be replaced by sanitizeFileName
.replace(
invalidUrlPathCharRE,
(c) => '_0' + c.charCodeAt(0).toString(16) + '_',
),
)
return flatId
}
Expand Down