Skip to content

Commit

Permalink
fix: Use posix path in import when building on windows (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstork authored Apr 3, 2024
1 parent bbf9f84 commit 64455ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
23 changes: 15 additions & 8 deletions packages/remix-adapter/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import type { Plugin, ResolvedConfig } from 'vite'
import { mkdir, writeFile } from 'node:fs/promises'
import { join, relative } from 'node:path'
import { join, relative, sep } from 'node:path'
import { sep as posixSep } from 'node:path/posix'
import { version, name } from '../package.json'

const SERVER_ID = 'virtual:netlify-server'
const RESOLVED_SERVER_ID = `\0${SERVER_ID}`

const toPosixPath = (path: string) => path.split(sep).join(posixSep)

// The virtual module that is the compiled server entrypoint.
const serverCode = /* js */ `
import { createRequestHandler } from "@netlify/remix-adapter";
import * as build from "virtual:remix/server-build";
export default createRequestHandler({ build });
`

// This is written to the functions directory. It just re-exports
// the compiled entrypoint, along with Netlify function config.
function generateNetlifyFunction(server: string) {
Expand Down Expand Up @@ -65,14 +70,16 @@ export function netlifyPlugin(): Plugin {
resolvedConfig = config
},
async writeBundle() {
// Write the server entrypoint to the Netlify functions directory
if (currentCommand === 'build' && isSsr) {
// Write the server entrypoint to the Netlify functions directory
const functionDir = join(resolvedConfig.root, '.netlify/functions-internal')
await mkdir(functionDir, { recursive: true })
await writeFile(
join(functionDir, 'remix-server.mjs'),
generateNetlifyFunction(relative(functionDir, join(resolvedConfig.build.outDir, 'server.js'))),
)
const functionsDirectory = join(resolvedConfig.root, '.netlify/functions-internal')

await mkdir(functionsDirectory, { recursive: true })

const serverPath = join(resolvedConfig.build.outDir, 'server.js')
const relativeServerPath = toPosixPath(relative(functionsDirectory, serverPath))

await writeFile(join(functionsDirectory, 'remix-server.mjs'), generateNetlifyFunction(relativeServerPath))
}
},
}
Expand Down
23 changes: 17 additions & 6 deletions packages/remix-edge-adapter/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { Plugin, ResolvedConfig } from 'vite'
import { writeFile, mkdir, readdir } from 'node:fs/promises'
import { join, relative } from 'node:path'
import { join, relative, sep } from 'node:path'
import { sep as posixSep } from 'node:path/posix'
import { version, name } from '../package.json'

const SERVER_ID = 'virtual:netlify-server'
const RESOLVED_SERVER_ID = `\0${SERVER_ID}`

const toPosixPath = (path: string) => path.split(sep).join(posixSep)

// The virtual module that is the compiled server entrypoint.
const serverCode = /* js */ `
import { createRequestHandler } from "@netlify/remix-edge-adapter";
import * as build from "virtual:remix/server-build";
export default createRequestHandler({ build });
`

// This is written to the edge functions directory. It just re-exports
// the compiled entrypoint, along with the Netlify function config.
function generateEntrypoint(server: string, exclude: Array<string> = []) {
Expand Down Expand Up @@ -93,12 +97,13 @@ export function netlifyPlugin(): Plugin {
}
},
async writeBundle() {
// Write the server entrypoint to the Netlify functions directory
if (currentCommand === 'build' && isSsr) {
const edgeFunctionDir = join(resolvedConfig.root, '.netlify/edge-functions')
const exclude: Array<string> = []
try {
// Get the client files so we can skip them in the edge function
const entries = await readdir(join(resolvedConfig.build.outDir, '..', 'client'), { withFileTypes: true })
const clientDirectory = join(resolvedConfig.build.outDir, '..', 'client')
const entries = await readdir(clientDirectory, { withFileTypes: true })
for (const entry of entries) {
// With directories we don't bother to recurse into it and just skip the whole thing.
if (entry.isDirectory()) {
Expand All @@ -111,10 +116,16 @@ export function netlifyPlugin(): Plugin {
// Ignore if it doesn't exist
}

await mkdir(edgeFunctionDir, { recursive: true })
const edgeFunctionsDirectory = join(resolvedConfig.root, '.netlify/edge-functions')

await mkdir(edgeFunctionsDirectory, { recursive: true })

const serverPath = join(resolvedConfig.build.outDir, 'server.js')
const relativeServerPath = toPosixPath(relative(edgeFunctionsDirectory, serverPath))

await writeFile(
join(edgeFunctionDir, 'remix-server.mjs'),
generateEntrypoint(relative(edgeFunctionDir, join(resolvedConfig.build.outDir, 'server.js')), exclude),
join(edgeFunctionsDirectory, 'remix-server.mjs'),
generateEntrypoint(relativeServerPath, exclude),
)
}
},
Expand Down

0 comments on commit 64455ed

Please sign in to comment.