Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 11, 2024
1 parent 9ce1925 commit 0890b30
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions packages/nextlove/src/generators/extract-route-spec-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const extractRouteSpecs = async (opts: GenerateRouteTypesOpts) => {
.map((p) => `import {extractedRouteSpec as ${pathToId[p]}} from "./${p}"`)
.join("\n")}
export const routeSpecs = {
export const routes = {
${paths
.map((p) => {
const httpRoute = (
Expand All @@ -106,6 +106,11 @@ export const extractRouteSpecs = async (opts: GenerateRouteTypesOpts) => {
}
`

// Copy allowed import globs into project
if (opts.allowedImportPatterns) {
project.addSourceFilesAtPaths(opts.allowedImportPatterns)
}

// Generate types (.d.ts)
const entryPoint = project.createSourceFile(
"extracted-route-specs.ts",
Expand Down Expand Up @@ -146,40 +151,27 @@ export const extractRouteSpecs = async (opts: GenerateRouteTypesOpts) => {
...Object.keys(pkg.devDependencies),
],
plugins: [
// With this plugin, esbuild will never touch the actual filesystem and thus cannot interact with imports that don't match allowedImportPatterns[].
{
name: "resolve-virtual-fs",
setup(build) {
build.onLoad({ filter: /.*/ }, (args) => {
const contents = project.getSourceFile(args.path)?.getFullText()
if (!contents) {
return null
return {
contents: "export default {}",
loader: "ts",
}
}

return {
contents: contents ?? "",
contents,
loader: "ts",
resolveDir: path.dirname(args.path),
}
})
},
},
{
name: "allowed-imports",
setup(build) {
build.onLoad({ filter: /.*/ }, (args) => {
const isImportAllowed = micromatch.isMatch(
args.path,
opts.allowedImportPatterns ?? []
)

if (isImportAllowed) {
return
}

return { contents: `export default {}` }
})
},
},
],
})
}

0 comments on commit 0890b30

Please sign in to comment.