-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nuxt): tsconfig types and output dir (#21934)
- Loading branch information
Showing
11 changed files
with
196 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/nuxt/src/migrations/update-18-1-0/add-include-tsconfig.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
packages/nuxt/src/migrations/update-18-1-0/add-include-tsconfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.