2021-02-19 01:08:45 +00:00
|
|
|
|
|
|
|
import { NuxtApp } from './app'
|
|
|
|
import { Builder } from './builder'
|
|
|
|
import { resolveFiles } from './utils'
|
|
|
|
|
|
|
|
export interface NuxtPlugin {
|
|
|
|
src: string
|
|
|
|
mode: 'server' | 'client' | 'all'
|
|
|
|
}
|
|
|
|
|
|
|
|
const MODES_REGEX = /\.(server|client)(\.\w+)*$/
|
2021-04-04 22:15:34 +00:00
|
|
|
const getPluginMode = (src: string) => {
|
2021-02-19 01:08:45 +00:00
|
|
|
const [, mode = 'all'] = src.match(MODES_REGEX) || []
|
|
|
|
|
2021-04-04 22:15:34 +00:00
|
|
|
return mode as NuxtPlugin['mode']
|
2021-02-19 01:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:15:34 +00:00
|
|
|
export async function resolvePlugins (builder: Builder, app: NuxtApp) {
|
|
|
|
const plugins = await resolveFiles(builder, 'plugins/**/*.{js,ts}', app.dir)
|
|
|
|
|
|
|
|
return plugins.map(src => ({
|
|
|
|
src,
|
|
|
|
mode: getPluginMode(src)
|
|
|
|
})
|
|
|
|
)
|
2021-02-19 01:08:45 +00:00
|
|
|
}
|