Skip to content

Commit

Permalink
fix: find index ignore comment
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Mar 1, 2022
1 parent 9297e45 commit 38d9b20
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 58 deletions.
19 changes: 13 additions & 6 deletions packages/vite/src/node/importGlob.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
Expand Down
20 changes: 10 additions & 10 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -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 '..'
Expand All @@ -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'
}

Expand Down
43 changes: 1 addition & 42 deletions packages/vite/src/node/utils.ts
Expand Up @@ -643,45 +643,4 @@ export function parseRequest(id: string): Record<string, string> | 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)

0 comments on commit 38d9b20

Please sign in to comment.