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', jsx: 'preserve',
target: 'ESNext', target: 'ESNext',
module: 'ESNext', module: 'ESNext',
moduleResolution: 'Node', moduleResolution: nuxt.options.experimental.typescriptBundlerResolution ? 'Bundler' : 'Node',
skipLibCheck: true, skipLibCheck: true,
strict: nuxt.options.typescript?.strict ?? false, strict: nuxt.options.typescript?.strict ?? true,
allowJs: true, allowJs: true,
// TODO: remove by default in 3.7 // TODO: remove by default in 3.7
baseUrl: nuxt.options.srcDir, 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}\`.`) 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, { const nitroConfig: NitroConfig = defu(_nitroConfig, {
debug: nuxt.options.debug, debug: nuxt.options.debug,
rootDir: nuxt.options.rootDir, rootDir: nuxt.options.rootDir,
@ -44,6 +45,10 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
srcDir: nuxt.options.serverDir, srcDir: nuxt.options.serverDir,
dev: nuxt.options.dev, dev: nuxt.options.dev,
buildDir: nuxt.options.buildDir, 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: { imports: {
autoImport: nuxt.options.imports.autoImport as boolean, autoImport: nuxt.options.imports.autoImport as boolean,
imports: [ imports: [

View File

@ -12,8 +12,8 @@ export default defineUntypedSchema({
/** /**
* Enable Vue's reactivity transform * Enable Vue's reactivity transform
* @see https://vuejs.org/guide/extras/reactivity-transform.html * @see https://vuejs.org/guide/extras/reactivity-transform.html
* *
* Warning: Reactivity transform feature has been marked as deprecated in Vue 3.3 and is planned to be * Warning: Reactivity transform feature has been marked as deprecated in Vue 3.3 and is planned to be
* removed from core in Vue 3.4. * removed from core in Vue 3.4.
* @see https://github.com/vuejs/rfcs/discussions/369#discussioncomment-5059028 * @see https://github.com/vuejs/rfcs/discussions/369#discussioncomment-5059028
*/ */
@ -107,7 +107,7 @@ export default defineUntypedSchema({
/** /**
* When this option is enabled (by default) payload of pages generated with `nuxt generate` are extracted * When this option is enabled (by default) payload of pages generated with `nuxt generate` are extracted
* *
* @type {boolean | undefined} * @type {boolean | undefined}
*/ */
payloadExtraction: undefined, payloadExtraction: undefined,
@ -147,6 +147,27 @@ export default defineUntypedSchema({
*/ */
configSchema: true, 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 * Whether or not to add a compatibility layer for modules, plugins or user code relying on the old
* `@vueuse/head` API. * `@vueuse/head` API.

View File

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

View File

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