From 8b481c8ca43bbe6d5ba5e75af53d4db7b694d7b5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 26 Jan 2022 14:45:23 +0100 Subject: [PATCH 1/9] refactor: nitro interface --- src/build.ts | 127 ++++++++++---------- src/context.ts | 210 --------------------------------- src/index.ts | 2 +- src/nitro.ts | 123 +++++++++++++++++++ src/presets/azure.ts | 12 +- src/presets/azure_functions.ts | 12 +- src/presets/browser.ts | 18 +-- src/presets/cli.ts | 12 +- src/presets/cloudflare.ts | 12 +- src/presets/dev.ts | 9 +- src/presets/firebase.ts | 14 +-- src/presets/lambda.ts | 8 +- src/presets/netlify.ts | 16 +-- src/presets/node.ts | 8 +- src/presets/server.ts | 18 +-- src/presets/vercel.ts | 14 +-- src/presets/worker.ts | 6 +- src/rollup/config.ts | 206 +++++++++++++++----------------- src/rollup/plugins/static.ts | 11 +- src/runtime/server/index.ts | 12 +- src/server/dev.ts | 28 ++--- src/server/middleware.ts | 60 +++++----- src/server/vfs.ts | 6 +- src/types/index.d.ts | 10 +- src/types/nitro.d.ts | 93 +++++++++++++++ src/types/shims.d.ts | 15 +-- src/utils/index.ts | 42 +++---- src/utils/wpfs.ts | 2 +- 28 files changed, 536 insertions(+), 570 deletions(-) delete mode 100644 src/context.ts create mode 100644 src/nitro.ts create mode 100644 src/types/nitro.d.ts diff --git a/src/build.ts b/src/build.ts index 1e8e9e5f7b..e5f4922b14 100644 --- a/src/build.ts +++ b/src/build.ts @@ -4,21 +4,19 @@ import * as rollup from 'rollup' import fse from 'fs-extra' import { printFSTree } from './utils/tree' import { getRollupConfig } from './rollup/config' -import { hl, prettyPath, serializeTemplate, writeFile, isDirectory, replaceAll } from './utils' -import { NitroContext } from './context' +import { prettyPath, writeFile, isDirectory, replaceAll } from './utils' import { scanMiddleware } from './server/middleware' +import type { Nitro } from './types' -export async function prepare (nitroContext: NitroContext) { - consola.info(`Nitro preset is ${hl(nitroContext.preset)}`) +export async function prepare (nitro: Nitro) { + await cleanupDir(nitro.options.output.dir) - await cleanupDir(nitroContext.output.dir) - - if (!nitroContext.output.publicDir.startsWith(nitroContext.output.dir)) { - await cleanupDir(nitroContext.output.publicDir) + if (!nitro.options.output.publicDir.startsWith(nitro.options.output.dir)) { + await cleanupDir(nitro.options.output.publicDir) } - if (!nitroContext.output.serverDir.startsWith(nitroContext.output.dir)) { - await cleanupDir(nitroContext.output.serverDir) + if (!nitro.options.output.serverDir.startsWith(nitro.options.output.dir)) { + await cleanupDir(nitro.options.output.serverDir) } } @@ -27,51 +25,48 @@ async function cleanupDir (dir: string) { await fse.emptyDir(dir) } -export async function generate (nitroContext: NitroContext) { +export async function generate (nitro: Nitro) { consola.start('Generating public...') - await nitroContext._internal.hooks.callHook('nitro:generate', nitroContext) - - const publicDir = nitroContext._nuxt.publicDir - if (await isDirectory(publicDir)) { - await fse.copy(publicDir, nitroContext.output.publicDir) + const clientDist = resolve(nitro.options.buildDir, 'dist/client') + if (await isDirectory(clientDist)) { + await fse.copy(clientDist, join(nitro.options.output.publicDir, nitro.options.publicPath)) } - const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client') - if (await isDirectory(clientDist)) { - const buildAssetsDir = join(nitroContext.output.publicDir, nitroContext._nuxt.buildAssetsDir) - await fse.copy(clientDist, buildAssetsDir) + const publicDir = nitro.options.publicDir + if (await isDirectory(publicDir)) { + await fse.copy(publicDir, nitro.options.output.publicDir) } - consola.success('Generated public ' + prettyPath(nitroContext.output.publicDir)) + consola.success('Generated public ' + prettyPath(nitro.options.output.publicDir)) } -export async function build (nitroContext: NitroContext) { +export async function build (nitro: Nitro) { // Compile html template - const htmlSrc = resolve(nitroContext._nuxt.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`) - const htmlTemplate = { src: htmlSrc, contents: '', dst: '' } - htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.mjs').replace('app.template.mjs', 'document.template.mjs') - htmlTemplate.contents = nitroContext.vfs[htmlTemplate.src] || await fse.readFile(htmlTemplate.src, 'utf-8') - await nitroContext._internal.hooks.callHook('nitro:document', htmlTemplate) - const compiled = 'export default ' + serializeTemplate(htmlTemplate.contents) - await writeFile(htmlTemplate.dst, compiled) - - nitroContext.rollupConfig = getRollupConfig(nitroContext) - await nitroContext._internal.hooks.callHook('nitro:rollup:before', nitroContext) - return nitroContext._nuxt.dev ? _watch(nitroContext) : _build(nitroContext) + // const htmlSrc = resolve(nitro.options.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`) + // const htmlTemplate = { src: htmlSrc, contents: '', dst: '' } + // htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.mjs').replace('app.template.mjs', 'document.template.mjs') + // htmlTemplate.contents = nitro.vfs[htmlTemplate.src] || await fse.readFile(htmlTemplate.src, 'utf-8') + // await nitro.hooks.callHook('nitro:document', htmlTemplate) + // const compiled = 'export default ' + serializeTemplate(htmlTemplate.contents) + // await writeFile(htmlTemplate.dst, compiled) + + nitro.options.rollupConfig = getRollupConfig(nitro) + await nitro.hooks.callHook('nitro:rollup:before', nitro) + return nitro.options.dev ? _watch(nitro) : _build(nitro) } -export async function writeTypes (nitroContext: NitroContext) { +async function writeTypes (nitro: Nitro) { const routeTypes: Record = {} const middleware = [ - ...nitroContext.scannedMiddleware, - ...nitroContext.middleware + ...nitro.scannedMiddleware, + ...nitro.options.middleware ] for (const mw of middleware) { if (typeof mw.handle !== 'string') { continue } - const relativePath = relative(nitroContext._nuxt.buildDir, mw.handle).replace(/\.[a-z]+$/, '') + const relativePath = relative(nitro.options.buildDir, mw.handle).replace(/\.[a-z]+$/, '') routeTypes[mw.route] = routeTypes[mw.route] || [] routeTypes[mw.route].push(`Awaited>`) } @@ -88,58 +83,58 @@ export async function writeTypes (nitroContext: NitroContext) { 'export {}' ] - await writeFile(join(nitroContext._nuxt.buildDir, 'nitro.d.ts'), lines.join('\n')) + await writeFile(join(nitro.options.buildDir, 'nitro.d.ts'), lines.join('\n')) } -async function _build (nitroContext: NitroContext) { - nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir) - await writeTypes(nitroContext) +async function _build (nitro: Nitro) { + nitro.scannedMiddleware = await scanMiddleware(nitro.options.serverDir) + await writeTypes(nitro) consola.start('Building server...') - const build = await rollup.rollup(nitroContext.rollupConfig).catch((error) => { + const build = await rollup.rollup(nitro.options.rollupConfig).catch((error) => { consola.error('Rollup error: ' + error.message) throw error }) consola.start('Writing server bundle...') - await build.write(nitroContext.rollupConfig.output) + await build.write(nitro.options.rollupConfig.output) const rewriteBuildPaths = (input: unknown, to: string) => - typeof input === 'string' ? replaceAll(input, nitroContext.output.dir, to) : undefined + typeof input === 'string' ? replaceAll(input, nitro.options.output.dir, to) : undefined // Write build info - const nitroConfigPath = resolve(nitroContext.output.dir, 'nitro.json') + const nitroConfigPath = resolve(nitro.options.output.dir, 'nitro.json') const buildInfo = { date: new Date(), - preset: nitroContext.preset, + // preset: nitro.options.preset, commands: { - preview: rewriteBuildPaths(nitroContext.commands.preview, '.'), - deploy: rewriteBuildPaths(nitroContext.commands.deploy, '.') + preview: rewriteBuildPaths(nitro.options.commands.preview, '.'), + deploy: rewriteBuildPaths(nitro.options.commands.deploy, '.') } } await writeFile(nitroConfigPath, JSON.stringify(buildInfo, null, 2)) consola.success('Server built') - await printFSTree(nitroContext.output.serverDir) - await nitroContext._internal.hooks.callHook('nitro:compiled', nitroContext) + await printFSTree(nitro.options.output.serverDir) + await nitro.hooks.callHook('nitro:compiled', nitro) // Show deploy and preview hints - const rOutDir = relative(process.cwd(), nitroContext.output.dir) - if (nitroContext.commands.preview) { + const rOutDir = relative(process.cwd(), nitro.options.output.dir) + if (nitro.options.commands.preview) { // consola.info(`You can preview this build using \`${rewriteBuildPaths(nitroContext.commands.preview, rOutDir)}\``) consola.info('You can preview this build using `nuxi preview`') } - if (nitroContext.commands.deploy) { - consola.info(`You can deploy this build using \`${rewriteBuildPaths(nitroContext.commands.deploy, rOutDir)}\``) + if (nitro.options.commands.deploy) { + consola.info(`You can deploy this build using \`${rewriteBuildPaths(nitro.options.commands.deploy, rOutDir)}\``) } return { - entry: resolve(nitroContext.rollupConfig.output.dir, nitroContext.rollupConfig.output.entryFileNames as string) + entry: resolve(nitro.options.rollupConfig.output.dir, nitro.options.rollupConfig.output.entryFileNames as string) } } -function startRollupWatcher (nitroContext: NitroContext) { - const watcher = rollup.watch(nitroContext.rollupConfig) +function startRollupWatcher (nitro: Nitro) { + const watcher = rollup.watch(nitro.options.rollupConfig) let start: number watcher.on('event', (event) => { @@ -155,31 +150,31 @@ function startRollupWatcher (nitroContext: NitroContext) { // Finished building all bundles case 'END': - nitroContext._internal.hooks.callHook('nitro:compiled', nitroContext) + nitro.hooks.callHook('nitro:compiled', nitro) consola.success('Nitro built', start ? `in ${Date.now() - start} ms` : '') return // Encountered an error while bundling case 'ERROR': consola.error('Rollup error: ' + event.error) - // consola.error(event.error) + // consola.error(event.error) } }) return watcher } -async function _watch (nitroContext: NitroContext) { - let watcher = startRollupWatcher(nitroContext) +async function _watch (nitro: Nitro) { + let watcher = startRollupWatcher(nitro) - nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir, + nitro.scannedMiddleware = await scanMiddleware(nitro.options.serverDir, (middleware, event) => { - nitroContext.scannedMiddleware = middleware + nitro.scannedMiddleware = middleware if (['add', 'addDir'].includes(event)) { watcher.close() - writeTypes(nitroContext).catch(console.error) - watcher = startRollupWatcher(nitroContext) + writeTypes(nitro).catch(console.error) + watcher = startRollupWatcher(nitro) } } ) - await writeTypes(nitroContext) + await writeTypes(nitro) } diff --git a/src/context.ts b/src/context.ts deleted file mode 100644 index bb13c97f22..0000000000 --- a/src/context.ts +++ /dev/null @@ -1,210 +0,0 @@ -/* eslint-disable no-use-before-define */ -import { resolve } from 'pathe' -import defu from 'defu' -import { createHooks, Hookable, NestedHooks } from 'hookable' -import type { Preset } from 'unenv' -import type { NuxtHooks, NuxtOptions } from '@nuxt/schema' -import type { PluginVisualizerOptions } from 'rollup-plugin-visualizer' -import { tryImport, resolvePath, detectTarget, extendPreset, evalTemplate } from './utils' -import * as PRESETS from './presets' -import type { NodeExternalsOptions } from './rollup/plugins/externals' -import type { StorageOptions } from './rollup/plugins/storage' -import type { AssetOptions } from './rollup/plugins/assets' -import type { ServerMiddleware } from './server/middleware' -import type { RollupConfig } from './rollup/config' -import type { Options as EsbuildOptions } from './rollup/plugins/esbuild' -import { runtimeDir } from './dirs' - -export interface NitroHooks { - 'nitro:document': (htmlTemplate: { src: string, contents: string, dst: string }) => void - 'nitro:rollup:before': (context: NitroContext) => void | Promise - 'nitro:compiled': (context: NitroContext) => void - 'nitro:generate': (context: NitroContext) => void | Promise - 'close': () => void -} - -export interface NitroContext { - alias: Record - timing: boolean - inlineDynamicImports: boolean - minify: boolean - sourceMap: boolean - externals: boolean | NodeExternalsOptions - analyze: false | PluginVisualizerOptions - entry: string - node: boolean - preset: string - rollupConfig?: RollupConfig - esbuild?: { - options?: EsbuildOptions - } - experiments?: { - wasm?: boolean - } - commands: { - preview: string | ((config: NitroContext) => string) - deploy: string | ((config: NitroContext) => string) - }, - moduleSideEffects: string[] - renderer: string - serveStatic: boolean - middleware: ServerMiddleware[] - scannedMiddleware: ServerMiddleware[] - hooks: NestedHooks - nuxtHooks: NestedHooks - ignore: string[] - env: Preset - vfs: Record - output: { - dir: string - serverDir: string - publicDir: string - } - storage: StorageOptions, - assets: AssetOptions, - _nuxt: { - majorVersion: number - dev: boolean - ssr: boolean - rootDir: string - srcDir: string - buildDir: string - generateDir: string - publicDir: string - serverDir: string - baseURL: string - buildAssetsDir: string - isStatic: boolean - fullStatic: boolean - staticAssets: any - modulesDir: string[] - runtimeConfig: { public: any, private: any } - } - _internal: { - runtimeDir: string - hooks: Hookable - } -} - -type DeepPartial = T extends Record ? { [P in keyof T]?: DeepPartial | T[P] } : T - -export interface NitroInput extends DeepPartial {} - -export type NitroPreset = NitroInput | ((input: NitroInput) => NitroInput) - -export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): NitroContext { - const defaults: NitroContext = { - alias: {}, - timing: undefined, - inlineDynamicImports: undefined, - minify: undefined, - sourceMap: undefined, - externals: undefined, - analyze: nuxtOptions.build.analyze as any, - entry: undefined, - node: undefined, - preset: undefined, - rollupConfig: undefined, - experiments: {}, - moduleSideEffects: ['unenv/runtime/polyfill/'], - renderer: undefined, - serveStatic: undefined, - commands: { - preview: undefined, - deploy: undefined - }, - middleware: [], - scannedMiddleware: [], - ignore: [], - env: {}, - vfs: {}, - hooks: {}, - nuxtHooks: {}, - output: { - dir: '{{ _nuxt.rootDir }}/.output', - serverDir: '{{ output.dir }}/server', - publicDir: '{{ output.dir }}/public' - }, - storage: { mounts: { } }, - assets: { - inline: !nuxtOptions.dev, - dirs: {} - }, - _nuxt: { - majorVersion: nuxtOptions._majorVersion || 2, - dev: nuxtOptions.dev, - ssr: nuxtOptions.ssr, - rootDir: nuxtOptions.rootDir, - srcDir: nuxtOptions.srcDir, - buildDir: nuxtOptions.buildDir, - generateDir: nuxtOptions.generate.dir, - publicDir: resolve(nuxtOptions.srcDir, nuxtOptions.dir.public || nuxtOptions.dir.static), - serverDir: resolve(nuxtOptions.srcDir, (nuxtOptions.dir as any).server || 'server'), - baseURL: nuxtOptions.app.baseURL || '/', - buildAssetsDir: nuxtOptions.app.buildAssetsDir, - isStatic: nuxtOptions.target === 'static' && !nuxtOptions.dev, - fullStatic: nuxtOptions.target === 'static' && !nuxtOptions._legacyGenerate, - staticAssets: nuxtOptions.generate.staticAssets, - modulesDir: nuxtOptions.modulesDir, - runtimeConfig: { - public: nuxtOptions.publicRuntimeConfig, - private: nuxtOptions.privateRuntimeConfig - } - }, - _internal: { - runtimeDir, - hooks: createHooks() - } - } - - defaults.preset = input.preset || process.env.NITRO_PRESET || detectTarget() || 'server' - // eslint-disable-next-line import/namespace - let presetDefaults = PRESETS[defaults.preset] || tryImport(nuxtOptions.rootDir, defaults.preset) - if (!presetDefaults) { - throw new Error('Cannot resolve preset: ' + defaults.preset) - } - presetDefaults = presetDefaults.default || presetDefaults - - const _presetInput = defu(input, defaults) - const _preset = (extendPreset(presetDefaults /* base */, input) as Function)(_presetInput) - const nitroContext: NitroContext = defu(_preset, defaults) as any - - nitroContext.output.dir = resolvePath(nitroContext, nitroContext.output.dir) - nitroContext.output.publicDir = resolvePath(nitroContext, nitroContext.output.publicDir) - nitroContext.output.serverDir = resolvePath(nitroContext, nitroContext.output.serverDir) - - if (nitroContext.commands.preview) { - nitroContext.commands.preview = evalTemplate(nitroContext, nitroContext.commands.preview) - } - if (nitroContext.commands.deploy) { - nitroContext.commands.deploy = evalTemplate(nitroContext, nitroContext.commands.deploy) - } - - 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] } - } - } - } - - // Assets - nitroContext.assets.dirs.server = { - dir: resolve(nitroContext._nuxt.srcDir, 'server/assets'), meta: true - } - - // console.log(nitroContext) - // process.exit(1) - - return nitroContext -} diff --git a/src/index.ts b/src/index.ts index d8bc866cee..91d17fa0c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ export * from './build' -export * from './context' +export * from './nitro' export * from './server/middleware' export * from './server/dev' export { wpfs } from './utils/wpfs' diff --git a/src/nitro.ts b/src/nitro.ts new file mode 100644 index 0000000000..77e57b7ec7 --- /dev/null +++ b/src/nitro.ts @@ -0,0 +1,123 @@ +import { resolve } from 'pathe' +import defu from 'defu' +import { createHooks } from 'hookable' +import { tryImport, resolvePath, detectTarget } from './utils' +import * as PRESETS from './presets' +import type { Nitro, NitroOptions, NitroConfig } from './types' +// import { mergeHooks } from 'hookable' +import { pkgDir, runtimeDir } from './dirs' + +const nitroDefaults: NitroConfig = { + alias: { + '#nitro': runtimeDir + }, + unenv: {}, + analyze: false, + experiments: {}, + moduleSideEffects: ['unenv/runtime/polyfill/'], + middleware: [], + modulesDir: [], + ignore: [], + hooks: {}, + output: { + dir: '{{ rootDir }}/.output', + serverDir: '{{ output.dir }}/server', + publicDir: '{{ output.dir }}/public' + }, + storage: { mounts: {} }, + assets: { + // inline: !config.dev, + dirs: {} + }, + publicDir: 'public', + serverDir: 'server', + routerBase: '/', + publicPath: '/', + runtimeConfig: { + public: {}, + private: {} + } +} + +export function createNitro (config: NitroConfig = {}): Nitro { + // Apply nitro defaults + config = defu(config, nitroDefaults) + + // Apply preset defaults + config.extends = config.preset = config.extends || config.preset || process.env.NITRO_PRESET || detectTarget() || 'server' + config = extendConfig(config) + + // Normalize options + const options = config as NitroOptions + options.rootDir = resolve(options.rootDir || '.') + for (const key of ['srcDir', 'publicDir', 'serverDir', 'generateDir', 'buildDir']) { + options[key] = resolve(options.rootDir, options[key]) + } + options.modulesDir.push(resolve(options.rootDir, 'node_modules')) + options.modulesDir.push(resolve(pkgDir, 'node_modules')) + + // Create context + const nitro: Nitro = { + options, + hooks: createHooks(), + vfs: {}, + scannedMiddleware: [] + } + + // Init hooks + nitro.hooks.addHooks(nitro.options.hooks) + + // Resolve output dir + options.output.dir = resolvePath(nitro, nitro.options.output.dir) + options.output.publicDir = resolvePath(nitro, nitro.options.output.publicDir) + options.output.serverDir = resolvePath(nitro, nitro.options.output.serverDir) + + // Dev-only storage + if (nitro.options.dev) { + const fsMounts = { + root: resolve(nitro.options.rootDir), + src: resolve(nitro.options.srcDir), + build: resolve(nitro.options.buildDir), + cache: resolve(nitro.options.rootDir, 'node_modules/.nitro/cache') + } + for (const p in fsMounts) { + nitro.options.storage.mounts[p] = nitro.options.storage.mounts[p] || { + driver: 'fs', + driverOptions: { base: fsMounts[p] } + } + } + } + + // Assets + nitro.options.assets.dirs.server = { + dir: resolve(nitro.options.srcDir, 'server/assets'), meta: true + } + + return nitro +} + +function extendConfig (config: NitroConfig): NitroConfig { + if (!config.extends) { + return config + } + + let _extends = config.extends + if (typeof config.extends === 'string') { + _extends = PRESETS[config.extends] || tryImport(config.rootDir, config.extends) || {} + if (!_extends) { + throw new Error('Cannot resolve config: ' + config.extends) + } + _extends = (_extends as any).default || _extends + } + if (typeof _extends === 'function') { + _extends = _extends(config) + } + + // TODO: Merge hooks + const preset = extendConfig(_extends as NitroConfig) + return defu(config, preset) +} + +export function defineNitroPreset (preset: NitroConfig['extends']) { + return preset +} diff --git a/src/presets/azure.ts b/src/presets/azure.ts index 1b5c0dd91c..81a5fe1aaf 100644 --- a/src/presets/azure.ts +++ b/src/presets/azure.ts @@ -2,10 +2,10 @@ import fse from 'fs-extra' import { globby } from 'globby' import { join, resolve } from 'pathe' import { writeFile } from '../utils' -import { NitroPreset, NitroContext } from '../context' +import { defineNitroPreset } from '../nitro' -export const azure: NitroPreset = { - entry: '{{ _internal.runtimeDir }}/entries/azure', +export const azure = defineNitroPreset({ + entry: '#nitro/entries/azure', externals: true, output: { serverDir: '{{ output.dir }}/server/functions' @@ -14,13 +14,13 @@ export const azure: NitroPreset = { preview: 'npx @azure/static-web-apps-cli start {{ output.publicDir }} --api-location {{ output.serverDir }}/..' }, hooks: { - async 'nitro:compiled' (ctx: NitroContext) { + async 'nitro:compiled' (ctx: any) { await writeRoutes(ctx) } } -} +}) -async function writeRoutes ({ output }: NitroContext) { +async function writeRoutes ({ output }) { const host = { version: '2.0' } diff --git a/src/presets/azure_functions.ts b/src/presets/azure_functions.ts index 30d67fec16..0f098bd97d 100644 --- a/src/presets/azure_functions.ts +++ b/src/presets/azure_functions.ts @@ -2,22 +2,22 @@ import { createWriteStream } from 'fs' import archiver from 'archiver' import { join, resolve } from 'pathe' import { writeFile } from '../utils' -import { NitroPreset, NitroContext } from '../context' +import { defineNitroPreset } from '../nitro' // eslint-disable-next-line -export const azure_functions: NitroPreset = { +export const azure_functions = defineNitroPreset({ serveStatic: true, - entry: '{{ _internal.runtimeDir }}/entries/azure_functions', + entry: '#nitro/entries/azure_functions', externals: true, commands: { deploy: 'az functionapp deployment source config-zip -g -n --src {{ output.dir }}/deploy.zip' }, hooks: { - async 'nitro:compiled' (ctx: NitroContext) { + async 'nitro:compiled' (ctx: any) { await writeRoutes(ctx) } } -} +}) function zipDirectory (dir: string, outfile: string): Promise { const archive = archiver('zip', { zlib: { level: 9 } }) @@ -34,7 +34,7 @@ function zipDirectory (dir: string, outfile: string): Promise { }) } -async function writeRoutes ({ output: { dir, serverDir } }: NitroContext) { +async function writeRoutes ({ output: { dir, serverDir } }) { const host = { version: '2.0', extensions: { http: { routePrefix: '' } } diff --git a/src/presets/browser.ts b/src/presets/browser.ts index 5cc7a5d840..e31e1beeed 100644 --- a/src/presets/browser.ts +++ b/src/presets/browser.ts @@ -2,13 +2,12 @@ import { existsSync, promises as fsp } from 'fs' import { resolve } from 'pathe' import consola from 'consola' import { joinURL } from 'ufo' -import { extendPreset, prettyPath } from '../utils' -import { NitroPreset, NitroContext, NitroInput } from '../context' -import { worker } from './worker' +import { prettyPath } from '../utils' +import { defineNitroPreset } from '../nitro' -export const browser: NitroPreset = extendPreset(worker, (input: NitroInput) => { - // TODO: Join base at runtime - const baseURL = input._nuxt.baseURL +export const browser = defineNitroPreset((_input) => { + // TODO + const baseURL = '/' const script = `