fix(core): catch undefined plugins

This commit is contained in:
Ayo Ayco 2025-01-08 21:32:55 +01:00
parent 73617647db
commit 014deadf75
5 changed files with 4 additions and 182 deletions

View file

@ -1,130 +0,0 @@
import { consola } from 'consola'
import { eventHandler } from 'h3'
import { createHooks } from 'hookable'
import { hooks as mcflyHooks } from './hooks.mjs'
import { evaluateServerScripts } from './evaluate-scripts.mjs'
import { injectHtmlFragments } from './inject-fragments.mjs'
import { injectCustomElements } from './inject-elements.mjs'
import defaultMcflyConfig from './default-mcfly-config.mjs'
import { loadConfig } from 'c12'
/**
* @typedef {import('../config').McFlyConfig} Config
* @typedef {import('unstorage').Storage} Storage
* @typedef {import('unstorage').StorageValue} StorageValue
* @typedef {import('h3').EventHandler} EventHandler
*/
/**
* Intercepts all routes and assembles the correct HTML to return
* @deprecated
* @param {{
* storage: Storage
* }} param0
* @returns {EventHandler}
*/
export function useMcFlyRoute({ storage }) {
return eventHandler(async (event) => {
const hooks = createHooks()
Object.keys(mcflyHooks).forEach((hookName) => hooks.addHooks(hookName))
const { path } = event
let { config } = await loadConfig({ name: 'mcfly' })
if (!config || Object.keys(config).length === 0) {
config = defaultMcflyConfig
consola.warn(`[WARN]: McFly configuration not loaded, using defaults...`)
}
const plugins = config.plugins
plugins.forEach((plugin) => {
const pluginHooks = Object.keys(plugin)
pluginHooks.forEach((pluginHook) => {
hooks.hook(pluginHook, plugin[pluginHook])
})
})
const { components: componentType } = config
let html = await getHtml(path, storage)
if (html) {
const transforms = [
{
fn: evaluateServerScripts,
args: [''],
hook: mcflyHooks.serverScriptsEvaluated,
},
{
fn: injectHtmlFragments,
args: [storage],
hook: mcflyHooks.fragmentsInjected,
},
{
fn: injectCustomElements,
args: [componentType, storage],
hook: mcflyHooks.customElementsInjected,
},
]
if (!!componentType && !!html) {
for (const transform of transforms) {
html = await transform.fn(html.toString(), ...transform.args)
// call hook
if (transform.hook) {
hooks.callHook(transform.hook)
}
}
} else {
consola.error('[ERR]: Failed to insert registry', {
componentType: !componentType ? 'missing' : 'okay',
html: !html ? 'missing' : 'okay',
})
}
}
if (html) {
hooks.callHook(mcflyHooks.pageRendered)
}
return (
html ??
new Response(
'😱 ERROR 404: Not found. You can put a 404.html on the ./src/pages directory to customize this error page.',
{ status: 404 }
)
)
})
}
function getPurePath(path) {
return path.split('?')[0]
}
/**
* Gets the correct HTML depending on the path requested
* @param {string} path
* @param {Storage} storage
* @returns {Promise<StorageValue>}
*/
async function getHtml(path, storage) {
const purePath = getPurePath(path)
const rawPath =
purePath[purePath.length - 1] === '/' ? purePath.slice(0, -1) : purePath
const filename = rawPath === '' ? '/index.html' : `${rawPath}.html`
const fallback = getPath(rawPath + '/index.html')
const filePath = getPath(filename)
let html = await storage.getItem(filePath)
if (!html) html = await storage.getItem(fallback)
if (!html) html = await storage.getItem(getPath('/404.html'))
return html
}
/**
* Gets the storage path for a file
* @param {string} filename
* @returns {string}
*/
function getPath(filename) {
return `assets:pages${filename}`
}

View file

@ -1 +0,0 @@
export { useMcFlyRoute } from './event-handler.js'

View file

@ -1,6 +1,6 @@
{
"name": "@mcflyjs/core",
"version": "0.7.0",
"version": "0.7.1",
"description": "McFly core package",
"type": "module",
"main": "index.js",

View file

@ -31,7 +31,7 @@ export default eventHandler(async (event) => {
consola.warn(`[WARN]: McFly configuration not loaded, using defaults...`)
}
const plugins = config.plugins
const plugins = config.plugins ?? []
plugins.forEach((plugin) => {
const pluginHooks = Object.keys(plugin)

View file

@ -121,15 +121,9 @@ importers:
'@mcflyjs/cli':
specifier: ^0.1.1
version: 0.1.1(magicast@0.3.5)
'@mcflyjs/config':
specifier: ^0.2.1
version: link:../../packages/config
'@mcflyjs/core':
specifier: ^0.6.1
version: 0.6.1(magicast@0.3.5)(typescript@5.7.2)
nitropack:
specifier: ~2.10.4
version: 2.10.4(typescript@5.7.2)
specifier: ^0.7.1
version: link:../../packages/core
packages:
@ -607,9 +601,6 @@ packages:
resolution: {integrity: sha512-YT0XVwbUO9SscfoCrNY1Tz92SwLrCKbvHYrPUWjC8UAVaaX8tekdfRDF7DiIr8PX5me3YIZUBnQw0/loFNPhvQ==}
hasBin: true
'@mcflyjs/core@0.6.1':
resolution: {integrity: sha512-ozgpIRpNix7gyuFlB8ofEm+iENMvxQsgyzxMTbIrOM69QYiEV1GsjxWyUqH3QuCwA4UB1KHdyWS+PNaCWSepOQ==}
'@netlify/functions@2.8.2':
resolution: {integrity: sha512-DeoAQh8LuNPvBE4qsKlezjKj0PyXDryOFJfJKo3Z1qZLKzQ21sT314KQKPVjfvw6knqijj+IO+0kHXy/TJiqNA==}
engines: {node: '>=14.0.0'}
@ -3126,44 +3117,6 @@ snapshots:
transitivePeerDependencies:
- magicast
'@mcflyjs/core@0.6.1(magicast@0.3.5)(typescript@5.7.2)':
dependencies:
c12: 2.0.1(magicast@0.3.5)
esbuild: 0.24.2
esprima: 4.0.1
h3: 1.13.0
nitropack: 2.10.4(typescript@5.7.2)
ultrahtml: 1.5.3
transitivePeerDependencies:
- '@azure/app-configuration'
- '@azure/cosmos'
- '@azure/data-tables'
- '@azure/identity'
- '@azure/keyvault-secrets'
- '@azure/storage-blob'
- '@capacitor/preferences'
- '@deno/kv'
- '@electric-sql/pglite'
- '@libsql/client'
- '@netlify/blobs'
- '@planetscale/database'
- '@upstash/redis'
- '@vercel/blob'
- '@vercel/kv'
- aws4fetch
- better-sqlite3
- drizzle-orm
- encoding
- idb-keyval
- magicast
- mysql2
- rolldown
- supports-color
- typescript
- uWebSockets.js
- uploadthing
- xml2js
'@netlify/functions@2.8.2':
dependencies:
'@netlify/serverless-functions-api': 1.26.1