Created
May 21, 2023 14:48
-
-
Save milansimek/4e51825ca30c7cfe019f68bc3922c8f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, writeFileSync } from 'node:fs'; | |
import adapter from '@sveltejs/adapter-node'; | |
export default function (opts = {}) { | |
const PORT = 3001; | |
const HOST = 'localhost'; | |
const adapterNode = adapter(opts); | |
adapterNode.originalAdapt = adapterNode.adapt; | |
adapterNode.adapt = async function (builder) { | |
const result = await adapterNode.originalAdapt(builder); | |
console.log('> Adding proxy middleware to server'); | |
const buildDir = opts.out || 'build'; | |
const indexJsPath = `./${buildDir}/index.js`; | |
const importProxyMiddleWare = `import { createProxyMiddleware } from "http-proxy-middleware";`; | |
const serverInitString = `const server = polka()`; | |
const proxyJs = `.use('/socket.io', createProxyMiddleware({ target: 'http://${HOST}:${PORT}', ws: true }))`; | |
let indexJs = readFileSync(indexJsPath, 'utf8'); | |
indexJs = indexJs.replace(serverInitString, serverInitString + proxyJs); | |
indexJs = importProxyMiddleWare + '\n' + indexJs; | |
writeFileSync(indexJsPath, indexJs, 'utf8'); | |
console.log('> Proxy middleware added to server'); | |
return result; | |
} | |
return adapterNode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment