Skip to content

Commit

Permalink
feat: nitro app plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 6, 2022
1 parent 3d82df1 commit 613a559
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 3 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ export default definenitroConfig({
All assets in `public/` directory will be automatically served.
## Nitro plugins
In order to extend nitro's runtime behavior, we can register plugins.
They will be synchronously on first nitro initialization given `nitroApp` context which can be used to hook into lifecycle events.
**Example:** Simple plugin
```js
// plugins/test.ts
import { defineNitroPlugin } from '#nitro'
export default defineNitroPlugin((nitroApp) => {
console.log('Nitro plugin', nitroApp)
})
```
```js
// nitro.config.ts
import { definenitroConfig } from 'nitropack'
export default definenitroConfig({
plugins: [
'~/plugins/test'
]
})
```
## Deployment Presets
Currently supported presets:
Expand Down Expand Up @@ -353,6 +381,12 @@ Watch options for development mode. See [chokidar](https://github.com/paulmillr/
Auto import options. See [unjs/unimport](https://github.com/unjs/unimport) for more information.
#### `plugins`
- Default: `[]`
Array of paths to nitro plugins. They will be executed by order on first initialization.
### Routing
#### `baseURL`
Expand Down
5 changes: 4 additions & 1 deletion playground/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export default defineNitroConfig({
// baseURL: '/app',
routes: {
'/api/swr': { swr: true }
}
},
plugins: [
'~/plugins/test'
]
})
5 changes: 5 additions & 0 deletions playground/plugins/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineNitroPlugin } from '#nitro'

export default defineNitroPlugin((_nitroApp) => {
console.log('Nitro plugin!')
})
4 changes: 3 additions & 1 deletion src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const nitroImports: Preset[] = [
'defineCachedEventHandler',
'useConfig',
'useStorage',
'useNitroApp'
'useNitroApp',
'defineNitroPlugin',
'nitroPlugin'
]
},
{
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const NitroDefaults: NitroConfig = {
storage: {},
publicAssets: [],
serverAssets: [],
plugins: [],
autoImport: {
presets: nitroImports
},
Expand Down
12 changes: 12 additions & 0 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as unenv from 'unenv'
import type { Preset } from 'unenv'
import { sanitizeFilePath } from 'mlly'
import unimportPlugin from 'unimport/unplugin'
import { hash } from 'ohash'
import type { Nitro } from '../types'
import { resolveAliases } from '../utils'
import { runtimeDir } from '../dirs'
Expand Down Expand Up @@ -205,6 +206,17 @@ export const getRollupConfig = (nitro: Nitro) => {
'#polyfill': env.polyfill.map(p => `import '${p}';`).join('\n')
}))

// Plugins
rollupConfig.plugins.push(virtual({
'#nitro/virtual/plugins': `
${nitro.options.plugins.map(plugin => `import _${hash(plugin)} from '${plugin}';`).join('\n')}
export const plugins = [
${nitro.options.plugins.map(plugin => `_${hash(plugin)}`).join(',\n')}
]
`
}))

// https://github.com/rollup/plugins/tree/master/packages/alias
rollupConfig.plugins.push(alias({
entries: resolveAliases({
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createHooks, Hookable } from 'hookable'
import { useConfig } from './config'
import { timingMiddleware } from './timing'
import { cachedEventHandler } from './cache'
import { plugins } from '#nitro/virtual/plugins'
import handleError from '#nitro/error'
import { handlers } from '#nitro/virtual/server-handlers'

Expand Down Expand Up @@ -59,12 +60,18 @@ function createNitroApp (): NitroApp {
const $fetch = createFetch({ fetch: localFetch, Headers })
globalThis.$fetch = $fetch

return {
const app: NitroApp = {
hooks,
h3App,
localCall,
localFetch
}

for (const plugin of plugins) {
plugin(app)
}

return app
}

export const nitroApp: NitroApp = createNitroApp()
Expand Down
1 change: 1 addition & 0 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { useStorage } from '#nitro/virtual/storage'
export { useConfig } from './config'
export { defineCachedFunction, defineCachedEventHandler } from './cache'
export { useNitroApp } from './app'
export * from './plugin'
11 changes: 11 additions & 0 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { NitroApp } from './app'

export interface NitroAppPlugin {
(nitro: NitroApp): void
}

export function defineNitroPlugin (def: NitroAppPlugin) {
return def
}

export const nitroPlugin = defineNitroPlugin
3 changes: 3 additions & 0 deletions src/runtime/virtual/plugins.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { NitroAppPlugin } from '../plugin'

export const plugins: NitroAppPlugin[] = []
1 change: 1 addition & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export interface NitroOptions {
serverAssets: ServerAssetDir[]
publicAssets: PublicAssetDir[]
autoImport: UnimportOptions
plugins: string[]

// Dev
dev: boolean
Expand Down

0 comments on commit 613a559

Please sign in to comment.