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:
parent
62b246d902
commit
cc5289a89f
29
examples/with-pkg/README.md
Normal file
29
examples/with-pkg/README.md
Normal 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.
|
3
examples/with-pkg/index.js
Normal file
3
examples/with-pkg/index.js
Normal 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')
|
32
examples/with-pkg/package.json
Normal file
32
examples/with-pkg/package.json
Normal 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"
|
||||
]
|
||||
}
|
||||
}
|
2
examples/with-pkg/pages/index.js
Normal file
2
examples/with-pkg/pages/index.js
Normal file
|
@ -0,0 +1,2 @@
|
|||
export default () =>
|
||||
<h1>Home page</h1>
|
16
examples/with-pkg/server.js
Normal file
16
examples/with-pkg/server.js
Normal 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')
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue