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 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] ```ts [nuxt.config.js]
export default { import { defineNuxtConfig } from '@nuxt/bridge'
buildModules: [
'@nuxt/bridge' export default defineNuxtConfig({
],
bridge: { bridge: {
nitro: true, nitro: true,
// vite: true // vite: true
} }
} })
``` ```
If you have a project with `target: 'static'`, update "build" script to use `nuxt generate` 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({ export default defineNuxtConfig({
// vite: true // vite: true

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,3 +2,15 @@
module.exports = function (...args) { module.exports = function (...args) {
return import('./dist/module.mjs').then(m => m.default.call(this, ...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", "license": "MIT",
"type": "module", "type": "module",
"main": "./module.cjs", "main": "./module.cjs",
"types": "./dist/module.d.ts", "types": "./types.d.ts",
"files": [ "files": [
"module.cjs", "module.cjs",
"types.d.ts",
"dist" "dist"
], ],
"scripts": { "scripts": {

View File

@ -1,5 +1,6 @@
import { createRequire } from 'module' import { createRequire } from 'module'
import { defineNuxtModule, installModule, checkNuxtCompatibilityIssues } from '@nuxt/kit' import { defineNuxtModule, installModule, checkNuxtCompatibilityIssues } from '@nuxt/kit'
import type { BridgeConfig } from '../types'
import { setupNitroBridge } from './nitro' import { setupNitroBridge } from './nitro'
import { setupAppBridge } from './app' import { setupAppBridge } from './app'
import { setupCAPIBridge } from './capi' import { setupCAPIBridge } from './capi'
@ -7,19 +8,6 @@ import { setupBetterResolve } from './resolve'
import { setupGlobalImports } from './global-imports' import { setupGlobalImports } from './global-imports'
import { setupTypescript } from './typescript' 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({ export default defineNuxtModule({
name: 'nuxt-bridge', name: 'nuxt-bridge',
configKey: '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 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 { export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
rootDir: string rootDir: string
dev?: boolean 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 { ModuleInstallOptions } from './module'
import { NuxtHooks } from './hooks' import { NuxtHooks } from './hooks'
export interface TypedConfigSchema extends ConfigSchema { export interface ConfigSchema extends _ConfigSchema {
hooks: NuxtHooks, hooks: NuxtHooks,
modules: ModuleInstallOptions[] modules: ModuleInstallOptions[]
buildModules: ModuleInstallOptions[] buildModules: ModuleInstallOptions[]
/**
* Enable vite mode
*
* @default false
*/
vite: boolean | import('vite').InlineConfig
[key: string]: any [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] } 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' import { NitroInput } from '../dist'
declare module '@nuxt/kit' { declare module '@nuxt/kit' {
interface NuxtConfig { interface ConfigSchema {
nitro?: NitroInput nitro: NitroInput
}
}
declare module '@nuxt/types' {
interface NuxtConfig {
nitro?: NitroInput
} }
} }

View File

@ -1,6 +1,6 @@
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { createHooks } from 'hookable' 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 pagesModule from '../pages/module'
import metaModule from '../meta/module' import metaModule from '../meta/module'
import componentsModule from '../components/module' import componentsModule from '../components/module'
@ -72,3 +72,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
return 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' { declare module '@nuxt/kit' {
interface NuxtConfig { interface ConfigSchema {
meta?: MetaObject, meta: MetaObject,
} }
} }

View File

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

View File

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

View File

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