From d08df28ff33a16c62d5943b8d5172d7f6cf48a13 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Thu, 22 Aug 2024 16:26:53 +0200 Subject: [PATCH 1/3] fix: limit files scanned by the module scanner --- examples/modules/.gitignore | 7 ++++++ examples/modules/README.md | 3 +++ examples/modules/nitro.config.ts | 4 ++++ examples/modules/package.json | 11 +++++++++ examples/modules/server/modules/log/index.ts | 14 +++++++++++ .../modules/log/runtime/middleware/log.ts | 3 +++ examples/modules/server/routes/index.ts | 3 +++ examples/modules/tsconfig.json | 4 ++++ pnpm-lock.yaml | 6 +++++ src/core/scan.ts | 24 +++++++++++++++---- 10 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 examples/modules/.gitignore create mode 100644 examples/modules/README.md create mode 100644 examples/modules/nitro.config.ts create mode 100644 examples/modules/package.json create mode 100644 examples/modules/server/modules/log/index.ts create mode 100644 examples/modules/server/modules/log/runtime/middleware/log.ts create mode 100644 examples/modules/server/routes/index.ts create mode 100644 examples/modules/tsconfig.json diff --git a/examples/modules/.gitignore b/examples/modules/.gitignore new file mode 100644 index 0000000000..ebe9be8721 --- /dev/null +++ b/examples/modules/.gitignore @@ -0,0 +1,7 @@ +node_modules +dist +.data +.nitro +.cache +.output +.env diff --git a/examples/modules/README.md b/examples/modules/README.md new file mode 100644 index 0000000000..f25a324a63 --- /dev/null +++ b/examples/modules/README.md @@ -0,0 +1,3 @@ +# Nitro starter + +Look at the [nitro quick start](https://nitro.unjs.io/guide#quick-start) to learn more how to get started. diff --git a/examples/modules/nitro.config.ts b/examples/modules/nitro.config.ts new file mode 100644 index 0000000000..234d9ef730 --- /dev/null +++ b/examples/modules/nitro.config.ts @@ -0,0 +1,4 @@ +// https://nitro.unjs.io/config +export default defineNitroConfig({ + srcDir: "server", +}); diff --git a/examples/modules/package.json b/examples/modules/package.json new file mode 100644 index 0000000000..0ef33628a5 --- /dev/null +++ b/examples/modules/package.json @@ -0,0 +1,11 @@ +{ + "type": "module", + "scripts": { + "build": "nitro build", + "dev": "nitro dev", + "preview": "node .output/server/index.mjs" + }, + "devDependencies": { + "nitropack": "latest" + } +} diff --git a/examples/modules/server/modules/log/index.ts b/examples/modules/server/modules/log/index.ts new file mode 100644 index 0000000000..a2dc5f858f --- /dev/null +++ b/examples/modules/server/modules/log/index.ts @@ -0,0 +1,14 @@ +import { fileURLToPath } from "node:url"; + +export default defineNitroModule({ + name: "log", + setup: (nitro) => { + const resolve = (path: string) => + fileURLToPath(new URL(path, import.meta.url)); + + nitro.options.handlers ||= []; + nitro.options.handlers.push({ + handler: resolve("runtime/middleware/log.ts"), + }); + }, +}); diff --git a/examples/modules/server/modules/log/runtime/middleware/log.ts b/examples/modules/server/modules/log/runtime/middleware/log.ts new file mode 100644 index 0000000000..be6bc2d479 --- /dev/null +++ b/examples/modules/server/modules/log/runtime/middleware/log.ts @@ -0,0 +1,3 @@ +export default defineEventHandler((event) => { + console.log(event.toString()); +}); diff --git a/examples/modules/server/routes/index.ts b/examples/modules/server/routes/index.ts new file mode 100644 index 0000000000..40d8050765 --- /dev/null +++ b/examples/modules/server/routes/index.ts @@ -0,0 +1,3 @@ +export default eventHandler((event) => { + return "Start by editing server/routes/index.ts."; +}); diff --git a/examples/modules/tsconfig.json b/examples/modules/tsconfig.json new file mode 100644 index 0000000000..d32e2ef7d0 --- /dev/null +++ b/examples/modules/tsconfig.json @@ -0,0 +1,4 @@ +// https://nitro.unjs.io/guide/typescript +{ + "extends": "./.nitro/types/tsconfig.json" +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5360622e2..20c3582350 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -382,6 +382,12 @@ importers: specifier: link:../.. version: link:../.. + examples/modules: + devDependencies: + nitropack: + specifier: link:../.. + version: link:../.. + examples/nano-jsx: devDependencies: nano-jsx: diff --git a/src/core/scan.ts b/src/core/scan.ts index dcae4024c5..b0114de8a1 100644 --- a/src/core/scan.ts +++ b/src/core/scan.ts @@ -140,13 +140,20 @@ export async function scanTasks(nitro: Nitro) { } export async function scanModules(nitro: Nitro) { - const files = await scanFiles(nitro, "modules"); + const files = await scanFiles(nitro, "modules", [ + "*/index.{js,mjs,cjs,ts,mts,cts}", + "*.{js,mjs,cjs,ts,mts,cts}", + ]); return files.map((f) => f.fullPath); } -async function scanFiles(nitro: Nitro, name: string): Promise { +async function scanFiles( + nitro: Nitro, + name: string, + patterns: string | string[] = [GLOB_SCAN_PATTERN] +): Promise { const files = await Promise.all( - nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name)) + nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name, patterns)) ).then((r) => r.flat()); return files; } @@ -154,9 +161,16 @@ async function scanFiles(nitro: Nitro, name: string): Promise { async function scanDir( nitro: Nitro, dir: string, - name: string + name: string, + patterns: string | string[] ): Promise { - const fileNames = await globby(join(name, GLOB_SCAN_PATTERN), { + const globbyPattern: string[] = []; + // Normalize patterns to an array + for (const pattern of Array.isArray(patterns) ? patterns : [patterns]) { + globbyPattern.push(join(name, pattern)); + } + + const fileNames = await globby(globbyPattern, { cwd: dir, dot: true, ignore: nitro.options.ignore, From 394b705dbe050a314fe0ea80f6385c69c0b1dca1 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Thu, 22 Aug 2024 21:33:18 +0200 Subject: [PATCH 2/3] chore: remove example --- examples/modules/.gitignore | 7 ------- examples/modules/README.md | 3 --- examples/modules/nitro.config.ts | 4 ---- examples/modules/package.json | 11 ----------- examples/modules/server/modules/log/index.ts | 14 -------------- .../server/modules/log/runtime/middleware/log.ts | 3 --- examples/modules/server/routes/index.ts | 3 --- examples/modules/tsconfig.json | 4 ---- 8 files changed, 49 deletions(-) delete mode 100644 examples/modules/.gitignore delete mode 100644 examples/modules/README.md delete mode 100644 examples/modules/nitro.config.ts delete mode 100644 examples/modules/package.json delete mode 100644 examples/modules/server/modules/log/index.ts delete mode 100644 examples/modules/server/modules/log/runtime/middleware/log.ts delete mode 100644 examples/modules/server/routes/index.ts delete mode 100644 examples/modules/tsconfig.json diff --git a/examples/modules/.gitignore b/examples/modules/.gitignore deleted file mode 100644 index ebe9be8721..0000000000 --- a/examples/modules/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -node_modules -dist -.data -.nitro -.cache -.output -.env diff --git a/examples/modules/README.md b/examples/modules/README.md deleted file mode 100644 index f25a324a63..0000000000 --- a/examples/modules/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Nitro starter - -Look at the [nitro quick start](https://nitro.unjs.io/guide#quick-start) to learn more how to get started. diff --git a/examples/modules/nitro.config.ts b/examples/modules/nitro.config.ts deleted file mode 100644 index 234d9ef730..0000000000 --- a/examples/modules/nitro.config.ts +++ /dev/null @@ -1,4 +0,0 @@ -// https://nitro.unjs.io/config -export default defineNitroConfig({ - srcDir: "server", -}); diff --git a/examples/modules/package.json b/examples/modules/package.json deleted file mode 100644 index 0ef33628a5..0000000000 --- a/examples/modules/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "type": "module", - "scripts": { - "build": "nitro build", - "dev": "nitro dev", - "preview": "node .output/server/index.mjs" - }, - "devDependencies": { - "nitropack": "latest" - } -} diff --git a/examples/modules/server/modules/log/index.ts b/examples/modules/server/modules/log/index.ts deleted file mode 100644 index a2dc5f858f..0000000000 --- a/examples/modules/server/modules/log/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { fileURLToPath } from "node:url"; - -export default defineNitroModule({ - name: "log", - setup: (nitro) => { - const resolve = (path: string) => - fileURLToPath(new URL(path, import.meta.url)); - - nitro.options.handlers ||= []; - nitro.options.handlers.push({ - handler: resolve("runtime/middleware/log.ts"), - }); - }, -}); diff --git a/examples/modules/server/modules/log/runtime/middleware/log.ts b/examples/modules/server/modules/log/runtime/middleware/log.ts deleted file mode 100644 index be6bc2d479..0000000000 --- a/examples/modules/server/modules/log/runtime/middleware/log.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default defineEventHandler((event) => { - console.log(event.toString()); -}); diff --git a/examples/modules/server/routes/index.ts b/examples/modules/server/routes/index.ts deleted file mode 100644 index 40d8050765..0000000000 --- a/examples/modules/server/routes/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default eventHandler((event) => { - return "Start by editing server/routes/index.ts."; -}); diff --git a/examples/modules/tsconfig.json b/examples/modules/tsconfig.json deleted file mode 100644 index d32e2ef7d0..0000000000 --- a/examples/modules/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -// https://nitro.unjs.io/guide/typescript -{ - "extends": "./.nitro/types/tsconfig.json" -} From 85630d4b385f3448c19d7aaaa356c877373f02b4 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Thu, 22 Aug 2024 21:35:11 +0200 Subject: [PATCH 3/3] chore: remove examples --- pnpm-lock.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 20c3582350..f5360622e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -382,12 +382,6 @@ importers: specifier: link:../.. version: link:../.. - examples/modules: - devDependencies: - nitropack: - specifier: link:../.. - version: link:../.. - examples/nano-jsx: devDependencies: nano-jsx: