Skip to content

Commit e83687c

Browse files
committed
refactor: extracting TS transform and transformGlobImport into a function
1 parent 2f7d1c1 commit e83687c

File tree

1 file changed

+26
-23
lines changed
  • packages/vite/src/node/optimizer

1 file changed

+26
-23
lines changed

packages/vite/src/node/optimizer/scan.ts

+26-23
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ function esbuildScanPlugin(
200200
external: !entries.includes(path)
201201
})
202202

203+
const doTransformGlobImport = async (
204+
contents: string,
205+
id: string,
206+
loader: Loader
207+
) => {
208+
let transpiledContents
209+
// transpile because `transformGlobImport` only expects js
210+
if (loader !== 'js') {
211+
transpiledContents = (await transform(contents, { loader })).code
212+
} else {
213+
transpiledContents = contents
214+
}
215+
216+
const result = await transformGlobImport(
217+
transpiledContents,
218+
id,
219+
config.root,
220+
resolve
221+
)
222+
223+
return result?.s.toString() || transpiledContents
224+
}
225+
203226
return {
204227
name: 'vite:dep-scan',
205228
setup(build) {
@@ -305,26 +328,9 @@ function esbuildScanPlugin(
305328

306329
const key = `${path}?id=${scriptId++}`
307330
if (contents.includes('import.meta.glob')) {
308-
let transpiledContents
309-
// transpile because `transformGlobImport` only expects js
310-
if (loader !== 'js') {
311-
transpiledContents = (await transform(contents, { loader }))
312-
.code
313-
} else {
314-
transpiledContents = contents
315-
}
316-
317331
scripts[key] = {
318332
loader: 'js', // since it is transpiled
319-
contents:
320-
(
321-
await transformGlobImport(
322-
transpiledContents,
323-
path,
324-
config.root,
325-
resolve
326-
)
327-
)?.s.toString() || transpiledContents,
333+
contents: await doTransformGlobImport(contents, path, loader),
328334
pluginData: {
329335
htmlType: { loader }
330336
}
@@ -496,11 +502,8 @@ function esbuildScanPlugin(
496502

497503
if (contents.includes('import.meta.glob')) {
498504
return {
499-
loader,
500-
contents:
501-
(
502-
await transformGlobImport(contents, id, config.root, resolve)
503-
)?.s.toString() || contents
505+
loader: 'js', // since it is transpiled,
506+
contents: await doTransformGlobImport(contents, id, loader)
504507
}
505508
}
506509

0 commit comments

Comments
 (0)