Skip to content

Commit

Permalink
fix(nuxt): tsconfig types and output dir (#21934)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored Feb 23, 2024
1 parent 26ce6f6 commit 776367e
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 83 deletions.
4 changes: 2 additions & 2 deletions e2e/nuxt/src/nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('Nuxt Plugin', () => {
expect(result).toContain(
`Successfully ran target build for project ${app}`
);
checkFilesExist(`dist/${app}/.nuxt/nuxt.d.ts`);
checkFilesExist(`dist/${app}/.output/nitro.json`);
checkFilesExist(`${app}/.nuxt/nuxt.d.ts`);
checkFilesExist(`${app}/.output/nitro.json`);
});

it('should test application', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`app generated files content - as-provided general application should ad
exports[`app generated files content - as-provided general application should configure eslint correctly 1`] = `
"{
"extends": ["@nuxt/eslint-config", "../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"ignorePatterns": ["!**/*", ".nuxt/**", ".output/**", "node_modules"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx", "*.vue"],
Expand All @@ -34,7 +34,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '../',
srcDir: 'src',
buildDir: '../dist/my-app/.nuxt',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -53,11 +52,6 @@ export default defineNuxtConfig({
vite: {
plugins: [nxViteTsPaths()],
},
nitro: {
output: {
dir: '../dist/my-app/.output',
},
},
});
"
`;
Expand All @@ -77,7 +71,7 @@ exports[`app generated files content - as-provided general application should co
"{
"compilerOptions": {},
"files": [],
"include": [],
"include": [".nuxt/nuxt.d.ts"],
"references": [
{
"path": "./tsconfig.app.json"
Expand Down Expand Up @@ -161,7 +155,7 @@ exports[`app generated files content - as-provided general application should co
"{
"compilerOptions": {},
"files": [],
"include": [],
"include": [".nuxt/nuxt.d.ts"],
"references": [
{
"path": "./tsconfig.app.json"
Expand Down Expand Up @@ -222,7 +216,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '../',
srcDir: 'src',
buildDir: '../dist/myapp1/.nuxt',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -243,11 +236,6 @@ export default defineNuxtConfig({
vite: {
plugins: [nxViteTsPaths()],
},
nitro: {
output: {
dir: '../dist/myapp1/.output',
},
},
});
"
`;
Expand All @@ -260,7 +248,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '../',
srcDir: 'src',
buildDir: '../dist/myapp3/.nuxt',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -281,11 +268,6 @@ export default defineNuxtConfig({
vite: {
plugins: [nxViteTsPaths()],
},
nitro: {
output: {
dir: '../dist/myapp3/.output',
},
},
});
"
`;
Expand All @@ -298,7 +280,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '../',
srcDir: 'src',
buildDir: '../dist/myapp2/.nuxt',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -319,11 +300,6 @@ export default defineNuxtConfig({
vite: {
plugins: [nxViteTsPaths()],
},
nitro: {
output: {
dir: '../dist/myapp2/.output',
},
},
});
"
`;
Expand All @@ -336,7 +312,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '../',
srcDir: 'src',
buildDir: '../dist/myapp4/.nuxt',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -355,11 +330,6 @@ export default defineNuxtConfig({
vite: {
plugins: [nxViteTsPaths()],
},
nitro: {
output: {
dir: '../dist/myapp4/.output',
},
},
});
"
`;
4 changes: 0 additions & 4 deletions packages/nuxt/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
tmpl: '',
style: options.style,
projectRoot: options.appProjectRoot,
buildDirectory: joinPathFragments(`dist/${options.appProjectRoot}/.nuxt`),
nitroOutputDir: joinPathFragments(
`dist/${options.appProjectRoot}/.output`
),
hasVitest: options.unitTestRunner === 'vitest',
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { defineNuxtConfig } from 'nuxt/config';
export default defineNuxtConfig({
workspaceDir: '<%= offsetFromRoot %>',
srcDir: 'src',
buildDir: '<%= offsetFromRoot %><%= buildDirectory %>',
devtools: { enabled: true },
devServer: {
host: 'localhost',
Expand All @@ -28,9 +27,4 @@ export default defineNuxtConfig({
nxViteTsPaths()
],
},
nitro: {
output: {
dir: '<%= offsetFromRoot %><%= nitroOutputDir %>',
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import addIncludeToTsConfig from './add-include-tsconfig';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree, addProjectConfiguration, writeJson } from '@nx/devkit';

jest.mock('@nuxt/kit', () => ({
loadNuxtConfig: jest.fn().mockImplementation(() => {
return Promise.resolve({
buildDir: '../dist/my-nuxt-app/.nuxt',
});
}),
}));

jest.mock('../../utils/executor-utils', () => ({
loadNuxtKitDynamicImport: jest.fn().mockResolvedValue({
loadNuxtConfig: jest.fn().mockResolvedValue({
buildDir: '../dist/my-nuxt-app/.nuxt',
}),
}),
}));

describe('addIncludeToTsConfig', () => {
let tree: Tree;

beforeAll(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
addProjectConfiguration(tree, 'my-nuxt-app', {
root: `my-nuxt-app`,
sourceRoot: `my-nuxt-app/src`,
targets: {
test: {
executor: '@nx/vite:test',
options: {},
},
},
});

tree.write(
`my-nuxt-app/nuxt.config.ts`,
`
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import { defineNuxtConfig } from 'nuxt/config';
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
workspaceDir: '../../',
srcDir: 'src',
buildDir: '../dist/my-nuxt-app/.nuxt',
css: ['~/assets/css/styles.css'],
vite: {
plugins: [nxViteTsPaths()],
},
});
`
);

writeJson(tree, 'my-nuxt-app/tsconfig.json', {
compilerOptions: {},
files: [],
include: [],
references: [
{
path: './tsconfig.app.json',
},
{
path: './tsconfig.spec.json',
},
],
extends: '../tsconfig.base.json',
});
});

it('should add include to tsconfig', async () => {
await addIncludeToTsConfig(tree);
const tsConfig = tree.read('my-nuxt-app/tsconfig.json', 'utf-8');
const tsconfigJson = JSON.parse(tsConfig);
expect(tsconfigJson.include).toMatchObject([
'../dist/my-nuxt-app/.nuxt/nuxt.d.ts',
]);
});
});
75 changes: 75 additions & 0 deletions packages/nuxt/src/migrations/update-18-1-0/add-include-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
Tree,
formatFiles,
getProjects,
joinPathFragments,
updateJson,
workspaceRoot,
} from '@nx/devkit';
import { loadNuxtKitDynamicImport } from '../../utils/executor-utils';
import { basename } from 'path';

export default async function (tree: Tree) {
const projects = getProjects(tree);

for (const project of projects.values()) {
const nuxtConfigPath = findNuxtConfig(tree, project.root);

if (!nuxtConfigPath) {
continue;
}

const nuxtConfig = await getInfoFromNuxtConfig(
nuxtConfigPath,
project.root
);

const buildDir = nuxtConfig.buildDir ?? '.nuxt';

const tsConfigPath = joinPathFragments(project.root, 'tsconfig.json');

if (tree.exists(tsConfigPath)) {
updateJson(tree, tsConfigPath, (json) => {
if (!json.include) {
json.include = [];
}

if (!json.include.includes(buildDir + '/nuxt.d.ts')) {
json.include.push(buildDir + '/nuxt.d.ts');
}

return json;
});
}
}

await formatFiles(tree);
}

function findNuxtConfig(tree: Tree, projectRoot: string): string | undefined {
const allowsExt = ['js', 'mjs', 'ts', 'cjs', 'mts', 'cts'];

for (const ext of allowsExt) {
if (tree.exists(joinPathFragments(projectRoot, `nuxt.config.${ext}`))) {
return joinPathFragments(projectRoot, `nuxt.config.${ext}`);
}
}
}

async function getInfoFromNuxtConfig(
configFilePath: string,
projectRoot: string
): Promise<{
buildDir: string;
}> {
const { loadNuxtConfig } = await loadNuxtKitDynamicImport();

const config = await loadNuxtConfig({
cwd: joinPathFragments(workspaceRoot, projectRoot),
configFile: basename(configFilePath),
});

return {
buildDir: config?.buildDir,
};
}
20 changes: 7 additions & 13 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ async function buildNuxtTargets(
buildDir: string;
} = await getInfoFromNuxtConfig(configFilePath, context, projectRoot);

const { buildOutputs } = getOutputs(
nuxtConfig,

projectRoot
);
const { buildOutputs } = getOutputs(nuxtConfig, projectRoot);

const namedInputs = getNamedInputs(projectRoot, context);

Expand Down Expand Up @@ -179,16 +175,14 @@ function getOutputs(
} {
let nuxtBuildDir = nuxtConfig?.buildDir;
if (nuxtConfig?.buildDir && basename(nuxtConfig?.buildDir) === '.nuxt') {
// buildDir will most probably be `../dist/my-app/.nuxt`
// we want the "general" outputPath to be `../dist/my-app`
// 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) ??
'{workspaceRoot}/dist/{projectRoot}';
const buildOutputPath = normalizeOutputPath(nuxtBuildDir, projectRoot);

return {
buildOutputs: [buildOutputPath],
Expand All @@ -198,12 +192,12 @@ function getOutputs(
function normalizeOutputPath(
outputPath: string | undefined,
projectRoot: string
): string | undefined {
): string {
if (!outputPath) {
if (projectRoot === '.') {
return `{projectRoot}/dist`;
return `{projectRoot}`;
} else {
return `{workspaceRoot}/dist/{projectRoot}`;
return `{workspaceRoot}/{projectRoot}`;
}
} else {
if (isAbsolute(outputPath)) {
Expand Down
Loading

0 comments on commit 776367e

Please sign in to comment.