Merge branch 'dev' of github.com:nuxt/nuxt.js into dev

This commit is contained in:
Sebastien Chopin 2017-06-02 12:15:41 +02:00
commit b4b740f2a0
21 changed files with 102 additions and 84 deletions

View File

@ -1,5 +1,6 @@
language: node_js
node_js:
- "8.0"
- "7.2"
- "6.9"
before_install:

View File

@ -52,13 +52,12 @@ if (analyzeBuild) {
}
console.log('[nuxt] Building...') // eslint-disable-line no-console
new Nuxt(options).then(nuxt => {
nuxt.build()
.then(() => {
console.log('[nuxt] Building done') // eslint-disable-line no-console
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})
var nuxt = module.exports = new Nuxt(options)
nuxt.build()
.then(() => {
console.log('[nuxt] Building done') // eslint-disable-line no-console
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})

View File

@ -39,19 +39,20 @@ if (typeof options.rootDir !== 'string') {
}
options.dev = true // Add hot reloading and watching changes
new Nuxt(options).then(nuxt => {
var server = new nuxt.Server(nuxt)
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
listenOnConfigChanges(nuxt, server)
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
nuxt.build()
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})
listenOnConfigChanges(nuxt, server)
function listenOnConfigChanges (nuxt, server) {
nuxt.build()
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
function listenOnConfigChanges(nuxt, server) {
// Listen on nuxt.config.js changes
var build = _.debounce(() => {
debug('[nuxt.config.js] changed')
@ -66,20 +67,20 @@ function listenOnConfigChanges (nuxt, server) {
}
options.rootDir = rootDir
nuxt.close()
.then(() => {
nuxt.renderer = null
debug('Rebuilding the app...')
return (new Nuxt(options)).then(nuxt => nuxt.build())
})
.then((nuxt) => {
server.nuxt = nuxt
})
.catch((error) => {
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
process.exit(1)
})
.then(() => {
nuxt.renderer = null
debug('Rebuilding the app...')
return new Nuxt(options).build()
})
.then((nuxt) => {
server.nuxt = nuxt
})
.catch((error) => {
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
process.exit(1)
})
}, 200)
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
.on('all', build)
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, {ignoreInitial: true}))
.on('all', build)
}

View File

@ -20,13 +20,12 @@ if (typeof options.rootDir !== 'string') {
options.dev = false // Force production mode (no webpack middleware called)
console.log('[nuxt] Generating...') // eslint-disable-line no-console
new Nuxt(options).then(nuxt => {
nuxt.generate()
.then(() => {
console.log('[nuxt] Generate done') // eslint-disable-line no-console
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})
var nuxt = module.exports = new Nuxt(options)
nuxt.generate()
.then(() => {
console.log('[nuxt] Generate done') // eslint-disable-line no-console
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})

View File

@ -16,10 +16,7 @@ if (typeof options.rootDir !== 'string') {
}
options.dev = false // Force production mode (no webpack middleware called)
new Nuxt(options).then(nuxt => {
new nuxt.Server(nuxt)
.listen(
process.env.PORT || process.env.npm_package_config_nuxt_port,
process.env.HOST || process.env.npm_package_config_nuxt_host
)
})
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)

View File

@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
config.rootDir = rootDir // project folder
config.dev = false // production build
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(4000, 'localhost')

View File

@ -97,8 +97,10 @@ export function options () {
}
export async function build () {
this._nuxtPages = typeof this.createRoutes !== 'function'
// Initialize modules first
await this.module.init()
// Check if pages dir exists and warn if not
this._nuxtPages = typeof this.createRoutes !== 'function'
if (this._nuxtPages) {
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {

View File

@ -4,13 +4,27 @@ import path from 'path'
import fs from 'fs'
import {uniq} from 'lodash'
import hash from 'hash-sum'
import {chainFn} from './utils'
import {chainFn, sequence} from './utils'
const debug = require('debug')('nuxt:module')
class Module {
constructor (nuxt) {
this.nuxt = nuxt
this.options = nuxt.options
this.modules = []
this.initialized = false
}
async init () {
if (this.initialized) {
debug('[nuxt] Modules are already initialized')
return
}
// Install all modules in sequence
await sequence(this.options.modules, this.addModule.bind(this))
// Indicate modules are already initialized
this.initialized = true
}
addVendor (vendor) {
@ -31,8 +45,7 @@ class Module {
const srcPath = path.parse(src)
/* istanbul ignore if */
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
// eslint-disable-next-line no-console
console.warn('[nuxt] invalid template', template)
debug('[nuxt] invalid template', template)
return
}
// Generate unique and human readable dst filename
@ -82,8 +95,15 @@ class Module {
if (!moduleOpts) {
return
}
// Allow using babel style array options
if (Array.isArray(moduleOpts)) {
moduleOpts = {
src: moduleOpts[0],
options: moduleOpts[1]
}
}
// Allows passing runtime options to each module
const options = moduleOpts.options || {}
const options = moduleOpts.options || (typeof moduleOpts === 'object' ? moduleOpts : {})
const originalSrc = moduleOpts.src || moduleOpts
// Resolve module
let module = originalSrc

View File

@ -121,14 +121,6 @@ class Nuxt {
this.utils = utils
// Add module integration
this.module = new Module(this)
// Install all modules in sequence and then return `this` instance
return utils.sequence(options.modules, this.module.addModule.bind(this.module))
.then(() => this)
.catch(/* istanbul ignore next */ (err) => {
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
}
close (callback) {

View File

@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: true
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -14,7 +14,7 @@ test('Fail with routes() which throw an error', async t => {
}
}
}
const nuxt = await new Nuxt(options)
const nuxt = new Nuxt(options)
return new Promise((resolve) => {
var oldExit = process.exit
var oldCE = console.error // eslint-disable-line no-console

View File

@ -17,7 +17,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
try {
await nuxt.generate() // throw an error (of /validate route)
} catch (err) {}

View File

@ -16,7 +16,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -6,7 +6,7 @@ const readFile = pify(fs.readFile)
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false
})

View File

@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/error'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -2,8 +2,13 @@ module.exports = {
loading: true,
modules: [
'~modules/basic',
'~/modules/middleware',
'./modules/template'
{
src: '~/modules/middleware',
options: {
foo: 'bar'
}
},
['./modules/template', {baz: 'ping'}]
],
serverMiddleware: [
'./modules/middleware/midd2'

View File

@ -8,7 +8,7 @@ test('Nuxt.js Class', t => {
})
test('Nuxt.js Instance', async t => {
const nuxt = await new Nuxt()
const nuxt = new Nuxt()
t.is(typeof nuxt, 'object')
t.is(nuxt.dev, true)
t.is(typeof nuxt.build, 'function')
@ -16,7 +16,7 @@ test('Nuxt.js Instance', async t => {
})
test.serial('Fail when build not done and try to render', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname, 'fixtures/empty')
})
@ -37,7 +37,7 @@ test.serial('Fail when build not done and try to render', async t => {
})
test.serial('Fail to build when no pages/ directory but is in the parent', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
})
@ -58,7 +58,7 @@ test.serial('Fail to build when no pages/ directory but is in the parent', async
})
test.serial('Fail to build when no pages/ directory', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname)
})

View File

@ -8,6 +8,8 @@ const url = (route) => 'http://localhost:' + port + route
let nuxt = null
let server = null
const wp = p => /^win/.test(process.platform) ? p.replace(/[\\/]/g, '\\\\') : p
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
@ -15,7 +17,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
@ -26,7 +28,7 @@ test('Vendor', async t => {
})
test('Plugin', async t => {
t.true(nuxt.options.plugins[0].src.includes('fixtures/module/.nuxt/basic.reverse.'), 'plugin added to config')
t.true(nuxt.options.plugins[0].src.includes(wp('fixtures/module/.nuxt/basic.reverse.')), 'plugin added to config')
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
})

View File

@ -5,7 +5,7 @@ let utils
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
let nuxt = await new Nuxt({ dev: false })
let nuxt = new Nuxt({ dev: false })
utils = nuxt.utils
})

View File

@ -15,7 +15,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
@ -110,5 +110,5 @@ test.after('Should be able to start Nuxt with build done', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
})