2021-10-02 16:01:17 +00:00
|
|
|
import { createRequire } from 'module'
|
2021-10-02 18:31:00 +00:00
|
|
|
import { defineNuxtModule, installModule, checkNuxtCompatibilityIssues } from '@nuxt/kit'
|
2021-09-05 21:21:33 +00:00
|
|
|
import { setupNitroBridge } from './nitro'
|
|
|
|
import { setupAppBridge } from './app'
|
2021-09-22 15:34:11 +00:00
|
|
|
import { setupCAPIBridge } from './capi'
|
2021-10-02 11:44:30 +00:00
|
|
|
import { setupBetterResolve } from './resolve'
|
2021-10-02 16:59:32 +00:00
|
|
|
import { setupGlobalImports } from './global-imports'
|
2021-10-05 19:18:09 +00:00
|
|
|
import { setupTypescript } from './typescript'
|
2021-09-05 21:21:33 +00:00
|
|
|
|
2021-10-05 20:47:58 +00:00
|
|
|
export interface BridgeConfig {
|
|
|
|
nitro: boolean
|
|
|
|
vite: boolean
|
|
|
|
app: boolean | {}
|
|
|
|
capi: boolean | {}
|
|
|
|
globalImports: boolean
|
|
|
|
constraints: boolean
|
|
|
|
postcss8: boolean
|
|
|
|
swc: boolean
|
|
|
|
resolve: boolean
|
|
|
|
typescript: boolean
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
export default defineNuxtModule({
|
|
|
|
name: 'nuxt-bridge',
|
|
|
|
configKey: 'bridge',
|
|
|
|
defaults: {
|
|
|
|
nitro: true,
|
|
|
|
vite: false,
|
2021-09-22 15:34:11 +00:00
|
|
|
app: {},
|
|
|
|
capi: {},
|
2021-10-02 16:59:32 +00:00
|
|
|
globalImports: true,
|
2021-10-02 18:31:00 +00:00
|
|
|
constraints: true,
|
2021-09-05 21:21:33 +00:00
|
|
|
// TODO: Remove from 2.16
|
|
|
|
postcss8: true,
|
2021-10-05 19:18:09 +00:00
|
|
|
typescript: true,
|
2021-10-02 11:44:30 +00:00
|
|
|
resolve: true
|
2021-10-05 20:47:58 +00:00
|
|
|
} as BridgeConfig,
|
2021-09-05 21:21:33 +00:00
|
|
|
async setup (opts, nuxt) {
|
2021-10-02 16:59:32 +00:00
|
|
|
const _require = createRequire(import.meta.url)
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
if (opts.nitro) {
|
|
|
|
await setupNitroBridge()
|
|
|
|
}
|
|
|
|
if (opts.app) {
|
2021-09-22 15:34:11 +00:00
|
|
|
await setupAppBridge(opts.app)
|
|
|
|
}
|
|
|
|
if (opts.capi) {
|
|
|
|
if (!opts.app) {
|
|
|
|
throw new Error('[bridge] Cannot enable composition-api with app disabled!')
|
|
|
|
}
|
|
|
|
await setupCAPIBridge(opts.capi)
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
2021-10-02 16:59:32 +00:00
|
|
|
if (opts.globalImports) {
|
|
|
|
await setupGlobalImports()
|
|
|
|
}
|
2021-09-05 21:21:33 +00:00
|
|
|
if (opts.vite) {
|
2021-10-02 16:01:17 +00:00
|
|
|
await installModule(nuxt, _require.resolve('nuxt-vite'))
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
|
|
|
if (opts.postcss8) {
|
2021-10-02 16:01:17 +00:00
|
|
|
await installModule(nuxt, _require.resolve('@nuxt/postcss8'))
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
2021-10-05 19:18:09 +00:00
|
|
|
if (opts.typescript) {
|
|
|
|
await setupTypescript()
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
2021-10-02 11:44:30 +00:00
|
|
|
if (opts.resolve) {
|
|
|
|
setupBetterResolve()
|
|
|
|
}
|
2021-10-02 18:31:00 +00:00
|
|
|
if (opts.constraints) {
|
|
|
|
nuxt.hook('modules:done', (moduleContainer: any) => {
|
|
|
|
for (const [name, m] of Object.entries(moduleContainer.requiredModules || {})) {
|
|
|
|
const requires = (m as any)?.handler?.meta?.requires
|
|
|
|
if (requires) {
|
|
|
|
const issues = checkNuxtCompatibilityIssues(requires, nuxt)
|
|
|
|
if (issues.length) {
|
|
|
|
console.warn(`[bridge] Detected module incompatibility issues for \`${name}\`:\n` + issues.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
|
|
|
})
|
2021-10-05 20:47:58 +00:00
|
|
|
|
|
|
|
declare module '@nuxt/kit' {
|
|
|
|
interface NuxtConfig {
|
|
|
|
bridge?: Partial<BridgeConfig>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
declare module '@nuxt/types' {
|
|
|
|
interface NuxtConfig {
|
|
|
|
bridge?: Partial<BridgeConfig>
|
|
|
|
}
|
|
|
|
}
|