Commit ed0e1750 authored by Resi Respati's avatar Resi Respati Committed by Luis Fernando Alvarez D

[custom-server-typescript] fixed custom server build issues (#7385)

parent 091a81e6
...@@ -4,5 +4,5 @@ node_modules/ ...@@ -4,5 +4,5 @@ node_modules/
# next.js build output # next.js build output
.next .next
# typescript build output # production server output
dist dist
...@@ -40,5 +40,6 @@ now ...@@ -40,5 +40,6 @@ now
## The idea behind the example ## The idea behind the example
The example shows how you can use [TypeScript](https://typescriptlang.com) on both the server and the client while using [Nodemon](https://nodemon.io/) to live reload the server code without affecting the Next.js universal code. The example shows how you can use [TypeScript](https://typescriptlang.com) on both the server and the client while using [Nodemon](https://nodemon.io/) to live reload the server code without affecting the Next.js universal code.
Server entry point is `server/index.ts` in development and `production-server/index.js` in production.
Server entry point is `server/index.ts` in development and `dist/index.js` in production.
The second directory should be added to `.gitignore`. The second directory should be added to `.gitignore`.
{ {
"watch": ["server"], "watch": ["server", "static"],
"ext": "ts",
"exec": "ts-node --project tsconfig.server.json server/index.ts" "exec": "ts-node --project tsconfig.server.json server/index.ts"
} }
{ {
"name": "custom-server-typescript",
"version": "1.0.0",
"scripts": { "scripts": {
"dev": "nodemon", "dev": "nodemon",
"build": "next build && tsc && tsc --project tsconfig.server.json", "build": "next build && tsc --project tsconfig.server.json",
"start": "cross-env NODE_ENV=production node .next/server" "start": "cross-env NODE_ENV=production node dist/index.js"
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "^8.1.0",
"react": "^16.8.4", "react": "^16.8.4",
"react-dom": "^16.8.4" "react-dom": "^16.8.4"
}, },
"devDependencies": { "devDependencies": {
"@types/next": "^8.0.1", "@types/next": "^8.0.5",
"@types/react": "^16.8.8", "@types/react": "^16.8.17",
"@types/react-dom": "16.8.4",
"@zeit/next-typescript": "^1.1.1", "@zeit/next-typescript": "^1.1.1",
"cross-env": "^5.2.0", "cross-env": "^5.2.0",
"nodemon": "^1.18.10", "nodemon": "^1.19.0",
"ts-node": "^8.0.3", "ts-node": "^8.1.0",
"typescript": "^3.3.3333" "typescript": "^3.4.5"
} }
} }
import { createServer } from 'http' import { createServer } from 'http'
import { parse } from 'url' import { parse } from 'url'
import * as next from 'next' import next from 'next'
const port = parseInt(process.env.PORT || '3000', 10) const port = parseInt(process.env.PORT || '3000', 10)
const dev = process.env.NODE_ENV !== 'production' const dev = process.env.NODE_ENV !== 'production'
...@@ -22,4 +22,7 @@ app.prepare() ...@@ -22,4 +22,7 @@ app.prepare()
} }
}) })
.listen(port) .listen(port)
// tslint:disable-next-line:no-console
console.log(`> Server listening at http://localhost:${port} as ${dev ? 'development' : process.env.NODE_ENV}`);
}) })
{ {
"compileOnSave": false,
"compilerOptions": { "compilerOptions": {
"noEmit": true,
"strict": true,
"target": "esnext", "target": "esnext",
"module": "esnext", "module": "esnext",
"jsx": "preserve", "jsx": "preserve",
"allowJs": true, "lib": ["dom", "es2017"],
"baseUrl": ".",
"moduleResolution": "node", "moduleResolution": "node",
"strict": true,
"allowJs": true,
"noEmit": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"skipLibCheck": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"isolatedModules": true,
"removeComments": false, "removeComments": false,
"preserveConstEnums": true, "preserveConstEnums": true,
"sourceMap": true, "sourceMap": true
"skipLibCheck": true,
"baseUrl": ".",
"typeRoots": [
"./node_modules/@types"
],
"lib": [
"dom",
"es2015",
"es2016"
],
"outDir": ".next"
}, },
"exclude": [".next", "server/**/*.*"] "exclude": ["dist", ".next", "out", "next.config.js"]
} }
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"noEmit": false,
"module": "commonjs", "module": "commonjs",
"outDir": "dist",
"target": "es2017", "target": "es2017",
"lib": ["es2017"] "isolatedModules": false,
"noEmit": false
}, },
"include": ["server/**/*.ts"], "include": ["server/**/*.ts"]
"exclude": [".next"]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment