From 9ea0fc17f2597658e907bdb3676852e1fa5a5404 Mon Sep 17 00:00:00 2001 From: Inesh Bose Date: Sun, 24 Mar 2024 14:17:02 +0000 Subject: [PATCH 1/9] feat(kit): findPath considers templates too --- packages/kit/src/resolve.test.ts | 35 ++++++++++++++++++++++++++++++++ packages/kit/src/resolve.ts | 8 ++++++++ 2 files changed, 43 insertions(+) create mode 100644 packages/kit/src/resolve.test.ts diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts new file mode 100644 index 000000000000..45e02b256ed0 --- /dev/null +++ b/packages/kit/src/resolve.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest' +import { resolve } from 'pathe' +import { loadNuxt } from './loader/nuxt' +import { + findPath, + resolvePath +} from './resolve' +import { defineNuxtModule } from './module/define' +import { addTemplate } from './template' + +describe('resolvePath', () => { + it('should resolve paths correctly', async () => { + const nuxt = await loadNuxt({}) + expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) + }) +}) + +describe('findPath', () => { + it('should find paths correctly', async () => { + const nuxt = await loadNuxt({ + overrides: { + modules: [ + defineNuxtModule(() => { + addTemplate({ + filename: 'my-template.mjs', + getContents: () => 'export const myUtil = () => \'hello\'' + }) + }) + ] + } + }) + + expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'))).not.toBeNull() + }) +}) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index a768ba650983..43ea47367678 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -17,6 +17,9 @@ export interface ResolvePathOptions { /** The file extensions to try. Default is Nuxt configured extensions. */ extensions?: string[] + + /** Determine if the file is in Nuxt Templates. Default is false. */ + checkTemplates?: boolean; } /** @@ -93,6 +96,11 @@ export async function findPath (paths: string | string[], opts?: ResolvePathOpti return rPath } } + + const tPath = opts?.checkTemplates && tryUseNuxt()?.options.build.templates.find(template => template.dst === rPath) + if (tPath) { + return tPath.dst! + } } return null } From 66abdf1412ff81cf1158c6a16c9ea45b87e32556 Mon Sep 17 00:00:00 2001 From: Inesh Bose Date: Sun, 24 Mar 2024 14:23:48 +0000 Subject: [PATCH 2/9] fix: tests --- packages/kit/src/resolve.test.ts | 29 ++++++++++++++--------------- packages/kit/src/resolve.ts | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 45e02b256ed0..2ec3099dfc08 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -9,27 +9,26 @@ import { defineNuxtModule } from './module/define' import { addTemplate } from './template' describe('resolvePath', () => { + const nuxt = await loadNuxt({ + overrides: { + modules: [ + defineNuxtModule(() => { + addTemplate({ + filename: 'my-template.mjs', + getContents: () => 'export const myUtil = () => \'hello\'' + }) + }) + ] + } + }) + it('should resolve paths correctly', async () => { - const nuxt = await loadNuxt({}) - expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) + expect(await resolvePath('.nuxt/app.config', { extensions: ['ts', 'mjs'] })).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) }) }) describe('findPath', () => { it('should find paths correctly', async () => { - const nuxt = await loadNuxt({ - overrides: { - modules: [ - defineNuxtModule(() => { - addTemplate({ - filename: 'my-template.mjs', - getContents: () => 'export const myUtil = () => \'hello\'' - }) - }) - ] - } - }) - expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'))).not.toBeNull() }) }) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 43ea47367678..cbe3e1caeb71 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -19,7 +19,7 @@ export interface ResolvePathOptions { extensions?: string[] /** Determine if the file is in Nuxt Templates. Default is false. */ - checkTemplates?: boolean; + checkTemplates?: boolean } /** From 5f3b3447d21c639dda8aa9a27ead93348e32b029 Mon Sep 17 00:00:00 2001 From: Inesh Bose Date: Sun, 24 Mar 2024 14:29:08 +0000 Subject: [PATCH 3/9] fix: tests --- packages/kit/src/resolve.test.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 2ec3099dfc08..c0f987c8efbe 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -8,27 +8,27 @@ import { import { defineNuxtModule } from './module/define' import { addTemplate } from './template' -describe('resolvePath', () => { - const nuxt = await loadNuxt({ - overrides: { - modules: [ - defineNuxtModule(() => { - addTemplate({ - filename: 'my-template.mjs', - getContents: () => 'export const myUtil = () => \'hello\'' - }) +const nuxt = await loadNuxt({ + overrides: { + modules: [ + defineNuxtModule(() => { + addTemplate({ + filename: 'my-template.mjs', + getContents: () => 'export const myUtil = () => \'hello\'' }) - ] - } - }) + }) + ] + } +}) +describe('resolvePath', () => { it('should resolve paths correctly', async () => { - expect(await resolvePath('.nuxt/app.config', { extensions: ['ts', 'mjs'] })).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) + expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config')) }) }) describe('findPath', () => { it('should find paths correctly', async () => { - expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'))).not.toBeNull() + expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'), { checkTemplates: true })).not.toBeNull() }) }) From d60588842afec872db655c3f54b86979064a8d42 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:28:58 +0000 Subject: [PATCH 4/9] [autofix.ci] apply automated fixes --- packages/kit/src/resolve.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index c0f987c8efbe..1d6810095969 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -3,7 +3,7 @@ import { resolve } from 'pathe' import { loadNuxt } from './loader/nuxt' import { findPath, - resolvePath + resolvePath, } from './resolve' import { defineNuxtModule } from './module/define' import { addTemplate } from './template' @@ -14,11 +14,11 @@ const nuxt = await loadNuxt({ defineNuxtModule(() => { addTemplate({ filename: 'my-template.mjs', - getContents: () => 'export const myUtil = () => \'hello\'' + getContents: () => 'export const myUtil = () => \'hello\'', }) - }) - ] - } + }), + ], + }, }) describe('resolvePath', () => { From 1d9a4b6766e1780f69e7e63e40a889350d6da163 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 18 Apr 2024 22:03:38 +0300 Subject: [PATCH 5/9] fix(kit): support `checkTemplates` in `resolvePath` --- packages/kit/src/resolve.test.ts | 2 +- packages/kit/src/resolve.ts | 44 +++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 1d6810095969..91f1329cbb25 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -23,7 +23,7 @@ const nuxt = await loadNuxt({ describe('resolvePath', () => { it('should resolve paths correctly', async () => { - expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config')) + expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) }) }) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 4b353bf13c34..8206efae4f37 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -33,8 +33,13 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): path = normalize(path) // Fast return if the path exists - if (isAbsolute(path) && existsSync(path) && !(await isDirectory(path))) { - return path + if (isAbsolute(path)) { + if (opts?.checkTemplates && existsInVFS(path)) { + return path + } + if (existsSync(path) && !(await isDirectory(path))) { + return path + } } // Use current nuxt options @@ -52,6 +57,10 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): } // Check if resolvedPath is a file + if (opts?.checkTemplates && existsInVFS(path, nuxt)) { + return path + } + let _isDir = false if (existsSync(path)) { _isDir = await isDirectory(path) @@ -64,11 +73,17 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): for (const ext of extensions) { // path.[ext] const pathWithExt = path + ext + if (opts?.checkTemplates && existsInVFS(pathWithExt, nuxt)) { + return pathWithExt + } if (existsSync(pathWithExt)) { return pathWithExt } // path/index.[ext] const pathWithIndex = join(path, 'index' + ext) + if (opts?.checkTemplates && existsInVFS(pathWithIndex, nuxt)) { + return pathWithIndex + } if (_isDir && existsSync(pathWithIndex)) { return pathWithIndex } @@ -88,19 +103,23 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): * Try to resolve first existing file in paths */ export async function findPath (paths: string | string[], opts?: ResolvePathOptions, pathType: 'file' | 'dir' = 'file'): Promise { + const nuxt = opts?.checkTemplates ? tryUseNuxt() : undefined + for (const path of toArray(paths)) { const rPath = await resolvePath(path, opts) + + // Check VFS + if (opts?.checkTemplates && existsInVFS(rPath, nuxt)) { + return rPath + } + + // Check file system if (await existsSensitive(rPath)) { const _isDir = await isDirectory(rPath) if (!pathType || (pathType === 'file' && !_isDir) || (pathType === 'dir' && _isDir)) { return rPath } } - - const tPath = opts?.checkTemplates && tryUseNuxt()?.options.build.templates.find(template => template.dst === rPath) - if (tPath) { - return tPath.dst! - } } return null } @@ -168,6 +187,17 @@ async function isDirectory (path: string) { return (await fsp.lstat(path)).isDirectory() } +function existsInVFS (path: string, nuxt = tryUseNuxt()) { + if (!nuxt) { return false } + + if (path in nuxt.vfs) { + return true + } + + const templates = nuxt.apps.default?.templates ?? nuxt.options.build.templates + return templates.some(template => template.dst === path) +} + export async function resolveFiles (path: string, pattern: string | string[], opts: { followSymbolicLinks?: boolean } = {}) { const files = await globby(pattern, { cwd: path, followSymbolicLinks: opts.followSymbolicLinks ?? true }) return files.map(p => resolve(path, p)).filter(p => !isIgnored(p)).sort() From 6099333f912448a263202dccb80556179589413b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 18 Apr 2024 22:05:37 +0300 Subject: [PATCH 6/9] refactor: rename option to `virtual` --- packages/kit/src/resolve.test.ts | 2 +- packages/kit/src/resolve.ts | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 91f1329cbb25..d81ae3a21ed9 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -29,6 +29,6 @@ describe('resolvePath', () => { describe('findPath', () => { it('should find paths correctly', async () => { - expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'), { checkTemplates: true })).not.toBeNull() + expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'), { virtual: true })).not.toBeNull() }) }) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 8206efae4f37..8bdf0693c3d4 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -18,8 +18,11 @@ export interface ResolvePathOptions { /** The file extensions to try. Default is Nuxt configured extensions. */ extensions?: string[] - /** Determine if the file is in Nuxt Templates. Default is false. */ - checkTemplates?: boolean + /** + * Whether to resolve files that exist in the Nuxt VFS (for example, as a Nuxt template). + * @default false + */ + virtual?: boolean } /** @@ -34,7 +37,7 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): // Fast return if the path exists if (isAbsolute(path)) { - if (opts?.checkTemplates && existsInVFS(path)) { + if (opts?.virtual && existsInVFS(path)) { return path } if (existsSync(path) && !(await isDirectory(path))) { @@ -57,7 +60,7 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): } // Check if resolvedPath is a file - if (opts?.checkTemplates && existsInVFS(path, nuxt)) { + if (opts?.virtual && existsInVFS(path, nuxt)) { return path } @@ -73,7 +76,7 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): for (const ext of extensions) { // path.[ext] const pathWithExt = path + ext - if (opts?.checkTemplates && existsInVFS(pathWithExt, nuxt)) { + if (opts?.virtual && existsInVFS(pathWithExt, nuxt)) { return pathWithExt } if (existsSync(pathWithExt)) { @@ -81,7 +84,7 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): } // path/index.[ext] const pathWithIndex = join(path, 'index' + ext) - if (opts?.checkTemplates && existsInVFS(pathWithIndex, nuxt)) { + if (opts?.virtual && existsInVFS(pathWithIndex, nuxt)) { return pathWithIndex } if (_isDir && existsSync(pathWithIndex)) { @@ -103,13 +106,13 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}): * Try to resolve first existing file in paths */ export async function findPath (paths: string | string[], opts?: ResolvePathOptions, pathType: 'file' | 'dir' = 'file'): Promise { - const nuxt = opts?.checkTemplates ? tryUseNuxt() : undefined + const nuxt = opts?.virtual ? tryUseNuxt() : undefined for (const path of toArray(paths)) { const rPath = await resolvePath(path, opts) // Check VFS - if (opts?.checkTemplates && existsInVFS(rPath, nuxt)) { + if (opts?.virtual && existsInVFS(rPath, nuxt)) { return rPath } From 19e391496bb33524dd4f07a88f3a240b984c6a07 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 18 Apr 2024 22:07:50 +0300 Subject: [PATCH 7/9] chore: lint --- packages/kit/src/resolve.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index d81ae3a21ed9..2242d499ecb5 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -1,10 +1,7 @@ import { describe, expect, it } from 'vitest' import { resolve } from 'pathe' import { loadNuxt } from './loader/nuxt' -import { - findPath, - resolvePath, -} from './resolve' +import { findPath, resolvePath } from './resolve' import { defineNuxtModule } from './module/define' import { addTemplate } from './template' From 65159dded098dcf92b12d5bd5904478e8619c7d6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 18 Apr 2024 22:09:15 +0300 Subject: [PATCH 8/9] test: revert change to resolvePath test --- packages/kit/src/resolve.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 2242d499ecb5..6ba7674c7d6e 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -20,7 +20,7 @@ const nuxt = await loadNuxt({ describe('resolvePath', () => { it('should resolve paths correctly', async () => { - expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config.mjs')) + expect(await resolvePath('.nuxt/app.config')).toBe(resolve(nuxt.options.buildDir, 'app.config')) }) }) From 0c771ad4cb136b60a9c08b60fcebc0f9924c921c Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 18 Apr 2024 22:10:23 +0300 Subject: [PATCH 9/9] test: update test --- packages/kit/src/resolve.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/resolve.test.ts b/packages/kit/src/resolve.test.ts index 6ba7674c7d6e..bcdb95486e8b 100644 --- a/packages/kit/src/resolve.test.ts +++ b/packages/kit/src/resolve.test.ts @@ -26,6 +26,6 @@ describe('resolvePath', () => { describe('findPath', () => { it('should find paths correctly', async () => { - expect(await findPath(resolve(nuxt.options.buildDir, 'my-template.mjs'), { virtual: true })).not.toBeNull() + expect(await findPath(resolve(nuxt.options.buildDir, 'my-template'), { virtual: true })).toBe(resolve(nuxt.options.buildDir, 'my-template.mjs')) }) })