refactor(cli,schema): add bundler module resolution flag (#22142)

This commit is contained in:
Daniel Roe 2023-07-14 14:46:40 +01:00 committed by GitHub
parent 21ea0faf8a
commit a07cfb8663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 15 deletions

View File

@ -14,9 +14,9 @@ export const writeTypes = async (nuxt: Nuxt) => {
jsx: 'preserve',
target: 'ESNext',
module: 'ESNext',
moduleResolution: 'Node',
moduleResolution: nuxt.options.experimental.typescriptBundlerResolution ? 'Bundler' : 'Node',
skipLibCheck: true,
strict: nuxt.options.typescript?.strict ?? false,
strict: nuxt.options.typescript?.strict ?? true,
allowJs: true,
// TODO: remove by default in 3.7
baseUrl: nuxt.options.srcDir,

View File

@ -37,6 +37,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
console.warn(`[nuxt] Could not load custom \`spaLoadingTemplate\` path as it does not exist: \`${spaLoadingTemplate}\`.`)
}
// @ts-expect-error `typescriptBundlerResolution` coming in next nitro version
const nitroConfig: NitroConfig = defu(_nitroConfig, {
debug: nuxt.options.debug,
rootDir: nuxt.options.rootDir,
@ -44,6 +45,10 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
srcDir: nuxt.options.serverDir,
dev: nuxt.options.dev,
buildDir: nuxt.options.buildDir,
experimental: {
// @ts-expect-error `typescriptBundlerResolution` coming in next nitro version
typescriptBundlerResolution: nuxt.options.experimental.typescriptBundlerResolution || nuxt.options.typescript?.tsConfig?.compilerOptions?.moduleResolution?.toLowercase() === 'bundler' || _nitroConfig.typescript?.tsConfig?.compilerOptions?.moduleResolution?.toLowercase() === 'bundler'
},
imports: {
autoImport: nuxt.options.imports.autoImport as boolean,
imports: [

View File

@ -147,6 +147,27 @@ export default defineUntypedSchema({
*/
configSchema: true,
/**
* This enables 'Bundler' module resolution mode for TypeScript, which is the recommended setting
* for frameworks like Nuxt and Vite.
*
* It improves type support when using modern libraries with `exports`.
*
* This is only not enabled by default because it could be a breaking change for some projects.
*
* See https://github.com/microsoft/TypeScript/pull/51669
*/
typescriptBundlerResolution: {
async $resolve (val, get) {
if (typeof val === 'boolean') { return val }
const setting = await get('typescript.tsConfig.compilerOptions.moduleResolution')
if (setting) {
return setting.toLowerCase() === 'bundler'
}
return false
}
},
/**
* Whether or not to add a compatibility layer for modules, plugins or user code relying on the old
* `@vueuse/head` API.

View File

@ -46,7 +46,7 @@ export default defineUntypedSchema({
/**
* You can extend generated `.nuxt/tsconfig.json` using this option.
* @type {typeof import('pkg-types')['readPackageJSON']}
* @type {typeof import('pkg-types')['TSConfig']}
*/
tsConfig: {},

View File

@ -2,15 +2,8 @@ import { addTypeTemplate } from 'nuxt/kit'
export default defineNuxtConfig({
experimental: {
typedPages: true
},
typescript: {
strict: true,
tsConfig: {
compilerOptions: {
moduleResolution: process.env.MODULE_RESOLUTION
}
}
typedPages: true,
typescriptBundlerResolution: process.env.MODULE_RESOLUTION === 'bundler'
},
buildDir: process.env.NITRO_BUILD_DIR,
builder: process.env.TEST_BUILDER as 'webpack' | 'vite' ?? 'vite',