Skip to content

Commit

Permalink
feat(nitro): storage support (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 11, 2021
1 parent 5acb394 commit 958563c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Preset } from '@nuxt/un'
import { tryImport, resolvePath, detectTarget, extendPreset } from './utils'
import * as PRESETS from './presets'
import type { NodeExternalsOptions } from './rollup/plugins/externals'
import type { StorageOptions } from './rollup/plugins/storage'
import type { ServerMiddleware } from './server/middleware'

export interface NitroContext {
Expand All @@ -32,6 +33,7 @@ export interface NitroContext {
serverDir: string
publicDir: string
}
storage: StorageOptions,
_nuxt: {
majorVersion: number
dev: boolean
Expand Down Expand Up @@ -85,6 +87,7 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N
serverDir: '{{ output.dir }}/server',
publicDir: '{{ output.dir }}/public'
},
storage: { mounts: { } },
_nuxt: {
majorVersion: nuxtOptions._majorVersion || 2,
dev: nuxtOptions.dev,
Expand Down Expand Up @@ -129,6 +132,22 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N

nitroContext._internal.hooks.addHooks(nitroContext.hooks)

// Dev-only storage
if (nitroContext._nuxt.dev) {
const fsMounts = {
root: resolve(nitroContext._nuxt.rootDir),
src: resolve(nitroContext._nuxt.srcDir),
build: resolve(nitroContext._nuxt.buildDir),
cache: resolve(nitroContext._nuxt.rootDir, '.nuxt/nitro/cache')
}
for (const p in fsMounts) {
nitroContext.storage.mounts[p] = nitroContext.storage.mounts[p] || {
driver: 'fs',
driverOptions: { base: fsMounts[p] }
}
}
}

// console.log(nitroContext)
// process.exit(1)

Expand Down
1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"std-env": "^2.3.0",
"table": "^6.0.9",
"ufo": "^0.6.11",
"unstorage": "^0.1.2",
"upath": "^2.0.1",
"vue": "3.0.11",
"vue-bundle-renderer": "^0.2.3",
Expand Down
4 changes: 4 additions & 0 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { staticAssets, dirnames } from './plugins/static'
import { middleware } from './plugins/middleware'
import { esbuild } from './plugins/esbuild'
import { raw } from './plugins/raw'
import { storage } from './plugins/storage'

export type RollupConfig = InputOptions & { output: OutputOptions }

Expand Down Expand Up @@ -153,6 +154,9 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
rollupConfig.plugins.push(staticAssets(nitroContext))
}

// Storage
rollupConfig.plugins.push(storage(nitroContext.storage))

// Middleware
rollupConfig.plugins.push(middleware(() => {
const _middleware = [
Expand Down
46 changes: 46 additions & 0 deletions src/rollup/plugins/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import virtual from '@rollup/plugin-virtual'

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

const drivers = {
fs: 'unstorage/drivers/fs',
http: 'unstorage/drivers/http',
memory: 'unstorage/drivers/memory'
}

export function storage (opts: StorageOptions) {
const mounts: { path: string, driver: string, opts: object }[] = []

for (const path in opts.mounts) {
const mount = opts.mounts[path]
mounts.push({
path,
driver: drivers[mount.driver] || mount.driver,
opts: mount.driverOptions || {}
})
}

const driverImports = Array.from(new Set(mounts.map(m => m.driver)))

return virtual({
'~nitro/storage': `
import { createStorage } from 'unstorage'
${driverImports.map(i => `import ${getImportName(i)} from '${i}'`).join('\n')}
export const storage = createStorage({})
${mounts.map(m => `storage.mount('${m.path}', ${getImportName(m.driver)}(${JSON.stringify(m.opts)}))`).join('\n')}
`
})
}

function getImportName (id: string) {
return '_' + id.replace(/[\\/.]/g, '_')
}

0 comments on commit 958563c

Please sign in to comment.