Allow synchronous module + fix typo in logs

This commit is contained in:
Sébastien Chopin 2017-05-21 19:17:36 +02:00
parent 1f317a188b
commit 2247097b64

View File

@ -32,7 +32,7 @@ class Module {
/* istanbul ignore if */
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
// eslint-disable-next-line no-console
console.warn('[Nuxt] invalid template', template)
console.warn('[nuxt] invalid template', template)
return
}
// Generate unique and human readable dst filename
@ -45,15 +45,9 @@ class Module {
options: template.options
}
this.options.build.templates.push(templateObj)
// Watch template for changes
this.addWatch(src)
return templateObj
}
addWatch (pattern) {
this.options.build.watch.push(pattern)
}
addPlugin (template) {
const {dst} = this.addTemplate(template)
// Add to nuxt plugins
@ -90,21 +84,22 @@ class Module {
}
// Allows passing runtime options to each module
const options = moduleOpts.options || {}
let src = moduleOpts.src || moduleOpts
const originalSrc = moduleOpts.src || moduleOpts
let src = originalSrc
// Resolve module
let module
try {
if (typeof src === 'string') {
// Using ~ shorthand modules are resolved from project srcDir
if (src.indexOf('~') === 0) {
src = path.resolve(this.options.srcDir, src.substr(1))
// Using ~ or ./ shorthand modules are resolved from project srcDir
if (src.indexOf('~') === 0 || src.indexOf('./') === 0) {
src = path.join(this.options.srcDir, src.substr(1))
}
// eslint-disable-next-line no-eval
module = eval('require')(src)
}
} catch (e) /* istanbul ignore next */ {
// eslint-disable-next-line no-console
console.error('[Nuxt] Unable to resolve module', src)
console.error('[nuxt] Unable to resolve module', src)
// eslint-disable-next-line no-console
console.error(e)
return
@ -113,7 +108,7 @@ class Module {
/* istanbul ignore if */
if (!(module instanceof Function)) {
// eslint-disable-next-line no-console
console.error('[Nuxt] Module should be a function', module)
return console.error(`[nuxt] Module [${originalSrc}] should export a function`)
}
// Add module to this.modules
this.modules.push(module)
@ -126,9 +121,14 @@ class Module {
}
resolve(module)
})
// If module send back a promise
if (result && result.then instanceof Function) {
return result.then(resolve)
}
// If not expecting a callback but returns no promise (=synchronous)
if (module.length < 2) {
return resolve(module)
}
})
}
}