Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: client introduce page #18

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/core/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>turbo console</title>
<title>Launch Editor</title>
</head>
<style>
#error-info {
color: red;
font-size: 2rem;
}

body {
background-color: white;
}

@media (prefers-color-scheme: dark) {
body {
background-color: black;
}
}
</style>
<body>
<div id="error-info"></div>
Expand Down
33 changes: 33 additions & 0 deletions src/core/client/intro.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Intro</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
</head>

<body>
<main class="container">
<h2>Unplugin Turbo Console</h2>
<article>
<p>🎉 Congratulations! Launch edtior server is running ...</p>
<p>You can click the links below to test it:(if the file exists)</p>
<a href="/client#c3JjL21haW4uanM=" target="_blank">src/main.js</a>
<br>
<a href="/client#c3JjL21haW4udHM=" target="_blank">src/main.ts</a>
<br>
<a href="/client#LmdpdGlnbm9yZQ==" target="_blank">.gitignore</a>
<br>
<a href="/client#UkVBRE1FLm1k" target="_blank">README.md</a>
<br>
<a href="/client#cGFja2FnZS5qc29u" target="_blank">package.json</a>
</article>
<footer>
<a href="https://github.com/unplguin/unplugin-turbo-console" class="secondary">Star On GitHub</a>
</footer>
</main>
</body>

</html>
1 change: 1 addition & 0 deletions src/core/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const DIR_DIST = typeof __dirname !== 'undefined'
: dirname(fileURLToPath(import.meta.url))

export const DIR_CLIENT = resolve(DIR_DIST, './client/index.html')
export const DIR_INTRO = resolve(DIR_DIST, './client/intro.html')
4 changes: 2 additions & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import serveStatic from 'serve-static'

// @ts-expect-error any
import launch from 'launch-editor'
import { DIR_CLIENT } from '../dir'
import { DIR_CLIENT, DIR_INTRO } from '../dir'

export async function startServer(port: number = 3070) {
try {
Expand All @@ -16,7 +16,7 @@ export async function startServer(port: number = 3070) {
const app = createApp()

app.use('/client', fromNodeMiddleware(serveStatic(DIR_CLIENT)))

app.use('/intro', fromNodeMiddleware(serveStatic(DIR_INTRO)))
app.use('/health', eventHandler(() => {
return {
message: 'ok',
Expand Down
5 changes: 5 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ export const getEnforce: Record<Framework, 'pre' | 'post'> = {
esbuild: 'post',
rspack: 'pre',
}

export function printInfo(port: number) {
// eslint-disable-next-line no-console
console.log('\x1B[32m%s\x1B[0m\x1B[1m%s\x1B[0m\x1B[36m%s\x1B[0m', ' ➜', ` TurboConsole:`, ` http://localhost:${port}/intro`)
}
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Context, Options } from './types'
import { DETAULT_OPTIONS, PLUGIN_NAME } from './core/constants'
import { startServer } from './core/server/index'
import { transformer } from './core/transform/transformer'
import { filter, getEnforce } from './core/utils'
import { filter, getEnforce, printInfo } from './core/utils'

export const unpluginFactory: UnpluginFactory<Options | undefined> = (options = {}, meta) => {
const mergedOptions = {
Expand Down Expand Up @@ -40,12 +40,18 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
},
vite: {
apply: 'serve',
async configureServer() {
async configureServer(server) {
if (options.disableLaunchEditor)
return

await detectPort()

const _print = server.printUrls
server.printUrls = () => {
_print()
printInfo(mergedOptions.port!)
}

startServer(mergedOptions.port)
},
},
Expand All @@ -59,6 +65,8 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
compiler.hooks.done.tap(PLUGIN_NAME, (state) => {
if (state.hasErrors())
return

printInfo(mergedOptions.port!)
startServer(mergedOptions.port)
})
}
Expand All @@ -74,6 +82,7 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
if (state.hasErrors())
return

printInfo(mergedOptions.port!)
startServer(mergedOptions.port)
})
}
Expand Down
6 changes: 3 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Options } from 'tsup'
import { defineConfig } from 'tsup'

export default <Options>{
export default defineConfig({
entryPoints: [
'src/*.ts',
],
Expand All @@ -10,4 +10,4 @@ export default <Options>{
splitting: true,
onSuccess: 'npm run build:fix',
shims: true,
}
})
Loading