From 0c0ab1566b0c0df1d12c75e563c5befcacab44e2 Mon Sep 17 00:00:00 2001
From: Jack Hsu <jack.hsu@gmail.com>
Date: Thu, 28 Mar 2024 16:14:34 -0400
Subject: [PATCH] fix(nuxt): use loadConfigFile from devkit rather than
 @nuxt/kit

---
 e2e/nuxt/src/nuxt.test.ts                     |  2 +-
 .../plugins/__snapshots__/plugin.spec.ts.snap |  8 ++---
 packages/nuxt/src/plugins/plugin.spec.ts      |  4 +--
 packages/nuxt/src/plugins/plugin.ts           | 33 ++++++++-----------
 4 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/e2e/nuxt/src/nuxt.test.ts b/e2e/nuxt/src/nuxt.test.ts
index 84aaae69af32d..8af5eb4ec47cc 100644
--- a/e2e/nuxt/src/nuxt.test.ts
+++ b/e2e/nuxt/src/nuxt.test.ts
@@ -17,7 +17,7 @@ describe('Nuxt Plugin', () => {
       unsetProjectNameAndRootFormat: false,
     });
     runCLI(
-      `generate @nx/nuxt:app ${app} --unitTestRunner=vitest --projectNameAndRootFormat=as-provided e2eTestRunner=cypress`
+      `generate @nx/nuxt:app ${app} --unitTestRunner=vitest --projectNameAndRootFormat=as-provided --e2eTestRunner=cypress`
     );
     runCLI(
       `generate @nx/nuxt:component --directory=${app}/src/components/one --name=one --nameAndDirectoryFormat=as-provided --unitTestRunner=vitest`
diff --git a/packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap b/packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
index b3ccfdc896c04..c1d56e7d44a02 100644
--- a/packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
+++ b/packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
@@ -25,7 +25,7 @@ exports[`@nx/nuxt/plugin not root project should create nodes 1`] = `
             "cwd": "my-app",
           },
           "outputs": [
-            "{workspaceRoot}/dist/my-app/",
+            "{workspaceRoot}/dist/my-app/.nuxt",
           ],
         },
         "acme-serve-static": {
@@ -56,7 +56,7 @@ exports[`@nx/nuxt/plugin not root project should create nodes 1`] = `
             "cwd": "my-app",
           },
           "outputs": [
-            "{workspaceRoot}/dist/my-app/",
+            "{workspaceRoot}/dist/my-app/.nuxt",
           ],
         },
         "my-serve": {
@@ -96,7 +96,7 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
             "cwd": ".",
           },
           "outputs": [
-            "dist/my-app/",
+            "dist/my-app/.nuxt",
           ],
         },
         "build-static": {
@@ -118,7 +118,7 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
             "cwd": ".",
           },
           "outputs": [
-            "dist/my-app/",
+            "dist/my-app/.nuxt",
           ],
         },
         "serve": {
diff --git a/packages/nuxt/src/plugins/plugin.spec.ts b/packages/nuxt/src/plugins/plugin.spec.ts
index 12ce5693df64e..df3faf1b0242a 100644
--- a/packages/nuxt/src/plugins/plugin.spec.ts
+++ b/packages/nuxt/src/plugins/plugin.spec.ts
@@ -2,8 +2,8 @@ import { CreateNodesContext } from '@nx/devkit';
 import { createNodes } from './plugin';
 import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
 
-jest.mock('@nuxt/kit', () => ({
-  loadNuxtConfig: jest.fn().mockImplementation(() => {
+jest.mock('@nx/devkit/src/utils/config-utils', () => ({
+  loadConfigFile: jest.fn().mockImplementation(() => {
     return Promise.resolve({
       buildDir: '../dist/my-app/.nuxt',
     });
diff --git a/packages/nuxt/src/plugins/plugin.ts b/packages/nuxt/src/plugins/plugin.ts
index cee7590da73c3..02b5761c9328d 100644
--- a/packages/nuxt/src/plugins/plugin.ts
+++ b/packages/nuxt/src/plugins/plugin.ts
@@ -13,9 +13,9 @@ import { basename, dirname, isAbsolute, join, relative } from 'path';
 import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
 import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
 import { existsSync, readdirSync } from 'fs';
-import { loadNuxtKitDynamicImport } from '../utils/executor-utils';
 import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
 import { getLockFileName } from '@nx/js';
+import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';
 
 const cachePath = join(projectGraphCacheDirectory, 'nuxt.hash');
 const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
@@ -208,15 +208,16 @@ async function getInfoFromNuxtConfig(
 ): Promise<{
   buildDir: string;
 }> {
-  const { loadNuxtConfig } = await loadNuxtKitDynamicImport();
-
-  const config = await loadNuxtConfig({
-    cwd: joinPathFragments(context.workspaceRoot, projectRoot),
-    configFile: basename(configFilePath),
-  });
-
+  // TODO(Colum): Once plugins are isolated we can go back to @nuxt/kit since each plugin will be run in its own worker.
+  const config = await loadConfigFile(
+    join(context.workspaceRoot, configFilePath)
+  );
   return {
-    buildDir: config?.buildDir,
+    buildDir:
+      config?.buildDir ??
+      // Match .nuxt default build dir from '@nuxt/schema'
+      // See: https://github.com/nuxt/nuxt/blob/871404ae5673425aeedde82f123ea58aa7c6facf/packages/schema/src/config/common.ts#L117-L119
+      '.nuxt',
   };
 }
 
@@ -226,16 +227,10 @@ function getOutputs(
 ): {
   buildOutputs: string[];
 } {
-  let nuxtBuildDir = nuxtConfig?.buildDir;
-  if (nuxtConfig?.buildDir && basename(nuxtConfig?.buildDir) === '.nuxt') {
-    // if buildDir exists, it will be `something/something/.nuxt`
-    // we want the "general" outputPath to be `something/something`
-    nuxtBuildDir = nuxtConfig.buildDir.replace(
-      basename(nuxtConfig.buildDir),
-      ''
-    );
-  }
-  const buildOutputPath = normalizeOutputPath(nuxtBuildDir, projectRoot);
+  const buildOutputPath = normalizeOutputPath(
+    nuxtConfig?.buildDir,
+    projectRoot
+  );
 
   return {
     buildOutputs: [buildOutputPath],