Nuxt/packages/bridge/src/module.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

import { createRequire } from 'module'
import { defineNuxtModule, installModule, checkNuxtCompatibilityIssues } from '@nuxt/kit'
2021-09-05 21:21:33 +00:00
import { setupNitroBridge } from './nitro'
import { setupAppBridge } from './app'
import { setupCAPIBridge } from './capi'
import { setupBetterResolve } from './resolve'
import { setupGlobalImports } from './global-imports'
import { setupTypescript } from './typescript'
2021-09-05 21:21:33 +00:00
export default defineNuxtModule({
name: 'nuxt-bridge',
configKey: 'bridge',
defaults: {
nitro: true,
vite: false,
app: {},
capi: {},
globalImports: true,
constraints: true,
2021-09-05 21:21:33 +00:00
// TODO: Remove from 2.16
postcss8: true,
typescript: true,
resolve: true
2021-09-05 21:21:33 +00:00
},
async setup (opts, nuxt) {
const _require = createRequire(import.meta.url)
2021-09-05 21:21:33 +00:00
if (opts.nitro) {
await setupNitroBridge()
}
if (opts.app) {
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
}
if (opts.globalImports) {
await setupGlobalImports()
}
2021-09-05 21:21:33 +00:00
if (opts.vite) {
await installModule(nuxt, _require.resolve('nuxt-vite'))
2021-09-05 21:21:33 +00:00
}
if (opts.postcss8) {
await installModule(nuxt, _require.resolve('@nuxt/postcss8'))
2021-09-05 21:21:33 +00:00
}
if (opts.typescript) {
await setupTypescript()
2021-09-05 21:21:33 +00:00
}
if (opts.resolve) {
setupBetterResolve()
}
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
}
})