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

Function scanImports

packages/vite/src/node/optimizer/scan.ts:116–207  ·  view source on GitHub ↗
(environment: ScanEnvironment)

Source from the content-addressed store, hash-verified

114 /(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
115
116export function scanImports(environment: ScanEnvironment): {
117 cancel: () => Promise<void>
118 result: Promise<{
119 deps: Record<string, string>
120 missing: Record<string, string>
121 }>
122} {
123 const start = performance.now()
124 const { config } = environment
125
126 const scanContext = { cancelled: false }
127 async function cancel() {
128 scanContext.cancelled = true
129 }
130
131 async function scan() {
132 const entries = await computeEntries(environment)
133 if (!entries.length) {
134 if (!config.optimizeDeps.entries && !config.optimizeDeps.include) {
135 environment.logger.warn(
136 colors.yellow(
137 '(!) Could not auto-determine entry point from rolldownOptions or html files ' +
138 'and there are no explicit optimizeDeps.include patterns. ' +
139 'Skipping dependency pre-bundling.',
140 ),
141 )
142 }
143 return
144 }
145 if (scanContext.cancelled) return
146
147 debug?.(
148 `Crawling dependencies using entries: ${entries
149 .map((entry) => `\n ${colors.dim(entry)}`)
150 .join('')}`,
151 )
152 const deps: Record<string, string> = {}
153 const missing: Record<string, string> = {}
154
155 const context = await prepareRolldownScanner(
156 environment,
157 entries,
158 deps,
159 missing,
160 )
161 if (scanContext.cancelled) return
162
163 try {
164 await context.build()
165 return {
166 // Ensure a fixed order so hashes are stable and improve logs
167 deps: orderedDependencies(deps),
168 missing,
169 }
170 } catch (e) {
171 // The scanner runs in the background and may still be crawling when the
172 // server is closed. In that case resolutions reject with
173 // `ERR_CLOSED_SERVER` and the scan build fails.

Callers 2

scan.spec.tsFile · 0.90

Calls 1

scanFunction · 0.85

Tested by

no test coverage detected