Skip to content

Commit

Permalink
refactor: extracting TS transform and transformGlobImport into a func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
candy-Tong committed Oct 26, 2022
1 parent 2f7d1c1 commit 361a9dc
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ function esbuildScanPlugin(
external: !entries.includes(path)
})

const doTransformGlobImport = async (
contents: string,
id: string,
loader: Loader
) => {
let transpiledContents
// transpile because `transformGlobImport` only expects js
if (loader !== 'js') {
transpiledContents = (await transform(contents, { loader })).code
} else {
transpiledContents = contents
}

const result = await transformGlobImport(
transpiledContents,
id,
config.root,
resolve
)

return result?.s.toString() || transpiledContents
}

return {
name: 'vite:dep-scan',
setup(build) {
Expand Down Expand Up @@ -305,26 +328,9 @@ function esbuildScanPlugin(

const key = `${path}?id=${scriptId++}`
if (contents.includes('import.meta.glob')) {
let transpiledContents
// transpile because `transformGlobImport` only expects js
if (loader !== 'js') {
transpiledContents = (await transform(contents, { loader }))
.code
} else {
transpiledContents = contents
}

scripts[key] = {
loader: 'js', // since it is transpiled
contents:
(
await transformGlobImport(
transpiledContents,
path,
config.root,
resolve
)
)?.s.toString() || transpiledContents,
contents: await doTransformGlobImport(contents, path, loader),
pluginData: {
htmlType: { loader }
}
Expand Down Expand Up @@ -497,10 +503,7 @@ function esbuildScanPlugin(
if (contents.includes('import.meta.glob')) {
return {
loader,
contents:
(
await transformGlobImport(contents, id, config.root, resolve)
)?.s.toString() || contents
contents: await doTransformGlobImport(contents, id, loader)
}
}

Expand Down

0 comments on commit 361a9dc

Please sign in to comment.