feat: export defineNuxtConfig from nuxt3 and @nuxt/bridge (#669)

This commit is contained in:
pooya parsa 2021-10-06 14:31:52 +02:00 committed by GitHub
parent 6dfe0d129a
commit dd73a8bcad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 75 additions and 82 deletions

View File

@ -31,18 +31,17 @@ npm install -D @nuxt/bridge@npm:@nuxt/bridge-edge
yarn add --dev @nuxt/bridge@npm:@nuxt/bridge-edge
```
Add `@nuxt/bridge` to `buildModules` inside `nuxt.config`:
Update `nuxt.config` file:
```ts [nuxt.config.js]
export default {
buildModules: [
'@nuxt/bridge'
],
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
nitro: true,
// vite: true
}
}
})
```
If you have a project with `target: 'static'`, update "build" script to use `nuxt generate`

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
// vite: true

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
})

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
})

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
})

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
vite: false

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
vite: true

View File

@ -2,3 +2,15 @@
module.exports = function (...args) {
return import('./dist/module.mjs').then(m => m.default.call(this, ...args))
}
module.exports.defineNuxtConfig = (config = {}) => {
if (config.bridge !== false) {
if (!config.buildModules) {
config.buildModules = []
}
if (!config.buildModules.find(m => m === '@nuxt/bridge')) {
config.buildModules.push('@nuxt/bridge')
}
}
return config
}

View File

@ -5,9 +5,10 @@
"license": "MIT",
"type": "module",
"main": "./module.cjs",
"types": "./dist/module.d.ts",
"types": "./types.d.ts",
"files": [
"module.cjs",
"types.d.ts",
"dist"
],
"scripts": {

View File

@ -1,5 +1,6 @@
import { createRequire } from 'module'
import { defineNuxtModule, installModule, checkNuxtCompatibilityIssues } from '@nuxt/kit'
import type { BridgeConfig } from '../types'
import { setupNitroBridge } from './nitro'
import { setupAppBridge } from './app'
import { setupCAPIBridge } from './capi'
@ -7,19 +8,6 @@ import { setupBetterResolve } from './resolve'
import { setupGlobalImports } from './global-imports'
import { setupTypescript } from './typescript'
export interface BridgeConfig {
nitro: boolean
vite: boolean
app: boolean | {}
capi: boolean | {}
globalImports: boolean
constraints: boolean
postcss8: boolean
swc: boolean
resolve: boolean
typescript: boolean
}
export default defineNuxtModule({
name: 'nuxt-bridge',
configKey: 'bridge',
@ -80,16 +68,3 @@ export default defineNuxtModule({
}
}
})
declare module '@nuxt/kit' {
interface NuxtConfig {
bridge?: Partial<BridgeConfig>
}
}
// @ts-ignore
declare module '@nuxt/types' {
interface NuxtConfig {
bridge?: Partial<BridgeConfig>
}
}

27
packages/bridge/types.d.ts vendored Normal file
View File

@ -0,0 +1,27 @@
import type { NuxtConfig as _NuxtConfig } from '@nuxt/kit'
export interface BridgeConfig {
nitro: boolean
vite: boolean
app: boolean | {}
capi: boolean | {}
globalImports: boolean
constraints: boolean
postcss8: boolean
swc: boolean
resolve: boolean
typescript: boolean
}
// TODO: Also inherit from @nuxt/types.NuxtConfig for legacy type compat
export interface NuxtConfig extends _NuxtConfig {
bridge?: Partial<BridgeConfig> | false
}
declare module '@nuxt/kit' {
interface ConfigSchema {
bridge: BridgeConfig
}
}
export declare function defineNuxtConfig(config: NuxtConfig): NuxtConfig

View File

@ -17,20 +17,6 @@ export const nuxtCtx = getContext<Nuxt>('nuxt')
*/
export const useNuxt = nuxtCtx.use
/**
* This helper serves to add the correct typings for your `nuxt.config.js`.
*
* @example
* ```ts
* import { defineNuxtConfig } from '@nuxt/kit'
* export default defineNuxtConfig({
* // your Nuxt config
* })
*/
export function defineNuxtConfig (config: NuxtConfig) {
return config
}
export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
rootDir: string
dev?: boolean

View File

@ -1,23 +1,19 @@
import { ConfigSchema } from '../../schema/config'
import { ConfigSchema as _ConfigSchema } from '../../schema/config'
import { ModuleInstallOptions } from './module'
import { NuxtHooks } from './hooks'
export interface TypedConfigSchema extends ConfigSchema {
export interface ConfigSchema extends _ConfigSchema {
hooks: NuxtHooks,
modules: ModuleInstallOptions[]
buildModules: ModuleInstallOptions[]
/**
* Enable vite mode
*
* @default false
*/
vite: boolean | import('vite').InlineConfig
[key: string]: any
// TODO: Move to schema when untyped supports type annotation
vite: boolean | import('vite').InlineConfig
}
export interface NuxtOptions extends TypedConfigSchema { }
export interface NuxtOptions extends ConfigSchema { }
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> | T[P] }
export interface NuxtConfig extends DeepPartial<TypedConfigSchema> { }
export interface NuxtConfig extends DeepPartial<ConfigSchema> { }

View File

@ -2,14 +2,8 @@ import './shims'
import { NitroInput } from '../dist'
declare module '@nuxt/kit' {
interface NuxtConfig {
nitro?: NitroInput
}
}
declare module '@nuxt/types' {
interface NuxtConfig {
nitro?: NitroInput
interface ConfigSchema {
nitro: NitroInput
}
}

View File

@ -1,6 +1,6 @@
import { resolve } from 'pathe'
import { createHooks } from 'hookable'
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer, NuxtHooks } from '@nuxt/kit'
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, NuxtConfig, nuxtCtx, installModule, ModuleContainer, NuxtHooks } from '@nuxt/kit'
import pagesModule from '../pages/module'
import metaModule from '../meta/module'
import componentsModule from '../components/module'
@ -72,3 +72,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
return nuxt
}
export function defineNuxtConfig (config: NuxtConfig): NuxtConfig {
return config
}

View File

@ -15,7 +15,7 @@ export interface MetaObject extends Record<string, any> {
}
declare module '@nuxt/kit' {
interface NuxtConfig {
meta?: MetaObject,
interface ConfigSchema {
meta: MetaObject,
}
}

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
})

View File

@ -1,4 +1,4 @@
import { defineNuxtConfig } from '@nuxt/kit'
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
buildDir: process.env.NITRO_BUILD_DIR,

View File

@ -1,11 +1,10 @@
import { defineNuxtConfig } from '@nuxt/bridge'
// @ts-ignore
global.__NUXT_PREPATHS__ = (global.__NUXT_PREPATHS__ || []).concat(__dirname)
export default {
export default defineNuxtConfig({
components: true,
buildModules: [
'@nuxt/bridge'
],
serverMiddleware: [
{
handle (req, _res, next) {
@ -19,4 +18,4 @@ export default {
nitro: {
output: { dir: process.env.NITRO_OUTPUT_DIR }
}
}
})