Skip to content

Commit

Permalink
feat: findIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Feb 28, 2022
1 parent 3a3a4d0 commit 2306744
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/playground/worker/classic-worker.js
Expand Up @@ -4,7 +4,7 @@ function text(el, text) {
}

const classicWorker = new Worker(
new URL('./newUrl/classic-worker.js', import.meta.url) ,
new URL('./newUrl/classic-worker.js', import.meta.url) /* , */ ,
// test comment

)
Expand Down
34 changes: 2 additions & 32 deletions packages/vite/src/node/importGlob.ts
Expand Up @@ -8,7 +8,7 @@ import {
preloadMarker
} from './plugins/importAnalysisBuild'
import { isCSSRequest } from './plugins/css'
import { cleanUrl } from './utils'
import { cleanUrl, findIndex } from './utils'
import type { RollupError } from 'rollup'

export interface AssertOptions {
Expand Down Expand Up @@ -183,7 +183,7 @@ function lexGlobPattern(
}
}

const endIndex = getEndIndex(code, i)
const endIndex = findIndex(code, i, ')')
const options = code.substring(i + 1, endIndex)
const commaIndex = options.indexOf(`,`)
let assert = {}
Expand All @@ -193,36 +193,6 @@ function lexGlobPattern(
return [pattern, assert, endIndex + 1]
}

// reg without the 'g' option, only matches the first match
const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//m
const singlelineCommentsRE = /\/\/.*/

function getEndIndex(code: string, i: number): number {
const findStart = i
const endIndex = code.indexOf(`)`, findStart)
const subCode = code.substring(findStart)

const matched =
subCode.match(singlelineCommentsRE) ?? subCode.match(multilineCommentsRE)
if (!matched) {
return endIndex
}

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 getEndIndex(code, commentEnd)
} else {
return endIndex
}
}

function error(pos: number) {
const err = new Error(
`import.meta.glob() can only accept string literals.`
Expand Down
36 changes: 7 additions & 29 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 } from '../utils'
import { parseRequest, findIndex } from '../utils'
import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants'
import MagicString from 'magic-string'
import type { ViteDevServer } from '..'
Expand All @@ -19,35 +19,13 @@ type WorkerType = 'classic' | 'module' | 'ignore'

const WORKER_FILE_ID = 'worker_url_file'

// find the workerOptions end with `)`
function lexWorkerOptions(code: string, pos: number) {
let pattern = ''
const opStack = []

for (let i = pos; i < code.length; i++) {
const char = code.charAt(i)
if (char === '(') {
opStack.push(char)
} else if (char === ')') {
if (opStack.length) {
opStack.pop()
} else {
break
}
}
pattern += char
}

// had `,` split worker params
const commaIndex = pattern.indexOf(',')
if (commaIndex > -1) {
pattern = pattern.substring(commaIndex + 1)
}
return pattern
}

function getWorkerType(code: string, i: number): WorkerType {
let workerOptsString = lexWorkerOptions(code, i)
const endIndex = findIndex(code, i, ')')
const commaIndex = findIndex(code, i, ',')
if (commaIndex === -1) {
return 'classic'
}
let workerOptsString = code.slice(commaIndex + 1, endIndex)
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(workerOptsString)
if (hasViteIgnore) {
return 'ignore'
Expand Down
43 changes: 43 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -642,3 +642,46 @@ export function parseRequest(id: string): Record<string, string> | null {
}
return Object.fromEntries(new URLSearchParams(search.slice(1)))
}

// find index ignore comment
export function findIndex(code: string, i: number, endChar: string): number {
// reg without the 'g' option, only matches the first match
const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//m
const singlelineCommentsRE = /\/\/.*/

const findStart = i
const endIndex = code.indexOf(endChar, findStart)
const subCode = code.substring(findStart)

const matchedSingleline = subCode.match(singlelineCommentsRE)
const matchedMultiline = subCode.match(multilineCommentsRE)

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
}
}

0 comments on commit 2306744

Please sign in to comment.