Skip to content

Commit 3efa28e

Browse files
fix: use fileURLToPath for correct path resolution on Windows (#137)
* feat: support @vitejs/plugin-basic-ssl * fix: try to fix windows path resolution * fix: remove win32 hack which now breaks things * fix: use fileURLToPath for correct path resolution on Windows
1 parent f5cb1eb commit 3efa28e

10 files changed

Lines changed: 83 additions & 57 deletions

File tree

.changeset/old-needles-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vitrify': patch
3+
---
4+
5+
fix: use fileURLToPath for correct path resolution on Windows

packages/create-vitrify/templates/vite-ui-plugin/src/vite-plugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Plugin } from 'vite'
22
import { promises } from 'fs'
3+
import { fileURLToPath } from 'url'
34
const { readFile } = promises
45

56
export default async function ({
@@ -9,7 +10,7 @@ export default async function ({
910
} = {}): Promise<Plugin> {
1011
const pkgJson = JSON.parse(
1112
await readFile(
12-
new URL('../package.json', import.meta.url).pathname,
13+
fileURLToPath(new URL('../package.json', import.meta.url)),
1314
'utf-8'
1415
)
1516
)
@@ -39,7 +40,7 @@ export default async function ({
3940
}$`
4041
),
4142
// name: name + key.slice(1),
42-
replacement: new URL('.' + val.src, import.meta.url).pathname
43+
replacement: fileURLToPath(new URL('.' + val.src, import.meta.url))
4344
}
4445
})
4546

packages/tools/src/parse/packageJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fileURLToPath } from "node:url"
2+
13
interface PackageJson {
24
name: string
35
exports: Record<
@@ -21,7 +23,7 @@ export const parseExportAliases = (pkgJson: PackageJson) => {
2123
.map(([key, val]) => {
2224
return {
2325
find: name + key.slice(1),
24-
replacement: new URL('.' + val.src, import.meta.url).pathname
26+
replacement: fileURLToPath(new URL('.' + val.src, import.meta.url))
2527
}
2628
})
2729
.sort(

packages/tools/src/render/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Handlebars from 'handlebars'
22
import { existsSync, promises } from 'fs'
3+
import { fileURLToPath } from 'url'
34

45
export { renderPackageJson } from './packageJson.js'
56
export { renderTsconfigJson } from './tsconfigJson.js'
@@ -82,7 +83,8 @@ export const renderDirectory = async ({
8283
templateVariables: Record<string, any>
8384
outputDir: URL
8485
}) => {
85-
if (outputDir.pathname[outputDir.pathname.length - 1] !== '/') {
86+
const outputDirPath = fileURLToPath(outputDir)
87+
if (outputDirPath[outputDirPath.length - 1] !== '/') {
8688
throw new Error(
8789
'outputDir is not a directory. Make sure the URL ends with a /'
8890
)

packages/vitrify/src/node/app-urls.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// import { resolve } from 'import-meta-resolve'
22
import { existsSync } from 'fs'
3-
import { fileURLToPath } from 'url'
3+
import { fileURLToPath, pathToFileURL } from 'url'
44

55
export const resolve = (packageName: string, base: URL, counter = 0): URL => {
66
const packageUrl = new URL(`./node_modules/${packageName}/`, base)
@@ -20,7 +20,7 @@ export const getPkgJsonDir = (dir: URL): URL => {
2020
return getPkgJsonDir(new URL('..', dir))
2121
}
2222
export const getAppDir = (dir?: URL) =>
23-
getPkgJsonDir(dir ?? new URL(`file://${process.cwd()}/`))
23+
getPkgJsonDir(dir ?? pathToFileURL(`${process.cwd()}/`))
2424
export const getCliDir = () => getPkgJsonDir(new URL('./', import.meta.url))
2525
export const getCliViteDir = (cliDir: URL) => new URL('src/vite/', cliDir)
2626
export const getSrcDir = (appDir: URL) => new URL('src/', appDir)

packages/vitrify/src/node/bin/cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cli
2121
.option('--productName [productName]', 'Product name')
2222
.option('--debug', 'Debug build')
2323
.action(async (options) => {
24+
console.log('test')
2425
const { build } = await import('./build.js')
2526
let appDir: URL
2627
let prerender
@@ -29,11 +30,13 @@ cli
2930
appDir = new URL(`file://${options.appDir}`)
3031
} else {
3132
appDir = getAppDir()
33+
console.log(appDir)
3234
}
33-
3435
const baseOutDir =
3536
parsePath(options.outDir, appDir) || new URL('dist/', appDir)
3637

38+
console.log( new URL("./", appDir).href)
39+
3740
const args: {
3841
base: string
3942
appDir?: URL
@@ -86,7 +89,7 @@ cli
8689
outDir: fileURLToPath(new URL('ssr/server/', baseOutDir))
8790
})
8891
;({ prerender } = await import(
89-
new URL('ssr/server/prerender.mjs', baseOutDir).pathname
92+
new URL('ssr/server/prerender.mjs', baseOutDir).href
9093
))
9194

9295
const {

packages/vitrify/src/node/bin/dev.ts

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LogLevel, InlineConfig, ViteDevServer } from 'vite'
1+
import { type LogLevel, type InlineConfig, type ViteDevServer, resolveConfig, ResolvedConfig } from 'vite'
22
import { baseConfig } from '../index.js'
33
import type { Server } from 'net'
44
import fastify from 'fastify'
@@ -53,19 +53,10 @@ export async function createVitrifyDevServer({
5353
)
5454

5555
if (!appDir) appDir = getAppDir()
56-
let config: InlineConfig = {}
56+
let config: InlineConfig | ResolvedConfig = {}
5757
let ssrMode: 'server' | 'fastify' | undefined
5858
if (ssr === 'ssr') ssrMode = 'server'
5959
if (ssr === 'fastify') ssrMode = 'fastify'
60-
config = await baseConfig({
61-
framework,
62-
ssr: ssrMode,
63-
command: 'dev',
64-
mode: 'development',
65-
appDir,
66-
publicDir,
67-
base
68-
})
6960

7061
const wsPort = await getFirstOpenPort(24678)
7162
if (config.server?.https) {
@@ -74,25 +65,49 @@ export async function createVitrifyDevServer({
7465
)
7566
}
7667

68+
69+
config = await resolveConfig({
70+
...await baseConfig({
71+
framework,
72+
ssr: ssrMode,
73+
command: 'dev',
74+
mode: 'development',
75+
appDir,
76+
publicDir,
77+
base
78+
}),
79+
server: {
80+
host,
81+
port,
82+
// hmr: {
83+
// protocol: config.server?.https ? 'wss' : 'ws',
84+
// port: wsPort
85+
// }
86+
}
87+
},
88+
"serve")
89+
90+
7791
const vitrifyDevServer = await (
7892
await import('vite')
7993
).createServer({
94+
// @ts-ignore ignore
8095
configFile: false,
8196
...config,
8297
logLevel,
8398
define: {
8499
...config.define,
85100
__HOST__: `'${host}'`
86101
},
87-
server: {
88-
...config.server,
89-
host,
90-
port,
91-
hmr: {
92-
protocol: config.server?.https ? 'wss' : 'ws',
93-
port: wsPort
94-
}
95-
}
102+
// server: {
103+
// ...config.server,
104+
// host,
105+
// port,
106+
// hmr: {
107+
// protocol: config.server?.https ? 'wss' : 'ws',
108+
// port: wsPort
109+
// }
110+
// }
96111
})
97112

98113
return vitrifyDevServer
@@ -153,9 +168,9 @@ export async function createServer({
153168
: fileURLToPath(new URL(`src/vite/${framework}/ssr/app.ts`, cliDir))
154169

155170
const environment = vite.environments.ssr
156-
;({ setup, onTemplateRendered, onAppRendered, vitrifyConfig } =
157-
// @ts-expect-error missing types
158-
await environment.runner.import(entryUrl))
171+
; ({ setup, onTemplateRendered, onAppRendered, vitrifyConfig } =
172+
// @ts-expect-error missing types
173+
await environment.runner.import(entryUrl))
159174

160175
app = fastify({
161176
logger: {
@@ -196,14 +211,14 @@ export async function createServer({
196211
if (vite && app) {
197212
await app.ready()
198213
await app.close()
199-
;({ app, server, vite } = await createServer({
200-
ssr: 'fastify',
201-
host: host,
202-
port,
203-
appDir,
204-
publicDir,
205-
vite
206-
}))
214+
; ({ app, server, vite } = await createServer({
215+
ssr: 'fastify',
216+
host: host,
217+
port,
218+
appDir,
219+
publicDir,
220+
vite
221+
}))
207222
}
208223
}
209224
}

packages/vitrify/src/node/bin/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises as fs } from 'fs'
22
import readline from 'readline'
33
import { getAppDir, getCliDir, getProjectURLs } from '../app-urls.js'
4+
import { fileURLToPath } from 'url'
45

56
const rl = readline.createInterface({
67
input: process.stdin,
@@ -20,7 +21,7 @@ export async function run(filePath: string) {
2021
const cliDir = getCliDir()
2122
const projectURLs = getProjectURLs(appDir, cliDir)
2223
const pkg = JSON.parse(
23-
(await fs.readFile(projectURLs.cli('package.json'), 'utf-8')).toString()
24+
(await fs.readFile(fileURLToPath(projectURLs.cli('package.json')), 'utf-8')).toString()
2425
)
2526

2627
if (!run)

packages/vitrify/src/node/frameworks/vue/prerender.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OnTemplateRenderedHook } from 'src/node/vitrify-config.js'
33
import { routesToPaths } from '../../helpers/routes.js'
44
import { renderHtml } from './fastify-ssr-plugin.js'
55
import { type RouteRecordRaw } from 'vue-router'
6+
import { fileURLToPath } from 'url'
67

78
export const prerender = async ({
89
outDir,
@@ -36,8 +37,8 @@ export const prerender = async ({
3637
url.split('/').slice(0, -1).join('/'),
3738
`file://${outDir}`
3839
)
39-
if (!existsSync(directoryUrl.pathname)) {
40-
mkdirSync(directoryUrl.pathname, { recursive: true })
40+
if (!existsSync(fileURLToPath(directoryUrl))) {
41+
mkdirSync(fileURLToPath(directoryUrl), { recursive: true })
4142
}
4243

4344
const filename =

packages/vitrify/src/node/index.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export const baseConfig = async ({
211211
let rawVitrifyConfig: VitrifyConfig | VitrifyConfigAsync
212212
let vitrifyConfig: VitrifyConfig
213213

214+
console.log(appDir)
214215
try {
215216
if (fs.existsSync(fileURLToPath(new URL('vitrify.config.ts', appDir)))) {
216217
const configPath = fileURLToPath(new URL('vitrify.config.ts', appDir))
@@ -219,11 +220,13 @@ export const baseConfig = async ({
219220
)
220221
fs.writeFileSync(configPath + '.js', bundledConfig.code)
221222

223+
console.log('kljsdflkjdsf')
224+
console.log(configPath)
222225
rawVitrifyConfig = (await import('file://' + configPath + '.js')).default
223226
fs.unlinkSync(configPath + '.js')
224227
} else {
225228
rawVitrifyConfig = (
226-
await import(fileURLToPath(new URL('vitrify.config.js', appDir)))
229+
await import(new URL('vitrify.config.js', appDir).href)
227230
).default
228231
}
229232
if (typeof rawVitrifyConfig === 'function') {
@@ -427,7 +430,7 @@ export const baseConfig = async ({
427430
.replaceAll('+', '')
428431
429432
return `import ${varName} from '${
430-
new URL(url, appDir).pathname
433+
new URL(url, appDir).href
431434
}'; onAppMounted.push(${varName});`
432435
})
433436
.join('\n')}
@@ -446,7 +449,7 @@ export const baseConfig = async ({
446449
.replaceAll('+', '')
447450
448451
return `import ${varName} from '${
449-
new URL(url, appDir).pathname
452+
new URL(url, appDir).href
450453
}'; onAppRendered.push(${varName});`
451454
})
452455
.join('\n')}
@@ -465,7 +468,7 @@ export const baseConfig = async ({
465468
.replaceAll('+', '')
466469
467470
return `import ${varName} from '${
468-
new URL(url, appDir).pathname
471+
new URL(url, appDir).href
469472
}'; onTemplateRendered.push(${varName});`
470473
})
471474
.join('\n')}
@@ -484,7 +487,7 @@ export const baseConfig = async ({
484487
.replaceAll('+', '')
485488
486489
return `import ${varName} from '${
487-
new URL(url, appDir).pathname
490+
new URL(url, appDir).href
488491
}'; onAppCreated.push(${varName});`
489492
})
490493
.join('\n')}
@@ -501,7 +504,7 @@ export const baseConfig = async ({
501504
.replaceAll('+', '')
502505
503506
return `import ${varName} from '${
504-
new URL(url, appDir).pathname
507+
new URL(url, appDir).href
505508
}'; onSetup.push(${varName});`
506509
})
507510
.join('\n')}`
@@ -601,14 +604,7 @@ export const baseConfig = async ({
601604
entry = fileURLToPath(new URL('csr/entry.ts', frameworkDir))
602605
}
603606
let entryScript
604-
if (process.platform === 'win32') {
605-
const split = entry.split('node_modules')
606-
entryScript = `<script type="module" src="node_modules${split.at(
607-
-1
608-
)}"></script>`
609-
} else {
610-
entryScript = `<script type="module" src="${entry}"></script>`
611-
}
607+
entryScript = `<script type="module" src="${entry}"></script>`
612608
html = appendToBody(entryScript, html)
613609
if (productName) html = addOrReplaceTitle(productName, html)
614610
return html
@@ -793,7 +789,7 @@ export const baseConfig = async ({
793789
// environments: {
794790
// },
795791
server: {
796-
https: vitrifyConfig.server?.https,
792+
// https: vitrifyConfig.server?.https,
797793
// middlewareMode: mode === 'ssr' ? 'ssr' : undefined,
798794
middlewareMode: ssr ? true : false,
799795
fs: {

0 commit comments

Comments
 (0)