Skip to content

Commit

Permalink
Add nuxt-build and nuxt-start, build:false and dev option
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Nov 9, 2016
1 parent 78d8122 commit 18a2b57
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 227 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ So far, we get:
## Using nuxt.js programmatically

Nuxt is built on the top of ES2015, which makes the code more enjoyable and cleaner to read. It doesn't make use of any transpilers and depends upon Core V8 implemented features.
For these reasons, Nuxt.js targets Node.js `4.0` or higher (you might want to launch node with the `--harmony-proxies` flag if you running `node <= 6.5.0` )
For these reasons, nuxt.js targets Node.js `4.0` or higher (you might want to launch node with the `--harmony-proxies` flag if you running `node <= 6.5.0` )

```js
const Nuxt = require('nuxt')
Expand Down Expand Up @@ -115,3 +115,30 @@ cd node_modules/nuxt/
bin/nuxt examples/hello-world
# Go to http://localhost:3000
```

## Production deployment

To deploy, instead of running next, you probably want to build ahead of time. Therefore, building and starting are separate commands:

```bash
nuxt build
nuxt start
```

For example, to deploy with [`now`](https://zeit.co/now) a `package.json` like follows is recommended:
```json
{
"name": "my-app",
"dependencies": {
"next": "latest"
},
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start"
}
}
```
Then run `now` and enjoy!

Note: we recommend putting `.nuxt` in `.npmignore` or `.gitignore`.
6 changes: 4 additions & 2 deletions bin/nuxt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const { join } = require('path')
const { spawn } = require('cross-spawn')

const defaultCommand = 'start'
const defaultCommand = 'dev'
const commands = new Set([
defaultCommand,
'init'
'init',
'build',
'start'
])

let cmd = process.argv[2]
Expand Down
27 changes: 27 additions & 0 deletions bin/nuxt-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

const fs = require('fs')
const Nuxt = require('../')
const { resolve } = require('path')

const rootDir = resolve(process.argv.slice(2)[0] || '.')
const nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
let options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}

options.dev = false // Create production build when calling `nuxt build`

console.log('[nuxt] Building...')
new Nuxt(options)
.then((nuxt) => {
console.log('[nuxt] Building done')
})
.catch((err) => {
console.error(err)
process.exit()
})
26 changes: 26 additions & 0 deletions bin/nuxt-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node

const fs = require('fs')
const Nuxt = require('../')
const Server = require('../lib/server')
const { resolve } = require('path')

const rootDir = resolve(process.argv.slice(2)[0] || '.')
const nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
let options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}

new Nuxt(options)
.then((nuxt) => {
new Server(nuxt)
.listen(process.env.PORT, process.env.HOST)
})
.catch((err) => {
console.error(err)
process.exit()
})
48 changes: 4 additions & 44 deletions bin/nuxt-start
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env node

const http = require('http')
const co = require('co')
const fs = require('fs')
const pify = require('pify')
const serveStatic = require('serve-static')
const Nuxt = require('../')
const Server = require('../lib/server')
const { resolve } = require('path')

const rootDir = resolve(process.argv.slice(2)[0] || '.')
Expand All @@ -18,6 +15,9 @@ if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}

options.build = false // Disable building
options.dev = false // Force production mode (no webpack middlewares called)

new Nuxt(options)
.then((nuxt) => {
new Server(nuxt)
Expand All @@ -27,43 +27,3 @@ new Nuxt(options)
console.error(err)
process.exit()
})

class Server {

constructor (nuxt) {
this.server = http.createServer(this.handle.bind(this))
this.serveStatic = pify(serveStatic(resolve(rootDir, 'static')))
this.nuxt = nuxt
return this
}

handle (req, res) {
const method = req.method.toUpperCase()
const self = this

if (method !== 'GET' && method !== 'HEAD') {
return this.nuxt.render(req, res)
}
co(function * () {
if (req.url.includes('/static/')) {
const url = req.url
req.url = req.url.replace('/static/', '/')
yield self.serveStatic(req, res)
req.url = url
}
})
.then(() => {
// File not found
this.nuxt.render(req, res)
})
}

listen (port, host) {
host = host || 'localhost'
port = port || 3000
this.server.listen(port, host, () => {
console.log('Ready on http://%s:%s', host, port)
})
}

}
11 changes: 6 additions & 5 deletions examples/global-css/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ module.exports = {
To see the demo working:
```bash
npm install
npm start
npm run dev
```

Go to [http://localhost:8080](http://localhost:8080) and navigate inside the app.
Go to [http://localhost:3000](http://localhost:3000) and navigate inside the app.

## Production

In production, they will be minified and extracted in a file named `styles.css` and added in the `<head>` of the page.

To launch the demo in production mode so you can see the ``<head>` populated with the `<link>` tag:
To launch the demo in production mode so you can see the `<head>` populated with the `<link>` tag:

```bash
NODE_ENV=production npm start
npm run build
npm start
```

Go to [http://localhost:8080](http://localhost:8080) and check the source code.
Go to [http://localhost:3000](http://localhost:3000) and check the source code.
4 changes: 3 additions & 1 deletion examples/global-css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"sass-loader": "^4.0.2"
},
"scripts": {
"start": "nuxt"
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start"
}
}
2 changes: 1 addition & 1 deletion examples/plugins-vendor/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Using external modules and plugings with Nuxt.js
# Using external modules and plugings with nuxt.js

## Configuration: `build.vendor`

Expand Down
12 changes: 7 additions & 5 deletions examples/with-ava/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ let server = null

// Init nuxt.js and create server listening on localhost:4000
test.before('Init nuxt.js', (t) => {
process.env.NODE_ENV = 'test'
const Nuxt = require('../../../')
const options = {
rootDir: resolve(__dirname, '..')
rootDir: resolve(__dirname, '..'),
dev: false
}
return new Nuxt(options)
.then(function (_nuxt) {
Expand Down Expand Up @@ -65,9 +65,11 @@ test('Route / exits and render HTML', async t => {
*/
test('Route / exits and render HTML', async t => {
const window = await renderAndGetWindow('/')
t.is(window.document.querySelector('p').textContent, 'Hello world!')
t.is(window.document.querySelector('p').className, 'red-color')
t.true(window.document.querySelectorAll('style')[2].textContent.includes('.red-color {\n color: red;\n}'))
const element = window.document.querySelector('.red-color')
t.not(element, null)
t.is(element.textContent, 'Hello world!')
t.is(element.className, 'red-color')
t.is(window.getComputedStyle(element).color, 'red')
})

// Close server and ask nuxt to stop listening to file changes
Expand Down
2 changes: 1 addition & 1 deletion lib/app/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ Promise.all(resolveComponents)
}
})
.catch((err) => {
console.error('[Nuxt.js] Cannot load components', err)
console.error('[nuxt.js] Cannot load components', err)
})
4 changes: 2 additions & 2 deletions lib/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pick } from 'lodash'
import { app, router<%= (store ? ', store' : '') %> } from './index'
import { getMatchedComponents, getContext } from './utils'

const isDev = process.env.NODE_ENV !== 'production'
const isDev = <%= isDev %>
const _app = new Vue(app)

// This exported function will be called by `bundleRenderer`.
Expand Down Expand Up @@ -54,7 +54,7 @@ export default context => {
}))
.then((res) => {
<% if (isDev) { %>
debug('Data fetch ' + context.req.url + ': ' + (Date.now() - s) + 'ms')
debug('Data fetching ' + context.req.url + ': ' + (Date.now() - s) + 'ms')
<% } %>
// datas are the first row of each
context.nuxt.data = res.map((tab) => tab[0])
Expand Down

0 comments on commit 18a2b57

Please sign in to comment.