From 5fd248c1d08ffef2feda1f43d9b96b0efd34895a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Sep 2022 12:18:14 +0100 Subject: [PATCH 1/4] feat(nuxt): add `workspaceDir` option and add it to `modulesDir` --- packages/nuxt/src/core/nitro.ts | 1 + packages/nuxt/src/core/nuxt.ts | 3 +++ packages/schema/src/config/_common.ts | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index be20faa9253..8b896e1d526 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -18,6 +18,7 @@ export async function initNitro (nuxt: Nuxt) { const _nitroConfig = ((nuxt.options as any).nitro || {}) as NitroConfig const nitroConfig: NitroConfig = defu(_nitroConfig, { rootDir: nuxt.options.rootDir, + workspaceDir: nuxt.options.workspaceDir, srcDir: join(nuxt.options.srcDir, 'server'), dev: nuxt.options.dev, preset: nuxt.options.dev ? 'nitro-dev' : undefined, diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 99e840de305..632cc5dbfd1 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -7,6 +7,7 @@ import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, import escapeRE from 'escape-string-regexp' import fse from 'fs-extra' import { withoutLeadingSlash } from 'ufo' +import { findWorkspaceDir } from 'pkg-types' import pagesModule from '../pages/module' import metaModule from '../head/module' import componentsModule from '../components/module' @@ -204,6 +205,8 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { .map(i => new RegExp(`(^|\\/)${escapeRE(i.cwd!.split('node_modules/').pop()!)}(\\/|$)(?!node_modules\\/)`)) } }]) + options.workspaceDir = options.workspaceDir || await findWorkspaceDir(options.rootDir) + options.modulesDir.push(resolve(options.workspaceDir, 'node_modules')) options.modulesDir.push(resolve(pkgDir, 'node_modules')) options.build.transpile.push('@nuxt/ui-templates') options.alias['vue-demi'] = resolve(options.appDir, 'compat/vue-demi') diff --git a/packages/schema/src/config/_common.ts b/packages/schema/src/config/_common.ts index 41122a3603f..2e9797b770d 100644 --- a/packages/schema/src/config/_common.ts +++ b/packages/schema/src/config/_common.ts @@ -37,7 +37,7 @@ export default defineUntypedSchema({ theme: null, /** - * Define the workspace directory of your application. + * Define the root directory of your application. * * This property can be overwritten (for example, running `nuxt ./my-app/` * will set the `rootDir` to the absolute path of `./my-app/` from the @@ -51,6 +51,19 @@ export default defineUntypedSchema({ $resolve: val => typeof val === 'string' ? resolve(val) : process.cwd() }, + /** + * Define the workspace directory of your application. + * + * Often this is used when in a monorepo setup. Nuxt will attempt to detect + * your workspace directory automatically, but you can override it here. + * + * It is normally not needed to configure this option. + * @version 3 + */ + workspaceDir: { + $resolve: (val, get) => val ? resolve(get('rootDir'), val) : '' + }, + /** * Define the source directory of your Nuxt application. * From 2837240c019091e81809d165a347e8dc9ca70bfe Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Sep 2022 12:27:23 +0100 Subject: [PATCH 2/4] fix: add pkg-types dependency --- packages/nuxt/package.json | 1 + yarn.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 2f8eecc6f4e..8f063b46196 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -58,6 +58,7 @@ "ohmyfetch": "^0.4.18", "pathe": "^0.3.7", "perfect-debounce": "^0.1.3", + "pkg-types": "^0.3.5", "scule": "^0.3.2", "strip-literal": "^0.4.0", "ufo": "^0.8.5", diff --git a/yarn.lock b/yarn.lock index d4614001303..43991804ad3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10259,6 +10259,7 @@ __metadata: ohmyfetch: ^0.4.18 pathe: ^0.3.7 perfect-debounce: ^0.1.3 + pkg-types: ^0.3.5 scule: ^0.3.2 strip-literal: ^0.4.0 ufo: ^0.8.5 From dabe04c7486e477fe0aeb87ce2d54b681a00c2e0 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Sep 2022 20:20:56 +0100 Subject: [PATCH 3/4] refactor: move workspace dir resolution into schema --- packages/nuxt/package.json | 1 - packages/nuxt/src/core/nuxt.ts | 2 -- packages/schema/build.config.ts | 1 - packages/schema/package.json | 1 + packages/schema/src/config/_common.ts | 3 ++- yarn.lock | 2 +- 6 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 37120ce13f3..4160f9bf870 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -58,7 +58,6 @@ "ohmyfetch": "^0.4.18", "pathe": "^0.3.7", "perfect-debounce": "^0.1.3", - "pkg-types": "^0.3.5", "scule": "^0.3.2", "strip-literal": "^0.4.0", "ufo": "^0.8.5", diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 27ee7413dbc..de2f18acc49 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -7,7 +7,6 @@ import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, import escapeRE from 'escape-string-regexp' import fse from 'fs-extra' import { withoutLeadingSlash } from 'ufo' -import { findWorkspaceDir } from 'pkg-types' import pagesModule from '../pages/module' import metaModule from '../head/module' import componentsModule from '../components/module' @@ -212,7 +211,6 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { .map(i => new RegExp(`(^|\\/)${escapeRE(i.cwd!.split('node_modules/').pop()!)}(\\/|$)(?!node_modules\\/)`)) } }]) - options.workspaceDir = options.workspaceDir || await findWorkspaceDir(options.rootDir) options.modulesDir.push(resolve(options.workspaceDir, 'node_modules')) options.modulesDir.push(resolve(pkgDir, 'node_modules')) options.build.transpile.push('@nuxt/ui-templates') diff --git a/packages/schema/build.config.ts b/packages/schema/build.config.ts index 539d2739bfc..80bbf5fb7b2 100644 --- a/packages/schema/build.config.ts +++ b/packages/schema/build.config.ts @@ -26,7 +26,6 @@ export default defineBuildConfig({ 'hookable', 'nitropack', 'webpack', - 'pkg-types', 'webpack-bundle-analyzer', 'rollup-plugin-visualizer', 'vite', diff --git a/packages/schema/package.json b/packages/schema/package.json index 8478ab70e61..ab89f40ae6d 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -26,6 +26,7 @@ "defu": "^6.1.0", "jiti": "^1.15.0", "pathe": "^0.3.7", + "pkg-types": "^0.3.5", "postcss-import-resolver": "^2.0.0", "scule": "^0.3.2", "std-env": "^3.2.1", diff --git a/packages/schema/src/config/_common.ts b/packages/schema/src/config/_common.ts index e2a7fc40bd2..5ed8a6d3122 100644 --- a/packages/schema/src/config/_common.ts +++ b/packages/schema/src/config/_common.ts @@ -4,6 +4,7 @@ import createRequire from 'create-require' import { pascalCase } from 'scule' import jiti from 'jiti' import defu from 'defu' +import { findWorkspaceDir } from 'pkg-types' import { RuntimeConfig } from '../types/config' @@ -61,7 +62,7 @@ export default defineUntypedSchema({ * @version 3 */ workspaceDir: { - $resolve: (val, get) => val ? resolve(get('rootDir'), val) : '' + $resolve: async (val, get) => val ? resolve(await get('rootDir'), val) : await findWorkspaceDir(await get('rootDir')) }, /** diff --git a/yarn.lock b/yarn.lock index 30d268d625c..2f1c981f591 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1819,6 +1819,7 @@ __metadata: defu: ^6.1.0 jiti: ^1.15.0 pathe: ^0.3.7 + pkg-types: ^0.3.5 postcss-import-resolver: ^2.0.0 scule: ^0.3.2 std-env: ^3.2.1 @@ -10259,7 +10260,6 @@ __metadata: ohmyfetch: ^0.4.18 pathe: ^0.3.7 perfect-debounce: ^0.1.3 - pkg-types: ^0.3.5 scule: ^0.3.2 strip-literal: ^0.4.0 ufo: ^0.8.5 From e0002cc453aaa1e44b9f65f76e3e050f588d46ba Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Sep 2022 20:31:57 +0100 Subject: [PATCH 4/4] fix: default to `rootDir` if `workspaceDir` can't be resolved --- packages/schema/src/config/_common.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/schema/src/config/_common.ts b/packages/schema/src/config/_common.ts index 5ed8a6d3122..77e8b706454 100644 --- a/packages/schema/src/config/_common.ts +++ b/packages/schema/src/config/_common.ts @@ -62,7 +62,7 @@ export default defineUntypedSchema({ * @version 3 */ workspaceDir: { - $resolve: async (val, get) => val ? resolve(await get('rootDir'), val) : await findWorkspaceDir(await get('rootDir')) + $resolve: async (val, get) => val ? resolve(await get('rootDir'), val) : await findWorkspaceDir(await get('rootDir')).catch(() => get('rootDir')) }, /**