Skip to content

Commit b44104c

Browse files
committed
feat: route map
1 parent f6c7e9a commit b44104c

File tree

11 files changed

+147
-34
lines changed

11 files changed

+147
-34
lines changed

examples/vite-vue3/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
<script setup lang="ts">
66
const msg: string = "Hello World"
7-
console.log(msg) // turbo-console-disable-line
7+
console.log(msg)
88
</script>

examples/vite-vue3/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import './assets/main.css'
22
import { createApp } from 'vue'
33
import App from './App.vue'
4-
console.log('main.ts') // turbo-console-disable-line
4+
console.log('main.ts')
5+
6+
console.log('main.ts--22')
57
createApp(App).mount('#app')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"@nuxt/schema": "^3.11.2",
159159
"@types/fs-extra": "^11.0.4",
160160
"@types/node": "^20.12.7",
161+
"@vitest/ui": "^1.5.0",
161162
"bumpp": "^9.4.0",
162163
"chalk": "^5.3.0",
163164
"eslint": "^9.0.0",

pnpm-lock.yaml

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/client/index.html

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
8+
<link rel="icon" type="image/svg+xml" href="https://unplugin.unjs.io/logo.svg">
69
<title>Launch Editor</title>
710
</head>
811
<style>
9-
#error-info {
10-
color: red;
11-
font-size: 2rem;
12-
}
13-
14-
body {
15-
background-color: white;
16-
}
17-
1812
@media (prefers-color-scheme: dark) {
1913
body {
2014
background-color: black;
2115
}
2216
}
2317
</style>
18+
2419
<body>
25-
<div id="error-info"></div>
20+
<main class="container">
21+
<article>
22+
<h4>Ops, It get some error when launch to the editor:</h4>
23+
<div id="error-info" style="color: #EE402E">
24+
25+
</div>
26+
<small>You can report this issus at <a href="https://github.com/unplugin/unplugin-turbo-console/issues"
27+
target="_blank">GitHub</a>.
28+
</small>
29+
</article>
30+
</main>
2631
</body>
2732
<script>
28-
(async () => {
29-
try {
30-
const hash = window.location.hash
31-
const decodeBase64 = window.atob(hash.slice(1))
32-
await fetch(`/__open-in-editor?file=${encodeURIComponent(decodeBase64)}`)
33-
window.close()
34-
}
35-
catch (error) {
36-
const errorInfo = document.getElementById('error-info')
37-
errorInfo.innerText = String(error)
38-
}
39-
})()
33+
; (async () => {
34+
try {
35+
const position = window.location.hash.slice(1)
36+
const raw = await fetch(`/__open-in-editor?position=${(position)}`)
37+
const response = await raw.json()
38+
if (response.status === 'error')
39+
throw new Error(response.message)
40+
41+
window.close()
42+
}
43+
catch (error) {
44+
const errorInfo = document.getElementById('error-info')
45+
errorInfo.innerText = String(error)
46+
}
47+
})()
4048
</script>
49+
4150
</html>

src/core/server/index.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,42 @@ export async function startServer(port: number = 3070) {
2121
}
2222
}))
2323

24+
app.use('/routeMap', eventHandler(() => {
25+
const routeMap = globalThis.TurboConsoleRouteMap || new Map()
26+
return Object.fromEntries(routeMap)
27+
}))
28+
2429
app.use('/__open-in-editor', eventHandler(async (event) => {
2530
try {
26-
const { file } = getQuery(event) as { file: string }
27-
launch(resolve(cwd(), file))
31+
const { position } = getQuery(event) as { position: string }
32+
const routeMap = globalThis.TurboConsoleRouteMap
33+
34+
if (!routeMap)
35+
throw new Error('routeMap is undefined')
36+
37+
const parsed = position.split(',')
38+
const routeMapString = parsed[0]
39+
const line = parsed[1] || 1
40+
const column = parsed[2] || 1
41+
42+
let filePath = ''
43+
44+
for (const [key, value] of routeMap) {
45+
if (value === routeMapString) {
46+
filePath = key
47+
break
48+
}
49+
}
50+
51+
launch(resolve(cwd(), `${filePath}:${line}:${column}`))
2852
return {
29-
message: 'ok',
53+
status: 'success',
3054
}
3155
}
3256
catch (error) {
3357
return {
34-
error: String(error),
58+
status: 'error',
59+
message: String(error),
3560
}
3661
}
3762
}))

src/core/transform/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export async function transform(context: Context) {
1515
if (!compiler) {
1616
return {
1717
code: magicString.toString(),
18-
map: magicString.generateMap({ source: id }),
18+
map: magicString.generateMap({
19+
source: id,
20+
file: id,
21+
includeContent: true,
22+
hires: true,
23+
}),
1924
}
2025
}
2126

src/core/utils.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ export function isPluginDisable(meta: {
114114
return false
115115
}
116116

117+
export function setRouteMap(filePath: string): string {
118+
let routeMap = globalThis.TurboConsoleRouteMap
119+
120+
if (typeof routeMap === 'undefined')
121+
routeMap = new Map<string, string>()
122+
123+
if (routeMap.has(filePath))
124+
return routeMap.get(filePath)!
125+
126+
function getRandomString() {
127+
const randomString = Math.random().toString(20).substring(2, 6)
128+
129+
for (const [_, value] of routeMap) {
130+
if (value === randomString)
131+
return getRandomString()
132+
}
133+
134+
return randomString
135+
}
136+
137+
const randomString = getRandomString()
138+
routeMap.set(filePath, randomString)
139+
globalThis.TurboConsoleRouteMap = routeMap
140+
141+
return randomString
142+
}
143+
117144
export function genConsoleString(genContext: GenContext) {
118145
const { options, originalColumn, originalLine, argType, id } = genContext
119146
let { argsName } = genContext
@@ -126,6 +153,9 @@ export function genConsoleString(genContext: GenContext) {
126153
const fileName = getExtendedPath(filePath, extendedPathFileNames)
127154
const fileType = extname(filePath)
128155

156+
const relativePath = relative(cwd(), filePath)
157+
const routeMapString = setRouteMap(relativePath)
158+
129159
// Parsing escaped unicode symbols
130160
try {
131161
argsName = JSON.parse(`"${argsName}"`)
@@ -139,9 +169,9 @@ export function genConsoleString(genContext: GenContext) {
139169

140170
// not output when argtype is string or number
141171
const lineInfo = `%c🚀 ${fileName}\u00B7${originalLine}${['StringLiteral', 'NumericLiteral'].includes(argType) ? '' : ` ~ ${argsName}`}`
142-
const codePosition = `${relative(cwd(), filePath)}:${originalLine}:${(originalColumn || 0) + 1}`
172+
const codePosition = `${routeMapString},${originalLine},${(originalColumn || 0) + 1}`
143173

144-
const launchEditorString = `%c🔦 http://localhost:${port}#${Buffer.from(codePosition, 'utf-8').toString('base64')}`
174+
const launchEditorString = `%c🔦 http://localhost:${port}#${codePosition}`
145175

146176
let consoleString = ''
147177

src/globals.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable no-var */
2+
/* eslint-disable vars-on-top */
3+
export interface global {}
4+
5+
declare global {
6+
var TurboConsoleRouteMap: Map<string, string>
7+
}

test/routeMap.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { setRouteMap } from '../src/core/utils'
3+
4+
describe('route map', () => {
5+
it('should work', () => {
6+
setRouteMap('src/app.vue')
7+
8+
expect(globalThis.TurboConsoleRouteMap instanceof Map).toMatchInlineSnapshot(`true`)
9+
})
10+
})

0 commit comments

Comments
 (0)