2017-05-11 10:59:08 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2017-06-05 08:57:41 +00:00
|
|
|
import { uniq } from 'lodash'
|
2017-05-11 18:11:00 +00:00
|
|
|
import hash from 'hash-sum'
|
2017-06-13 17:58:04 +00:00
|
|
|
import Tapable from 'tappable'
|
2017-06-16 12:42:45 +00:00
|
|
|
import { chainFn, sequence } from 'utils'
|
2017-06-18 09:35:00 +00:00
|
|
|
import Debug from 'debug'
|
2017-05-31 14:21:16 +00:00
|
|
|
|
2017-06-18 09:35:00 +00:00
|
|
|
const debug = Debug('nuxt:module')
|
2017-05-11 10:59:08 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
export default class ModuleContainer extends Tapable {
|
2017-06-05 08:57:41 +00:00
|
|
|
constructor (nuxt) {
|
2017-06-11 14:17:36 +00:00
|
|
|
super()
|
2017-05-11 10:04:15 +00:00
|
|
|
this.nuxt = nuxt
|
2017-05-11 10:59:08 +00:00
|
|
|
this.options = nuxt.options
|
2017-06-05 08:52:02 +00:00
|
|
|
this.requiredModules = []
|
2017-07-17 19:26:41 +00:00
|
|
|
}
|
2017-05-31 14:21:16 +00:00
|
|
|
|
2017-07-17 19:26:41 +00:00
|
|
|
async _ready () {
|
|
|
|
await sequence(this.options.modules, this.addModule.bind(this))
|
2017-07-30 11:47:50 +00:00
|
|
|
await this.applyPluginsAsync('ready', this)
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
addVendor (vendor) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!vendor) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
addTemplate (template) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 18:11:00 +00:00
|
|
|
if (!template) {
|
2017-05-11 10:59:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-11 18:11:00 +00:00
|
|
|
// Validate & parse source
|
|
|
|
const src = template.src || template
|
|
|
|
const srcPath = path.parse(src)
|
2017-05-14 22:33:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
|
2017-06-19 15:47:31 +00:00
|
|
|
/* istanbul ignore next */
|
2017-05-31 14:21:16 +00:00
|
|
|
debug('[nuxt] invalid template', template)
|
2017-05-11 10:59:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-11 18:11:00 +00:00
|
|
|
// Generate unique and human readable dst filename
|
2017-05-11 19:37:59 +00:00
|
|
|
const dst = template.fileName ||
|
2017-05-14 20:59:06 +00:00
|
|
|
(path.basename(srcPath.dir) + '.' + srcPath.name + '.' + hash(src) + srcPath.ext)
|
2017-05-11 18:11:00 +00:00
|
|
|
// Add to templates list
|
|
|
|
const templateObj = {
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
options: template.options
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-05-11 18:47:53 +00:00
|
|
|
this.options.build.templates.push(templateObj)
|
2017-05-11 18:11:00 +00:00
|
|
|
return templateObj
|
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
addPlugin (template) {
|
2017-06-11 14:17:36 +00:00
|
|
|
const { dst } = this.addTemplate(template)
|
2017-05-11 18:11:00 +00:00
|
|
|
// Add to nuxt plugins
|
2017-06-06 12:51:30 +00:00
|
|
|
this.options.plugins.unshift({
|
2017-06-11 14:17:36 +00:00
|
|
|
src: path.join(this.options.buildDir, dst),
|
2017-05-21 19:00:41 +00:00
|
|
|
ssr: template.ssr
|
2017-05-11 18:11:00 +00:00
|
|
|
})
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
addServerMiddleware (middleware) {
|
2017-05-19 09:14:24 +00:00
|
|
|
this.options.serverMiddleware.push(middleware)
|
2017-05-12 10:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
extendBuild (fn) {
|
2017-05-12 07:57:24 +00:00
|
|
|
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
extendRoutes (fn) {
|
2017-05-12 07:57:24 +00:00
|
|
|
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 08:57:41 +00:00
|
|
|
requireModule (moduleOpts) {
|
2017-06-05 08:52:02 +00:00
|
|
|
// Require once
|
|
|
|
return this.addModule(moduleOpts, true)
|
2017-05-20 09:13:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 11:11:40 +00:00
|
|
|
async addModule (moduleOpts, requireOnce) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!moduleOpts) {
|
|
|
|
return
|
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
|
|
|
await this.applyPluginsAsync('add', {moduleOpts, requireOnce})
|
|
|
|
|
2017-05-31 14:54:53 +00:00
|
|
|
// Allow using babel style array options
|
2017-05-31 14:58:36 +00:00
|
|
|
if (Array.isArray(moduleOpts)) {
|
2017-05-31 14:54:53 +00:00
|
|
|
moduleOpts = {
|
|
|
|
src: moduleOpts[0],
|
|
|
|
options: moduleOpts[1]
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-05-11 10:59:08 +00:00
|
|
|
// Allows passing runtime options to each module
|
2017-05-31 14:54:53 +00:00
|
|
|
const options = moduleOpts.options || (typeof moduleOpts === 'object' ? moduleOpts : {})
|
2017-05-21 17:17:36 +00:00
|
|
|
const originalSrc = moduleOpts.src || moduleOpts
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-05-11 10:59:08 +00:00
|
|
|
// Resolve module
|
2017-05-24 13:02:33 +00:00
|
|
|
let module = originalSrc
|
2017-06-13 22:09:03 +00:00
|
|
|
if (typeof module === 'string') {
|
2017-06-29 16:36:22 +00:00
|
|
|
module = require(this.nuxt.resolvePath(module))
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-06-13 22:09:03 +00:00
|
|
|
|
2017-05-11 10:59:08 +00:00
|
|
|
// Validate module
|
2017-05-14 22:33:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-24 13:02:33 +00:00
|
|
|
if (typeof module !== 'function') {
|
2017-06-13 22:09:03 +00:00
|
|
|
throw new Error(`[nuxt] Module ${JSON.stringify(originalSrc)} should export a function`)
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-06-05 08:52:02 +00:00
|
|
|
// Module meta
|
|
|
|
if (!module.meta) {
|
|
|
|
module.meta = {}
|
|
|
|
}
|
|
|
|
if (module.meta.name) {
|
|
|
|
const alreadyRequired = this.requiredModules.indexOf(module.meta.name) !== -1
|
|
|
|
if (requireOnce && alreadyRequired) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!alreadyRequired) {
|
|
|
|
this.requiredModules.push(module.meta.name)
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-05-11 10:59:08 +00:00
|
|
|
// Call module with `this` context and pass options
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-11 19:04:50 +00:00
|
|
|
const result = module.call(this, options, err => {
|
2017-05-14 23:17:13 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
resolve(module)
|
|
|
|
})
|
2017-05-21 17:17:36 +00:00
|
|
|
// If module send back a promise
|
2017-05-11 19:04:50 +00:00
|
|
|
if (result && result.then instanceof Function) {
|
|
|
|
return result.then(resolve)
|
|
|
|
}
|
2017-05-21 17:17:36 +00:00
|
|
|
// If not expecting a callback but returns no promise (=synchronous)
|
|
|
|
if (module.length < 2) {
|
|
|
|
return resolve(module)
|
|
|
|
}
|
2017-05-11 10:59:08 +00:00
|
|
|
})
|
2017-05-11 10:04:15 +00:00
|
|
|
}
|
|
|
|
}
|