Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: execute classic worker in dev mode #7099

Merged
merged 22 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
poyoho marked this conversation as resolved.
Show resolved Hide resolved
const commaIndex = options.indexOf(`,`)
const clearCode = code
.slice(i)
.replace(singlelineCommentsRE, blankReplacer)
.replace(multilineCommentsRE, blankReplacer)
const endIndex = clearCode.indexOf(')')
const commaIndex = clearCode.indexOf(',')
poyoho marked this conversation as resolved.
Show resolved Hide resolved
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, '')

poyoho marked this conversation as resolved.
Show resolved Hide resolved
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)