Skip to content

Commit

Permalink
fix(nuxt): provide node16-style type exports (#18431)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 11, 2023
1 parent e6bbf71 commit b19b35f
Show file tree
Hide file tree
Showing 30 changed files with 107 additions and 41 deletions.
8 changes: 7 additions & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"repository": "nuxt/nuxt",
"license": "MIT",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./package.json": "./package.json"
},
"files": [
"dist"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/app.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './dist/app'
export * from './dist/app/index.js'
9 changes: 7 additions & 2 deletions packages/nuxt/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ export default defineBuildConfig({
// Core
{ input: 'src/index' },
// App
{ input: 'src/app/', outDir: 'dist/app/' },
{ input: 'src/app/', outDir: 'dist/app/', ext: 'js' },
// Runtime dirs
...[
'core',
'head',
'components',
'pages'
].map(name => ({ input: `src/${name}/runtime/`, outDir: `dist/${name}/runtime`, format: 'esm' } as BuildEntry))
].map(name => ({ input: `src/${name}/runtime/`, outDir: `dist/${name}/runtime`, format: 'esm', ext: 'js' } as BuildEntry))
],
hooks: {
'mkdist:entry:options' (_ctx, _entry, mkdistOptions) {
mkdistOptions.addRelativeDeclarationExtensions = true
}
},
dependencies: [
'nuxi',
'vue-router',
Expand Down
File renamed without changes.
File renamed without changes.
32 changes: 23 additions & 9 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,48 @@
"repository": "nuxt/nuxt",
"license": "MIT",
"type": "module",
"main": "./dist/index.mjs",
"types": "./types.d.ts",
"bin": {
"nuxi": "./bin/nuxt.mjs",
"nuxt": "./bin/nuxt.mjs"
},
"exports": {
".": "./dist/index.mjs",
".": {
"types": "./types.d.ts",
"import": "./dist/index.mjs"
},
"./config": {
"types": "./config.d.ts",
"import": "./config.mjs",
"import": "./config.js",
"require": "./config.cjs"
},
"./schema": {
"types": "./schema.d.ts",
"import": "./schema.mjs"
"import": "./schema.js"
},
"./kit": {
"types": "./kit.d.ts",
"import": "./kit.mjs"
"import": "./kit.js"
},
"./app": {
"types": "./dist/app/index.d.ts",
"import": "./dist/app/index.js"
},
"./app": "./dist/app/index.mjs",
"./package.json": "./package.json"
},
"imports": {
"#app": "./dist/app/index.mjs",
"#head": "./dist/head/runtime/index.mjs",
"#pages": "./dist/pages/runtime/index.mjs"
"#app": {
"types": "./dist/app/index.d.ts",
"import": "./dist/app/index.js"
},
"#head": {
"types": "./dist/head/runtime/index.d.ts",
"import": "./dist/head/runtime/index.js"
},
"#pages": {
"types": "./dist/pages/runtime/index.d.ts",
"import": "./dist/pages/runtime/index.js"
}
},
"files": [
"app.d.ts",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { reactive } from 'vue'
import { useNuxtApp } from './nuxt'
import type { AppConfig } from 'nuxt/schema'
import { useNuxtApp } from './nuxt'
// @ts-ignore
import __appConfig from '#build/app.config.mjs'

Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// <reference path="types/augments.d.ts" />

export * from './nuxt'
export * from './composables'
export * from './components'
export * from './composables/index'
export * from './components/index'
export * from './config'

// eslint-disable-next-line import/no-restricted-paths
export type { PageMeta } from '../pages/runtime'
export type { PageMeta } from '../pages/runtime/index'

export const isVue2 = false
export const isVue3 = true
3 changes: 2 additions & 1 deletion packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { createHooks } from 'hookable'
import { getContext } from 'unctx'
import type { SSRContext } from 'vue-bundle-renderer/runtime'
import type { H3Event } from 'h3'
import type { RuntimeConfig, AppConfigInput } from 'nuxt/schema'

// eslint-disable-next-line import/no-restricted-paths
import type { NuxtIslandContext } from '../core/runtime/nitro/renderer'
import type { RuntimeConfig, AppConfigInput } from 'nuxt/schema'

const nuxtAppCtx = /* #__PURE__ */ getContext<NuxtApp>('nuxt-app')

Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/components/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { genDynamicImport, genImport } from 'knitwork'
import MagicString from 'magic-string'
import { pascalCase } from 'scule'
import { resolve } from 'pathe'
import type { Component, ComponentsOptions } from 'nuxt/schema'

import { distDir } from '../dirs'
import { isVueTemplate } from './helpers'
import type { Component, ComponentsOptions } from 'nuxt/schema'

interface LoaderOptions {
getComponents (): Component[]
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/components/module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { statSync } from 'node:fs'
import { relative, resolve } from 'pathe'
import { defineNuxtModule, resolveAlias, addTemplate, addPluginTemplate, updateTemplates } from '@nuxt/kit'
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'

import { distDir } from '../dirs'
import { clientFallbackAutoIdPlugin } from './client-fallback-auto-id'
import { componentsPluginTemplate, componentsTemplate, componentsIslandsTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan'
import { loaderPlugin } from './loader'
import { TreeShakeTemplatePlugin } from './tree-shake'
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'

const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string'
const isDirectory = (p: string) => { try { return statSync(p).isDirectory() } catch (_e) { return false } }
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { promises as fsp } from 'node:fs'
import { dirname, resolve, join } from 'pathe'
import { defu } from 'defu'
import { findPath, resolveFiles, normalizePlugin, normalizeTemplate, compileTemplate, templateUtils, tryResolveModule, resolvePath, resolveAlias } from '@nuxt/kit'
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'

import * as defaultTemplates from './templates'
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'

export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
return defu(options, {
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import chokidar from 'chokidar'
import { importModule, isIgnored } from '@nuxt/kit'
import { debounce } from 'perfect-debounce'
import { normalize } from 'pathe'
import { createApp, generateApp as _generateApp } from './app'
import type { Nuxt } from 'nuxt/schema'

import { createApp, generateApp as _generateApp } from './app'

export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)
const generateApp = debounce(() => _generateApp(nuxt, app), undefined, { leading: true })
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import fsExtra from 'fs-extra'
import { dynamicEventHandler } from 'h3'
import { createHeadCore } from '@unhead/vue'
import { renderSSRHead } from '@unhead/ssr'
import type { Nuxt } from 'nuxt/schema'

import { distDir } from '../dirs'
import { ImportProtectionPlugin } from './plugins/import-protection'
import type { Nuxt } from 'nuxt/schema'

export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Resolve config
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join, normalize, relative, resolve } from 'pathe'
import { createHooks, createDebugger } from 'hookable'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { resolvePath, resolveAlias, resolveFiles, loadNuxtConfig, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'
import type { Nuxt, NuxtOptions, NuxtHooks } from 'nuxt/schema'

import escapeRE from 'escape-string-regexp'
import fse from 'fs-extra'
Expand All @@ -22,7 +23,6 @@ import { DevOnlyPlugin } from './plugins/dev-only'
import { addModuleTranspiles } from './modules'
import { initNitro } from './nitro'
import schemaModule from './schema'
import type { Nuxt, NuxtOptions, NuxtHooks } from 'nuxt/schema'

export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>()
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolve
import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports } from 'unimport'
import type { ImportsOptions, ImportPresetWithDeprecation } from 'nuxt/schema'

import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
import type { ImportsOptions, ImportPresetWithDeprecation } from 'nuxt/schema'

export default defineNuxtModule<Partial<ImportsOptions>>({
meta: {
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { join, relative, resolve } from 'pathe'
import { genString, genImport, genObjectFromRawEntries } from 'knitwork'
import escapeRE from 'escape-string-regexp'
import { joinURL } from 'ufo'
import type { NuxtApp, NuxtPage } from 'nuxt/schema'

import { distDir } from '../dirs'
import { resolvePagesRoutes, normalizeRoutes } from './utils'
import type { PageMetaPluginOptions } from './page-meta'
import { PageMetaPlugin } from './page-meta'
import type { NuxtApp, NuxtPage } from 'nuxt/schema'

export default defineNuxtModule({
meta: {
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/pages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { genImport, genDynamicImport, genArrayFromRaw, genSafeVariableName } fro
import escapeRE from 'escape-string-regexp'
import { filename } from 'pathe/utils'
import { hash } from 'ohash'
import { uniqueBy } from '../core/utils'
import type { NuxtPage } from 'nuxt/schema'

import { uniqueBy } from '../core/utils'

enum SegmentParserState {
initial,
static,
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/test/scan-components.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { resolve } from 'node:path'
import { expect, it, vi } from 'vitest'
import { scanComponents } from '../src/components/scan'
import type { ComponentsDir } from 'nuxt/schema'

import { scanComponents } from '../src/components/scan'

const fixtureDir = resolve(__dirname, 'fixture')
const rFixture = (...p: string[]) => resolve(fixtureDir, ...p)

Expand Down
8 changes: 7 additions & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"repository": "nuxt/nuxt",
"license": "MIT",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./package.json": "./package.json"
},
"files": [
"dist",
"schema"
Expand Down
7 changes: 6 additions & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"repository": "nuxt/nuxt",
"license": "MIT",
"type": "module",
"exports": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
}
},
"files": [
"dist"
],
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as _kit from '@nuxt/kit'
import { useTestContext } from './context'

// @ts-ignore type cast
// eslint-disable-next-line
const kit: typeof _kit = _kit.default || _kit

const isNuxtApp = (dir: string) => {
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/runtime/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as _kit from '@nuxt/kit'
import { createTest, exposeContextToEnv } from '@nuxt/test-utils'

// @ts-ignore type cast
// eslint-disable-next-line
const kit: typeof _kit = _kit.default || _kit

const options = JSON.parse(process.env.NUXT_TEST_OPTIONS || '{}')
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as _kit from '@nuxt/kit'
import { useTestContext } from './context'

// @ts-ignore type cast
// eslint-disable-next-line
const kit: typeof _kit = _kit.default || _kit

export async function startServer () {
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"license": "MIT",
"type": "module",
"types": "./dist/index.d.ts",
"exports": "./dist/index.mjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
}
},
"files": [
"dist"
],
Expand Down
5 changes: 4 additions & 1 deletion packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"type": "module",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.mjs",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./dist/*": "./dist/*"
},
"files": [
Expand Down
9 changes: 5 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ declare module 'nitropack' {
}

export default defineNuxtConfig({
typescript: { strict: true },
typescript: {
strict: true,
tsConfig: {
compilerOptions: {
// TODO: For testing (future) support for Node16-style module resolution.
// See https://github.com/nuxt/nuxt/issues/18426 and https://github.com/nuxt/nuxt/pull/18431
// moduleResolution: 'Node16'
}
}
},
app: {
pageTransition: true,
layoutTransition: true,
Expand Down
Loading

0 comments on commit b19b35f

Please sign in to comment.