refactor: add getNitroConfig for consolidating different nitro config sources
This commit is contained in:
parent
5095810077
commit
c282f37fae
10 changed files with 57 additions and 36 deletions
|
@ -3,7 +3,6 @@
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand } from 'citty'
|
||||||
import { dirname, resolve } from 'pathe'
|
import { dirname, resolve } from 'pathe'
|
||||||
import { loadConfig } from 'c12'
|
|
||||||
import {
|
import {
|
||||||
build,
|
build,
|
||||||
copyPublicAssets,
|
copyPublicAssets,
|
||||||
|
@ -12,26 +11,24 @@ import {
|
||||||
prerender,
|
prerender,
|
||||||
} from 'nitropack'
|
} from 'nitropack'
|
||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import { nitroConfig as mcflyNitroConfig } from '../../runtime/nitro-config.js'
|
import { getNitroConfig } from '../../get-nitro-config.js'
|
||||||
|
|
||||||
async function _build(args) {
|
async function _build(args) {
|
||||||
consola.start('Building project...')
|
consola.start('Building project...')
|
||||||
try {
|
try {
|
||||||
const rootDir = resolve(args.dir || args._dir || '.')
|
const rootDir = resolve(args.dir || args._dir || '.')
|
||||||
const { config: mcflyConfig } = await loadConfig({ name: 'mcfly' })
|
|
||||||
const { config: nitroConfig } = await loadConfig({ name: 'nitro' })
|
const nitroConfig = await getNitroConfig()
|
||||||
|
|
||||||
const nitro = await createNitro({
|
const nitro = await createNitro({
|
||||||
rootDir,
|
rootDir,
|
||||||
dev: false,
|
dev: false,
|
||||||
|
|
||||||
|
...nitroConfig,
|
||||||
|
|
||||||
minify: args.minify,
|
minify: args.minify,
|
||||||
preset: args.preset,
|
preset: args.preset,
|
||||||
// spread mcfly.nitro config
|
|
||||||
...(mcflyConfig.nitro ?? {}),
|
|
||||||
...(nitroConfig ?? {}),
|
|
||||||
...mcflyNitroConfig,
|
|
||||||
})
|
})
|
||||||
nitro.options.runtimeConfig.mcfly = mcflyConfig
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
const __dirname = dirname(__filename)
|
const __dirname = dirname(__filename)
|
||||||
|
|
|
@ -12,10 +12,9 @@ import {
|
||||||
prerender,
|
prerender,
|
||||||
} from 'nitropack'
|
} from 'nitropack'
|
||||||
import { resolve } from 'pathe'
|
import { resolve } from 'pathe'
|
||||||
import { loadConfig } from 'c12'
|
|
||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import { dirname } from 'pathe'
|
import { dirname } from 'pathe'
|
||||||
import { nitroConfig as mcflyNitroConfig } from '../../runtime/nitro-config.js'
|
import { getNitroConfig } from '../../get-nitro-config.js'
|
||||||
|
|
||||||
const hmrKeyRe = /^runtimeConfig\.|routeRules\./
|
const hmrKeyRe = /^runtimeConfig\.|routeRules\./
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
|
@ -42,8 +41,6 @@ async function serve(args) {
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
const rootDir = resolve(args.dir || args._dir || '.')
|
const rootDir = resolve(args.dir || args._dir || '.')
|
||||||
const { config: mcflyConfig } = await loadConfig({ name: 'mcfly' })
|
|
||||||
const { config: nitroConfig } = await loadConfig({ name: 'nitro' })
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('nitropack').Nitro} Nitro
|
* @typedef {import('nitropack').Nitro} Nitro
|
||||||
|
@ -60,6 +57,8 @@ async function serve(args) {
|
||||||
await nitro.close()
|
await nitro.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nitroConfig = await getNitroConfig()
|
||||||
|
|
||||||
// create new nitro
|
// create new nitro
|
||||||
nitro = await createNitro(
|
nitro = await createNitro(
|
||||||
{
|
{
|
||||||
|
@ -67,10 +66,7 @@ async function serve(args) {
|
||||||
dev: true,
|
dev: true,
|
||||||
preset: 'nitro-dev',
|
preset: 'nitro-dev',
|
||||||
_cli: { command: 'dev' },
|
_cli: { command: 'dev' },
|
||||||
// spread mcfly.nitro config
|
...nitroConfig,
|
||||||
...(mcflyConfig.nitro ?? {}),
|
|
||||||
...(nitroConfig ?? {}),
|
|
||||||
...mcflyNitroConfig,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
watch: true,
|
watch: true,
|
||||||
|
@ -95,7 +91,6 @@ async function serve(args) {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
nitro.hooks.hookOnce('restart', reload)
|
nitro.hooks.hookOnce('restart', reload)
|
||||||
nitro.options.runtimeConfig.mcfly = mcflyConfig
|
|
||||||
|
|
||||||
nitro.options.handlers.push({
|
nitro.options.handlers.push({
|
||||||
middleware: true,
|
middleware: true,
|
||||||
|
|
30
packages/core/get-nitro-config.js
Normal file
30
packages/core/get-nitro-config.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import { loadConfig } from 'c12'
|
||||||
|
import { mcflyNitroConfig } from './mcfly-nitro-config.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {import('nitropack').NitroConfig} NitroConfig
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Promise<NitroConfig>}
|
||||||
|
*/
|
||||||
|
export async function getNitroConfig() {
|
||||||
|
const { config: nitroConfig } = await loadConfig({ name: 'nitro' })
|
||||||
|
const { config: mcflyConfig } = await loadConfig({ name: 'mcfly' })
|
||||||
|
|
||||||
|
return {
|
||||||
|
// nitro config within user's mcfly.config.mjs
|
||||||
|
...(mcflyConfig.nitro ?? {}),
|
||||||
|
|
||||||
|
// nitro config from nitro.config.mjs
|
||||||
|
...(nitroConfig ?? {}),
|
||||||
|
|
||||||
|
// McFly standard nitro config
|
||||||
|
...mcflyNitroConfig,
|
||||||
|
|
||||||
|
// expose mcfly config to framework runtime
|
||||||
|
runtimeConfig: {
|
||||||
|
mcfly: mcflyConfig,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
* @typedef {import('nitropack').NitroConfig} NitroConfig
|
* @typedef {import('nitropack').NitroConfig} NitroConfig
|
||||||
* @type {NitroConfig}
|
* @type {NitroConfig}
|
||||||
*/
|
*/
|
||||||
export const nitroConfig = {
|
export const mcflyNitroConfig = {
|
||||||
framework: {
|
framework: {
|
||||||
name: 'McFly',
|
name: 'McFly',
|
||||||
},
|
},
|
|
@ -10,8 +10,7 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"version": "npm version",
|
"version": "npm version",
|
||||||
"publish": "npm publish",
|
"publish": "npm publish",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
"build:middleware": "npx esbuild route-middleware.js --bundle --outfile=mcfly-middleware.js"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { eventHandler } from 'h3'
|
||||||
import { useRuntimeConfig, useStorage } from 'nitropack/runtime'
|
import { useRuntimeConfig, useStorage } from 'nitropack/runtime'
|
||||||
import { createHooks } from 'hookable'
|
import { createHooks } from 'hookable'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
|
import { colorize } from 'consola/utils'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
hooks as mcflyHooks,
|
hooks as mcflyHooks,
|
||||||
|
@ -9,7 +10,7 @@ import {
|
||||||
evaluateServerScripts,
|
evaluateServerScripts,
|
||||||
injectHtmlFragments,
|
injectHtmlFragments,
|
||||||
injectCustomElements,
|
injectCustomElements,
|
||||||
} from '@mcflyjs/core/runtime/index.js'
|
} from '@mcflyjs/core/runtime/index.js' // important to import from installed node_module because this script is passed to another context
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../config').McFlyConfig} Config
|
* @typedef {import('../config').McFlyConfig} Config
|
||||||
|
@ -29,18 +30,17 @@ export default eventHandler(async (event) => {
|
||||||
let { mcfly: config } = useRuntimeConfig()
|
let { mcfly: config } = useRuntimeConfig()
|
||||||
const storage = useStorage()
|
const storage = useStorage()
|
||||||
|
|
||||||
const publicAssets = (await storage.getKeys('root:public')).map(
|
|
||||||
(key) => `/${key.replace('root:public:', '')}`
|
|
||||||
)
|
|
||||||
|
|
||||||
// if not page, don't render
|
// if not page, don't render
|
||||||
if (event.path.startsWith('/api') || publicAssets.includes(event.path)) {
|
if (event.path.startsWith('/api')) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config || Object.keys(config).length === 0) {
|
if (!config || Object.keys(config).length === 0) {
|
||||||
config = defaultMcflyConfig
|
config = defaultMcflyConfig
|
||||||
consola.warn(`[WARN]: McFly configuration not loaded, using defaults...`)
|
consola.warn(
|
||||||
|
`[WARN]: McFly configuration not found, using defaults...`,
|
||||||
|
defaultMcflyConfig
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const plugins = config.plugins ?? []
|
const plugins = config.plugins ?? []
|
||||||
|
@ -96,11 +96,11 @@ export default eventHandler(async (event) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const timeEnd = performance.now()
|
const timeEnd = performance.now()
|
||||||
consola.info(
|
consola.log(
|
||||||
'Page rendered in',
|
colorize('green', event.path),
|
||||||
|
'rendered in',
|
||||||
Math.round(timeEnd - timeStart),
|
Math.round(timeEnd - timeStart),
|
||||||
'ms:',
|
'ms'
|
||||||
event.path
|
|
||||||
)
|
)
|
||||||
return (
|
return (
|
||||||
html ??
|
html ??
|
||||||
|
|
|
@ -4,4 +4,4 @@ export { getFiles } from './get-files.mjs'
|
||||||
export { hooks } from './hooks.mjs'
|
export { hooks } from './hooks.mjs'
|
||||||
export { injectCustomElements } from './inject-elements.mjs'
|
export { injectCustomElements } from './inject-elements.mjs'
|
||||||
export { injectHtmlFragments } from './inject-fragments.mjs'
|
export { injectHtmlFragments } from './inject-fragments.mjs'
|
||||||
export { nitroConfig } from './nitro-config.js'
|
export { mcflyNitroConfig as nitroConfig } from '../mcfly-nitro-config.js'
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { ELEMENT_NODE, parse, render, walkSync } from 'ultrahtml'
|
import { ELEMENT_NODE, parse, render, walkSync } from 'ultrahtml'
|
||||||
import { getFiles } from '@mcflyjs/core/runtime/get-files.mjs'
|
import { getFiles } from './get-files.mjs'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../../config/index.js').McFlyConfig} Config
|
* @typedef {import('../../config/index.js').McFlyConfig} Config
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { ELEMENT_NODE, parse, render, walkSync } from 'ultrahtml'
|
import { ELEMENT_NODE, parse, render, walkSync } from 'ultrahtml'
|
||||||
import { getFiles } from '@mcflyjs/core/runtime/get-files.mjs'
|
import { getFiles } from './get-files.mjs'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('ultrahtml').Node} HtmlNode
|
* @typedef {import('ultrahtml').Node} HtmlNode
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
"dev": "mcfly serve",
|
"dev": "mcfly serve",
|
||||||
"build": "mcfly build",
|
"build": "mcfly build",
|
||||||
"preview": "node .output/server/index.mjs",
|
"preview": "node .output/server/index.mjs",
|
||||||
"build:preview": "pnpm run build && pnpm run preview",
|
"build:preview": "pnpm run build --preset=node-server && pnpm run preview",
|
||||||
"deploy": "netlify deploy --site=$NETLIFY_SITE_ID --dir=dist --prod"
|
"deploy": "netlify deploy --site=$NETLIFY_SITE_ID --dir=dist --prod"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
Loading…
Reference in a new issue