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-11-29 12:17:29 +00:00
|
|
|
import type { BridgeConfig, ScriptSetupOptions } from '../types'
|
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-11 08:07:27 +00:00
|
|
|
import { setupAutoImports } from './auto-imports'
|
2021-10-05 19:18:09 +00:00
|
|
|
import { setupTypescript } from './typescript'
|
2021-10-06 12:37:45 +00:00
|
|
|
import { setupMeta } from './meta'
|
2021-10-06 17:59:35 +00:00
|
|
|
import { setupTranspile } from './transpile'
|
2021-10-07 08:31:29 +00:00
|
|
|
import { setupScriptSetup } from './setup'
|
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-06 17:59:35 +00:00
|
|
|
transpile: true,
|
2021-10-07 08:31:29 +00:00
|
|
|
scriptSetup: true,
|
2021-10-11 08:07:27 +00:00
|
|
|
autoImports: true,
|
2021-10-02 18:31:00 +00:00
|
|
|
constraints: true,
|
2021-10-06 12:37:45 +00:00
|
|
|
meta: null,
|
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-07 08:31:29 +00:00
|
|
|
if (opts.scriptSetup) {
|
2021-11-29 12:17:29 +00:00
|
|
|
await setupScriptSetup(opts.scriptSetup as ScriptSetupOptions)
|
2021-10-07 08:31:29 +00:00
|
|
|
}
|
2021-10-11 08:07:27 +00:00
|
|
|
if (opts.autoImports) {
|
|
|
|
await setupAutoImports()
|
2021-10-02 16:59:32 +00:00
|
|
|
}
|
2021-09-05 21:21:33 +00:00
|
|
|
if (opts.vite) {
|
2021-10-27 09:48:17 +00:00
|
|
|
const viteModule = await import('./vite/module').then(r => r.default || r)
|
2021-10-26 12:59:05 +00:00
|
|
|
await installModule(nuxt, viteModule)
|
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-06 17:59:35 +00:00
|
|
|
if (opts.transpile) {
|
|
|
|
setupTranspile()
|
|
|
|
}
|
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-10-06 12:37:45 +00:00
|
|
|
if (opts.meta !== false && opts.capi) {
|
|
|
|
await setupMeta({ needsExplicitEnable: opts.meta === null })
|
|
|
|
}
|
2021-09-05 21:21:33 +00:00
|
|
|
}
|
|
|
|
})
|