From 38d9b20dadb181b874481ba4d03deb23a479a579 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Tue, 1 Mar 2022 16:06:46 +0800 Subject: [PATCH] fix: find index ignore comment --- packages/vite/src/node/importGlob.ts | 19 +++++--- .../src/node/plugins/workerImportMetaUrl.ts | 20 ++++----- packages/vite/src/node/utils.ts | 43 +------------------ 3 files changed, 24 insertions(+), 58 deletions(-) diff --git a/packages/vite/src/node/importGlob.ts b/packages/vite/src/node/importGlob.ts index 04c4f57a2cf263..9b1d0cc95d8e82 100644 --- a/packages/vite/src/node/importGlob.ts +++ b/packages/vite/src/node/importGlob.ts @@ -8,7 +8,12 @@ import { preloadMarker } from './plugins/importAnalysisBuild' import { isCSSRequest } from './plugins/css' -import { cleanUrl, findIndex } from './utils' +import { + cleanUrl, + singlelineCommentsRE, + multilineCommentsRE, + blankReplacer +} from './utils' import type { RollupError } from 'rollup' export interface AssertOptions { @@ -182,13 +187,15 @@ function lexGlobPattern( throw new Error('unknown import.meta.glob lexer state') } } - - const endIndex = findIndex(code, i, ')') - const options = code.substring(i + 1, endIndex) - const commaIndex = options.indexOf(`,`) + const clearCode = code + .slice(i) + .replace(singlelineCommentsRE, blankReplacer) + .replace(multilineCommentsRE, blankReplacer) + const endIndex = clearCode.indexOf(')') + const commaIndex = clearCode.indexOf(',') let assert = {} if (commaIndex > -1) { - assert = JSON5.parse(options.substr(commaIndex + 1)) + assert = JSON5.parse(clearCode.substring(commaIndex + 1)) } return [pattern, assert, endIndex + 1] } diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index fd2580af9eb477..48181da953173e 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -10,7 +10,7 @@ import { } from '../utils' import path from 'path' import { bundleWorkerEntry } from './worker' -import { parseRequest, findIndex } from '../utils' +import { parseRequest } from '../utils' import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants' import MagicString from 'magic-string' import type { ViteDevServer } from '..' @@ -20,23 +20,23 @@ type WorkerType = 'classic' | 'module' | 'ignore' const WORKER_FILE_ID = 'worker_url_file' function getWorkerType(code: string, i: number): WorkerType { - const commaIndex = findIndex(code, i, ',') + const clearCode = code + .slice(i) + .replace(singlelineCommentsRE, '') + .replace(multilineCommentsRE, '') + + const commaIndex = clearCode.indexOf(',') if (commaIndex === -1) { return 'classic' } - const endIndex = findIndex(code, i, ')') - let workerOptsString = code.slice(commaIndex + 1, endIndex) + const endIndex = clearCode.indexOf(')') + const workerOptsString = code.substring(commaIndex + 1, endIndex) const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(workerOptsString) if (hasViteIgnore) { return 'ignore' } - workerOptsString = workerOptsString - .replace(multilineCommentsRE, '') - .replace(singlelineCommentsRE, '') - .trim() - - if (!workerOptsString.length) { + if (!workerOptsString.trim().length) { return 'classic' } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index b91f043a077eac..645c2c8ddf0aa6 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -643,45 +643,4 @@ export function parseRequest(id: string): Record | null { return Object.fromEntries(new URLSearchParams(search.slice(1))) } -// reg without the 'g' option, only matches the first match -const multilineCommentsOnceRE = /\/\*(.|[\r\n])*?\*\//m -const singlelineCommentsOnceRE = /\/\/.*/ - -// find index ignore comment -export function findIndex(code: string, i: number, endChar: string): number { - const findStart = i - const endIndex = code.indexOf(endChar, findStart) - const subCode = code.substring(findStart) - - const matchedSingleline = subCode.match(singlelineCommentsOnceRE) - const matchedMultiline = subCode.match(multilineCommentsOnceRE) - - if (!matchedSingleline && !matchedMultiline) { - return endIndex - } - - let matched: RegExpMatchArray - if (!matchedSingleline || !matchedMultiline) { - matched = matchedSingleline ?? matchedMultiline! - } else { - if (matchedSingleline.index! > matchedMultiline.index!) { - matched = matchedMultiline - } else { - matched = matchedSingleline - } - } - - const str = matched[0] - const index = matched.index - if (!index) { - return endIndex - } - - const commentStart = findStart + index - const commentEnd = commentStart + str.length - if (endIndex > commentStart && endIndex < commentEnd) { - return findIndex(code, commentEnd, endChar) - } else { - return endIndex - } -} +export const blankReplacer = (match: string) => ' '.repeat(match.length)