Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
fix: windows errors, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Dec 16, 2021
1 parent 20f8dbf commit 34826cb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/peeky-config/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import { join } from 'path'
import consola from 'consola'
import shortid from 'shortid'
import { fixWindowsAbsoluteFileUrl } from '@peeky/utils'
import type { PeekyConfig } from './types'
import { setupConfigContentLoader } from './fs.js'
import { transformConfigCode } from './transform.js'
Expand Down Expand Up @@ -29,7 +30,7 @@ export async function setupConfigLoader (options: PeekyConfigLoaderOptions = {})
fs.writeFileSync(resolvedPath, result.code)
config = (
// eslint-disable-next-line no-eval
await eval(`import('${resolvedPath}')`)
await eval(`import('${fixWindowsAbsoluteFileUrl(resolvedPath)}')`)
).default
fs.unlinkSync(resolvedPath)
}
Expand Down
15 changes: 9 additions & 6 deletions packages/peeky-runner/src/runtime/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import { resolve, dirname, relative } from 'path'
import { builtinModules, createRequire } from 'module'
import vm from 'vm'
import { pathToFileURL } from 'url'
import { fileURLToPath, pathToFileURL } from 'url'
import { ViteDevServer, InlineConfig, createServer, mergeConfig } from 'vite'
import chalk from 'chalk'
import shortid from 'shortid'
import isEqual from 'lodash/isEqual.js'
import { isValidNodeImport } from 'mlly'
import type { ModuleFilterOption, ModuleFilter } from '@peeky/config'
import { slash } from '@peeky/utils'
import { slash, isWindows, fixWindowsAbsoluteFileUrl } from '@peeky/utils'
import { moduleCache, sourceMaps } from './module-cache.js'
import { mockedModules } from './mocked-files.js'
import { createPeekyGlobal } from './peeky-global/index.js'
Expand Down Expand Up @@ -162,12 +162,12 @@ async function cachedRequest (rawId: string, ctx: ExecutionContext): Promise<any
}

if (ctx.externalsCache.has(realPath)) {
return import(realPath)
return import(fixWindowsAbsoluteFileUrl(realPath))
}

try {
if (shouldExternalize(realPath) && (await isValidNodeImport(realPath))) {
const exports = await import(realPath)
const exports = await import(fixWindowsAbsoluteFileUrl(realPath))
ctx.externalsCache.add(realPath)
return exports
}
Expand Down Expand Up @@ -239,7 +239,7 @@ async function rawRequest (id: string, realPath: string, ctx: ExecutionContext):
sourceMaps.set(realPath, result.map)
}

const url = pathToFileURL(realPath)
const url = pathToFileURL(realPath).href

const exports = {}

Expand Down Expand Up @@ -301,7 +301,10 @@ function toFilePath (id: string, root: string): string {

if (absolute.startsWith('//')) { absolute = absolute.slice(1) }

return absolute
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
return isWindows && absolute.startsWith('/')
? fileURLToPath(pathToFileURL(absolute.slice(1)).href)
: absolute
}

function exportAll (exports: any, sourceModule: any) {
Expand Down
1 change: 1 addition & 0 deletions packages/peeky-utils/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isWindows = process.platform === 'win32'
8 changes: 8 additions & 0 deletions packages/peeky-utils/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function slash (path: string) {
return path.replace(/\\/g, '/')
}

export function fixWindowsAbsoluteFileUrl (path: string) {
if (path.match(/^\w:\\/)) {
return `file:///${slash(path)}`
} else {
return path
}
}
1 change: 1 addition & 0 deletions packages/peeky-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './code.js'
export * from './env.js'
export * from './esbuild.js'
export * from './format.js'
export * from './fs.js'
Expand Down

0 comments on commit 34826cb

Please sign in to comment.