From 5f4ab17e17715e4bb2018c2c39be17e4d8eea53f Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 23 Apr 2022 15:22:45 +0800 Subject: [PATCH] fix: escaped --- .../src/node/__tests__/cleanString.spec.ts | 27 ++++--------------- packages/vite/src/node/cleanString.ts | 6 +++-- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/packages/vite/src/node/__tests__/cleanString.spec.ts b/packages/vite/src/node/__tests__/cleanString.spec.ts index ecef71a9695373..1d90ff31da1ea7 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', () => { @@ -55,32 +56,14 @@ test('regexp', () => { const clean = emptyString(` /'1\\'1'/ '1\\'1' - /"1\\"1"/ - "1\\"1" - /"1\\"1\\"1"/ - "1\\"1\\"1" - /"1\\'1'\\"1"/ - "1\\'1'\\"1" - /"1'1'"/ - "1'1'" - /"1'\\'1\\''\\"1\\"\\""/ - "1'\\'1\\''\\"1\\"\\"" - /'1"\\"1\\""\\"1\\"\\"'/ - '1"\\"1\\""\\"1\\"\\"' - /'""1""'/ - '""1""' - /'"""1"""'/ - '"""1"""' - /'""""1""""'/ - '""""1""""' - /"''1''"/ - "''1''" - /"'''1'''"/ - "'''1'''" /"''''1''''"/ "''''1''''" /'1'/ // '1' /'1'/ /* '1' */ + /'1'/ // '1' + /'1'/ /* '1' */ + /'\\/1'/ // '1' + /'\\/1'/ /* '1' */ `) expect(clean).not.toMatch('1') }) diff --git a/packages/vite/src/node/cleanString.ts b/packages/vite/src/node/cleanString.ts index 4a4f179fb26476..2e273f8dab57a5 100644 --- a/packages/vite/src/node/cleanString.ts +++ b/packages/vite/src/node/cleanString.ts @@ -2,8 +2,10 @@ 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 = /"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'/ +const commentsRE = /\/\*(.|[\r\n])*?\*\/|\/\/.*/ +const regexpRE = /\/([^\/\r\n]|(?<=\\)\/)*\// +const cleanerRE = new RegExp(`${stringsRE}|${commentsRE}|${regexpRE}`, 'g') const blankReplacer = (s: string) => ' '.repeat(s.length) const stringBlankReplacer = (s: string) =>