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

Function parseGlobOptions

packages/vite/src/node/plugins/importMetaGlob.ts:153–249  ·  view source on GitHub ↗
(
  rawOpts: string,
  optsStartIndex: number,
  logger?: Logger,
)

Source from the content-addressed store, hash-verified

151}
152
153function parseGlobOptions(
154 rawOpts: string,
155 optsStartIndex: number,
156 logger?: Logger,
157): ParsedGeneralImportGlobOptions {
158 let opts: GeneralImportGlobOptions = {}
159 try {
160 opts = evalValue(rawOpts)
161 } catch {
162 throw err(
163 'Vite is unable to parse the glob options as the value is not static',
164 optsStartIndex,
165 )
166 }
167
168 if (opts == null) {
169 return {}
170 }
171
172 for (const key in opts) {
173 if (!(key in knownOptions)) {
174 throw err(`Unknown glob option "${key}"`, optsStartIndex)
175 }
176 const allowedTypes = knownOptions[key as keyof typeof knownOptions]
177 const valueType = typeof opts[key as keyof GeneralImportGlobOptions]
178 if (!allowedTypes.includes(valueType)) {
179 throw err(
180 `Expected glob option "${key}" to be of type ${allowedTypes.join(
181 ' or ',
182 )}, but got ${valueType}`,
183 optsStartIndex,
184 )
185 }
186 }
187
188 if (opts.base) {
189 if (opts.base[0] === '!') {
190 throw err('Option "base" cannot start with "!"', optsStartIndex)
191 } else if (
192 opts.base[0] !== '/' &&
193 !opts.base.startsWith('./') &&
194 !opts.base.startsWith('../')
195 ) {
196 throw err(
197 `Option "base" must start with '/', './' or '../', but got "${opts.base}"`,
198 optsStartIndex,
199 )
200 }
201 }
202
203 if (typeof opts.query === 'object') {
204 for (const key in opts.query) {
205 const value = opts.query[key]
206 if (!['string', 'number', 'boolean'].includes(typeof value)) {
207 throw err(
208 `Expected glob option "query.${key}" to be of type string, number, or boolean, but got ${typeof value}`,
209 optsStartIndex,
210 )

Callers 1

parseImportGlobFunction · 0.85

Calls 3

evalValueFunction · 0.90
errFunction · 0.70
warnMethod · 0.65

Tested by

no test coverage detected