diff --git a/examples/with-pkg/README.md b/examples/with-pkg/README.md new file mode 100644 index 00000000..4a2b7490 --- /dev/null +++ b/examples/with-pkg/README.md @@ -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. diff --git a/examples/with-pkg/index.js b/examples/with-pkg/index.js new file mode 100644 index 00000000..0306f3c5 --- /dev/null +++ b/examples/with-pkg/index.js @@ -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') diff --git a/examples/with-pkg/package.json b/examples/with-pkg/package.json new file mode 100644 index 00000000..fc01230b --- /dev/null +++ b/examples/with-pkg/package.json @@ -0,0 +1,32 @@ +{ + "name": "with-pkg", + "version": "1.0.0", + "main": "index.js", + "author": "Sergio Daniel Xalambrí ", + "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" + ] + } +} diff --git a/examples/with-pkg/pages/index.js b/examples/with-pkg/pages/index.js new file mode 100644 index 00000000..5b243bf1 --- /dev/null +++ b/examples/with-pkg/pages/index.js @@ -0,0 +1,2 @@ +export default () => +

Home page

diff --git a/examples/with-pkg/server.js b/examples/with-pkg/server.js new file mode 100644 index 00000000..c8e89a38 --- /dev/null +++ b/examples/with-pkg/server.js @@ -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') + }) +})