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-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-02 11:44:30 +00:00
|
|
|
swc: true,
|
|
|
|
resolve: true
|
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
|
|
|
}
|
|
|
|
if (opts.swc) {
|
2021-10-02 16:01:17 +00:00
|
|
|
await installModule(nuxt, _require.resolve('nuxt-swc'))
|
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
|
|
|
}
|
|
|
|
})
|