Skip to content

Commit

Permalink
fix(nuxt): use loadConfigFile from devkit rather than @nuxt/kit (#22571)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Mar 28, 2024
1 parent 557f873 commit ef81455
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion e2e/nuxt/src/nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 4 additions & 4 deletions packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand All @@ -118,7 +118,7 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
"cwd": ".",
},
"outputs": [
"dist/my-app/",
"dist/my-app/.nuxt",
],
},
"serve": {
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down
33 changes: 14 additions & 19 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() : {};
Expand Down Expand Up @@ -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',
};
}

Expand All @@ -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],
Expand Down

0 comments on commit ef81455

Please sign in to comment.