1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add with pkg example (#2751)

This commit is contained in:
Sergio Xalambrí 2017-08-10 01:06:14 -05:00 committed by Tim Neutkens
parent 62b246d902
commit cc5289a89f
5 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,29 @@
# Example with pkg
## How to use
Download the example [or clone the repo](https://github.com/zeit/next.js):
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-pkg
cd with-pkg
```
Install it and run:
```bash
npm install
npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example demostrate how you can use [pkg](https://github.com/zeit/pkg) to create a binary version of a Next.js application.
To do it we need to create at least a super simple custom server that allow us to run `node server.js` instead of `next` or `next start`. We also need to create a `index.js` that works as the entry point for **pkg**, in that file we force to set NODE_ENV as production.

View file

@ -0,0 +1,3 @@
// when pkg-ing the app we set NODE_ENV as production and require the `./server.js`
process.env.NODE_ENV = 'production'
require('./server.js')

View file

@ -0,0 +1,32 @@
{
"name": "with-pkg",
"version": "1.0.0",
"main": "index.js",
"author": "Sergio Daniel Xalambrí <sergiodxa@gmail.com>",
"license": "MIT",
"scripts": {
"dev": "node server.js",
"build": "next build",
"prestart": "npm run build",
"start": "NODE_ENV=production node server.js",
"predist": "npm run build",
"dist": "pkg index.js --out-dir dist"
},
"dependencies": {
"next": "^3.0.1",
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"pkg": "^4.2.2"
},
"pkg": {
"assets": [
".next",
"pages"
],
"scripts": [
".next/dist/**/*.js"
]
}
}

View file

@ -0,0 +1,2 @@
export default () =>
<h1>Home page</h1>

View file

@ -0,0 +1,16 @@
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare()
.then(() => {
createServer((req, res) => handle(req, res, parse(req.url, true).pathname))
.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})