Skip to content

Commit

Permalink
refactor!: simplify storage options
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 6, 2022
1 parent 5b2c950 commit cffb900
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ import { definenitroConfig } from 'nitropack'
export default definenitroConfig({
storage: {
mounts: {
'/redis': {
driver: 'redis',
driverOptions: { /* redis connector options */ }
}
'/redis': {
driver: 'redis',
/* redis connector options */
}
}
})
Expand Down
5 changes: 1 addition & 4 deletions src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ export async function createNitro (config: NitroConfig = {}): Promise<Nitro> {
cache: resolve(options.buildDir, 'cache')
}
for (const p in fsMounts) {
options.storage.mounts[p] = options.storage.mounts[p] || {
driver: 'fs',
driverOptions: { base: fsMounts[p] }
}
options.storage[p] = options.storage[p] || { driver: 'fs', base: fsMounts[p] }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const NitroDefaults: NitroConfig = {

// Featueres
experimental: {},
storage: { mounts: {} },
storage: {},
publicAssets: [],
serverAssets: [],
autoImport: {
Expand Down
4 changes: 3 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export const getRollupConfig = (nitro: Nitro) => {
}

// Storage
rollupConfig.plugins.push(storage(nitro.options.storage))
rollupConfig.plugins.push(storage({
mounts: nitro.options.storage
}))

// Handlers
rollupConfig.plugins.push(handlers(() => {
Expand Down
20 changes: 12 additions & 8 deletions src/rollup/plugins/storage.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import virtual from '@rollup/plugin-virtual'
import { serializeImportName } from '../../utils'

export interface StorageOptions {
mounts: {
[path: string]: {
driver: 'fs' | 'http' | 'memory',
driverOptions?: Record<string, any>
}
export interface StorageMounts {
[path: string]: {
driver: 'fs' | 'http' | 'memory' | 'redis' | 'cloudflare-kv',
[option: string]: any
}
}

export interface StorageOptions {
mounts: StorageMounts
}

const drivers = {
fs: 'unstorage/drivers/fs',
http: 'unstorage/drivers/http',
memory: 'unstorage/drivers/memory'
memory: 'unstorage/drivers/memory',
redis: 'unstorage/drivers/redis',
'cloudflare-kv': 'unstorage/drivers/cloudflare-kv'
}

export function storage (opts: StorageOptions) {
Expand All @@ -24,7 +28,7 @@ export function storage (opts: StorageOptions) {
mounts.push({
path,
driver: drivers[mount.driver] || mount.driver,
opts: mount.driverOptions || {}
opts: mount
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { NestedHooks, Hookable } from 'hookable'
import type { Consola, LogLevel } from 'consola'
import { WatchOptions } from 'chokidar'
import type { NodeExternalsOptions } from '../rollup/plugins/externals'
import type { StorageOptions } from '../rollup/plugins/storage'
import type { StorageMounts } from '../rollup/plugins/storage'
import type { RollupConfig } from '../rollup/config'
import type { Options as EsbuildOptions } from '../rollup/plugins/esbuild'
import { NitroDevEventHandler, NitroEventHandler } from './handler'
Expand Down Expand Up @@ -91,7 +91,7 @@ export interface NitroOptions {
}

// Features
storage: StorageOptions
storage: StorageMounts
timing: boolean
renderer: string
serveStatic: boolean
Expand Down

0 comments on commit cffb900

Please sign in to comment.