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