Last active
May 20, 2024 09:40
-
-
Save chris-schra/4c26924bcc9d6229fb5a3720f9d50b17 to your computer and use it in GitHub Desktop.
Custom bootstrap for next js server
This file contains hidden or 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
let entryModified = false; | |
module.exports = { | |
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => { | |
if(dev && config.entry && !entryModified) | |
{ | |
require('@babel/register')({ | |
extensions: ['.js', '.jsx', '.ts', '.tsx'] | |
}) | |
entryModified = true; | |
const oldEntry = config.entry; | |
config.entry = async () => { | |
return require("./server/bootstrap").default().then().then(oldEntry) | |
} | |
} | |
}, | |
} |
This file contains hidden or 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
{ | |
"scripts": { | |
"start": "ts-node --project tsconfig.server.json server/index.ts" | |
} | |
} |
This file contains hidden or 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
// this is actually ./server/bootstrap.ts | |
const mongoose = require('mongoose'); | |
const bootstrapServer = async () => { | |
// do your bootstrap stuff here | |
}; | |
export default bootstrapServer; |
This file contains hidden or 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
// this is actually ./server/index.ts | |
bootstrapServer().then(() => { | |
console.log("server bootstrapped"); | |
startServer({ dir }, port, host) | |
.then(async (app:any) => { | |
await app.prepare(); | |
// NOTE: you can do your own logging implementation here | |
console.log(`server is running at ${host}:${port}`); | |
}) | |
.catch((err:any) => { | |
console.error(err) | |
process.exit(1) | |
}) | |
}) |
This file contains hidden or 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
{ | |
"extends": "./tsconfig.json", | |
"compilerOptions": { | |
"module": "CommonJS", | |
"target": "ES2017" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment