From 0890b3028cfe04faf792c5ce248b88f6dce69000 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Thu, 11 Jan 2024 15:52:56 -0800 Subject: [PATCH] Simplify --- .../generators/extract-route-spec-schemas.ts | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/nextlove/src/generators/extract-route-spec-schemas.ts b/packages/nextlove/src/generators/extract-route-spec-schemas.ts index 104d67ac6..2c6698a88 100644 --- a/packages/nextlove/src/generators/extract-route-spec-schemas.ts +++ b/packages/nextlove/src/generators/extract-route-spec-schemas.ts @@ -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 = ( @@ -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", @@ -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 {}` } - }) - }, - }, ], }) }