Skip to content

Commit

Permalink
refactor(vite-node): move utils
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 22, 2022
1 parent 4d9e7cc commit 9cdc504
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
15 changes: 1 addition & 14 deletions packages/vite-node/src/server.ts
@@ -1,13 +1,10 @@
import type { TransformResult, ViteDevServer } from 'vite'
import { shouldExternalize } from './externalize'
import type { ViteNodeResolveId, ViteNodeServerOptions } from './types'
import { toFilePath } from './utils'
import { toFilePath, withInlineSourcemap } from './utils'

export * from './externalize'

let SOURCEMAPPING_URL = 'sourceMa'
SOURCEMAPPING_URL += 'ppingURL'

export class ViteNodeServer {
promiseMap = new Map<string, Promise<TransformResult | null | undefined>>()

Expand Down Expand Up @@ -83,13 +80,3 @@ export class ViteNodeServer {
}
}

export async function withInlineSourcemap(result: TransformResult) {
const { code, map } = result

if (code.includes(`${SOURCEMAPPING_URL}=`))
return result
if (map)
result.code = `${code}\n\n//# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')}\n`

return result
}
16 changes: 16 additions & 0 deletions packages/vite-node/src/utils.ts
@@ -1,5 +1,6 @@
import { fileURLToPath, pathToFileURL } from 'url'
import { dirname, resolve } from 'pathe'
import type { TransformResult } from 'vite'

export const isWindows = process.platform === 'win32'

Expand Down Expand Up @@ -41,3 +42,18 @@ export function toFilePath(id: string, root: string): string {
? fileURLToPath(pathToFileURL(absolute.slice(1)).href)
: absolute
}


let SOURCEMAPPING_URL = 'sourceMa'
SOURCEMAPPING_URL += 'ppingURL'

export async function withInlineSourcemap(result: TransformResult) {
const { code, map } = result

if (code.includes(`${SOURCEMAPPING_URL}=`))
return result
if (map)
result.code = `${code}\n\n//# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')}\n`

return result
}

0 comments on commit 9cdc504

Please sign in to comment.