diff --git a/packages/vite/src/node/__tests__/cleanString.spec.ts b/packages/vite/src/node/__tests__/cleanString.spec.ts index b77f4c0fb5fbad..f307c4816b7cd3 100644 --- a/packages/vite/src/node/__tests__/cleanString.spec.ts +++ b/packages/vite/src/node/__tests__/cleanString.spec.ts @@ -1,3 +1,4 @@ +import { assetAttrsConfig } from './../plugins/html' import { emptyString } from '../../node/cleanString' test('comments', () => { @@ -51,6 +52,16 @@ test('escape character', () => { expect(clean).not.toMatch('1') }) +test('regexp affect', () => { + const clean = emptyString(` + /'/ + '1' + /"/ + "1" + `) + expect(clean).not.toMatch('1') +}) + test('strings comment nested', () => { expect( emptyString(` diff --git a/packages/vite/src/node/cleanString.ts b/packages/vite/src/node/cleanString.ts index 2b7cba41aef66b..3d623dce79c0d3 100644 --- a/packages/vite/src/node/cleanString.ts +++ b/packages/vite/src/node/cleanString.ts @@ -2,8 +2,9 @@ import type { RollupError } from 'rollup' // bank on the non-overlapping nature of regex matches and combine all filters into one giant regex // /`([^`\$\{\}]|\$\{(`|\g<1>)*\})*`/g can match nested string template // but js not support match expression(\g<0>). so clean string template(`...`) in other ways. -const cleanerRE = - /"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|\/\*(.|[\r\n])*?\*\/|\/\/.*/g +const stringsRE = /"([^"\r\n]|(?<=\\)")*"|'([^'\r\n]|(?<=\\)')*'/.source +const commentsRE = /\/\*(.|[\r\n])*?\*\/|\/\/.*/.source +const cleanerRE = new RegExp(`${stringsRE}|${commentsRE}`, 'g') const blankReplacer = (s: string) => ' '.repeat(s.length) const stringBlankReplacer = (s: string) =>