mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
feat: nitro endpoint for viewing _vfs
(#362)
This commit is contained in:
parent
7fee983b52
commit
5d5516dd1d
@ -4,7 +4,7 @@ import { loading as loadingTemplate } from '@nuxt/design'
|
|||||||
import chokidar, { FSWatcher } from 'chokidar'
|
import chokidar, { FSWatcher } from 'chokidar'
|
||||||
import debounce from 'debounce'
|
import debounce from 'debounce'
|
||||||
import { stat } from 'fs-extra'
|
import { stat } from 'fs-extra'
|
||||||
import { createApp, Middleware } from 'h3'
|
import { createApp, Middleware, useBase } from 'h3'
|
||||||
import { createProxy } from 'http-proxy'
|
import { createProxy } from 'http-proxy'
|
||||||
import { listen, Listener, ListenOptions } from 'listhen'
|
import { listen, Listener, ListenOptions } from 'listhen'
|
||||||
import servePlaceholder from 'serve-placeholder'
|
import servePlaceholder from 'serve-placeholder'
|
||||||
@ -12,6 +12,7 @@ import serveStatic from 'serve-static'
|
|||||||
import { resolve } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import type { Server } from 'connect'
|
import type { Server } from 'connect'
|
||||||
import type { NitroContext } from '../context'
|
import type { NitroContext } from '../context'
|
||||||
|
import { handleVfs } from './vfs'
|
||||||
|
|
||||||
export function createDevServer (nitroContext: NitroContext) {
|
export function createDevServer (nitroContext: NitroContext) {
|
||||||
// Worker
|
// Worker
|
||||||
@ -57,6 +58,9 @@ export function createDevServer (nitroContext: NitroContext) {
|
|||||||
app.use(nitroContext._nuxt.publicPath, serveStatic(resolve(nitroContext._nuxt.buildDir, 'dist/client')))
|
app.use(nitroContext._nuxt.publicPath, serveStatic(resolve(nitroContext._nuxt.buildDir, 'dist/client')))
|
||||||
app.use(nitroContext._nuxt.routerBase, serveStatic(resolve(nitroContext._nuxt.publicDir)))
|
app.use(nitroContext._nuxt.routerBase, serveStatic(resolve(nitroContext._nuxt.publicDir)))
|
||||||
|
|
||||||
|
// debugging endpoint to view vfs
|
||||||
|
app.use('/_vfs', useBase('/_vfs', handleVfs(nitroContext)))
|
||||||
|
|
||||||
// Dynamic Middlwware
|
// Dynamic Middlwware
|
||||||
const legacyMiddleware = createDynamicMiddleware()
|
const legacyMiddleware = createDynamicMiddleware()
|
||||||
const devMiddleware = createDynamicMiddleware()
|
const devMiddleware = createDynamicMiddleware()
|
||||||
|
53
packages/nitro/src/server/vfs.ts
Normal file
53
packages/nitro/src/server/vfs.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { createError, Handle } from 'h3'
|
||||||
|
import { NitroContext } from '..'
|
||||||
|
|
||||||
|
export function handleVfs (ctx: NitroContext): Handle {
|
||||||
|
return (req) => {
|
||||||
|
if (req.url === '/') {
|
||||||
|
return '<!doctype html><html><body><ul>' + Object.keys(ctx.vfs).map((key) => {
|
||||||
|
const url = encodeURIComponent(key)
|
||||||
|
return `<li><a href="/_vfs/${url}">${key.replace(ctx._nuxt.rootDir, '')}</a></li>`
|
||||||
|
}).join('\n') + '</ul></body></html>'
|
||||||
|
}
|
||||||
|
const param = decodeURIComponent(req.url.slice(1))
|
||||||
|
if (param in ctx.vfs) {
|
||||||
|
return editorTemplate({
|
||||||
|
readOnly: true,
|
||||||
|
language: param.endsWith('html') ? 'html' : 'javascript',
|
||||||
|
theme: 'vs-dark',
|
||||||
|
value: ctx.vfs[param]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return createError({ message: 'File not found', statusCode: 404 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const monacoVersion = '0.20.0'
|
||||||
|
const monacoUrl = `https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/${monacoVersion}/min`
|
||||||
|
const vsUrl = `${monacoUrl}/vs`
|
||||||
|
|
||||||
|
const editorTemplate = (options: Record<string, any>) => `
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" data-name="vs/editor/editor.main" href="${vsUrl}/editor/editor.main.min.css">
|
||||||
|
</head>
|
||||||
|
<body style="margin: 0">
|
||||||
|
<div id="editor" style="height:100vh"></div>
|
||||||
|
<script src="${vsUrl}/loader.min.js"></script>
|
||||||
|
<script>
|
||||||
|
require.config({ paths: { vs: '${vsUrl}' } })
|
||||||
|
|
||||||
|
const proxy = URL.createObjectURL(new Blob([\`
|
||||||
|
self.MonacoEnvironment = { baseUrl: '${monacoUrl}' }
|
||||||
|
importScripts('${vsUrl}/base/worker/workerMain.min.js')
|
||||||
|
\`], { type: 'text/javascript' }))
|
||||||
|
window.MonacoEnvironment = { getWorkerUrl: () => proxy }
|
||||||
|
|
||||||
|
require(['vs/editor/editor.main'], function () {
|
||||||
|
monaco.editor.create(document.getElementById('editor'), ${JSON.stringify(options)})
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
Loading…
Reference in New Issue
Block a user