MCPcopy
hub / github.com/vitejs/vite / parseWorkerOptions

Function parseWorkerOptions

packages/vite/src/node/plugins/workerImportMetaUrl.ts:96–133  ·  view source on GitHub ↗
(
  rawOpts: string,
  optsStartIndex: number,
)

Source from the content-addressed store, hash-verified

94}
95
96async function parseWorkerOptions(
97 rawOpts: string,
98 optsStartIndex: number,
99): Promise<WorkerOptions> {
100 let opts: WorkerOptions = {}
101 try {
102 opts = evalValue<WorkerOptions>(rawOpts)
103 } catch {
104 const optsNode = (
105 (await parseAstAsync(`(${rawOpts})`))
106 .body[0] as ESTree.ExpressionStatement
107 ).expression
108
109 const type = extractWorkerTypeFromAst(optsNode, optsStartIndex)
110 if (type) {
111 return { type }
112 }
113
114 throw err(
115 'Vite is unable to parse the worker options as the value is not static. ' +
116 'To ignore this error, please use /* @vite-ignore */ in the worker options.',
117 optsStartIndex,
118 )
119 }
120
121 if (opts == null) {
122 return {}
123 }
124
125 if (typeof opts !== 'object') {
126 throw err(
127 `Expected worker options to be an object, got ${typeof opts}`,
128 optsStartIndex,
129 )
130 }
131
132 return opts
133}
134
135async function getWorkerType(
136 raw: string,

Callers 1

getWorkerTypeFunction · 0.85

Calls 3

evalValueFunction · 0.90
extractWorkerTypeFromAstFunction · 0.85
errFunction · 0.70

Tested by

no test coverage detected