2018-03-16 16:12:06 +00:00
import path from 'path'
import fs from 'fs'
import hash from 'hash-sum'
2018-08-07 09:24:01 +00:00
import consola from 'consola'
2018-10-17 21:28:25 +00:00
2018-12-22 21:05:13 +00:00
import { chainFn , sequence } from '@nuxt/utils'
2017-05-11 10:59:08 +00:00
2018-03-16 16:12:06 +00:00
export default class ModuleContainer {
2019-07-10 10:45:49 +00:00
constructor ( nuxt ) {
2017-05-11 10:04:15 +00:00
this . nuxt = nuxt
2017-05-11 10:59:08 +00:00
this . options = nuxt . options
2018-01-11 13:28:45 +00:00
this . requiredModules = { }
2020-06-03 19:34:10 +00:00
// Self bind to allow destructre from container
for ( const method of Object . getOwnPropertyNames ( ModuleContainer . prototype ) ) {
if ( typeof this [ method ] === 'function' ) {
this [ method ] = this [ method ] . bind ( this )
}
}
2017-07-17 19:26:41 +00:00
}
2017-05-31 14:21:16 +00:00
2019-07-10 10:45:49 +00:00
async ready ( ) {
2018-01-11 13:28:45 +00:00
// Call before hook
2017-10-31 11:45:32 +00:00
await this . nuxt . callHook ( 'modules:before' , this , this . options . modules )
2018-01-11 13:28:45 +00:00
2019-08-09 10:32:53 +00:00
if ( this . options . buildModules && ! this . options . _start ) {
2019-03-03 07:49:46 +00:00
// Load every devModule in sequence
2020-06-03 19:34:10 +00:00
await sequence ( this . options . buildModules , this . addModule )
2019-03-03 07:49:46 +00:00
}
2017-10-30 17:41:22 +00:00
// Load every module in sequence
2020-06-03 19:34:10 +00:00
await sequence ( this . options . modules , this . addModule )
// Load ah-hoc modules last
await sequence ( this . options . _modules , this . addModule )
2018-01-11 13:28:45 +00:00
2017-10-30 21:39:08 +00:00
// Call done hook
await this . nuxt . callHook ( 'modules:done' , this )
2017-05-11 10:59:08 +00:00
}
2019-07-10 10:45:49 +00:00
addVendor ( ) {
2018-08-07 09:24:01 +00:00
consola . warn ( 'addVendor has been deprecated due to webpack4 optimization' )
2017-05-11 10:59:08 +00:00
}
2019-07-10 10:45:49 +00:00
addTemplate ( template ) {
2018-01-11 15:13:52 +00:00
if ( ! template ) {
2019-01-19 12:00:51 +00:00
throw new Error ( 'Invalid template: ' + JSON . stringify ( template ) )
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +00:00
2017-05-11 18:11:00 +00:00
// Validate & parse source
const src = template . src || template
const srcPath = path . parse ( src )
2019-01-19 12:00:51 +00:00
2018-08-03 15:06:18 +00:00
if ( typeof src !== 'string' || ! fs . existsSync ( src ) ) {
2019-01-19 12:00:51 +00:00
throw new Error ( 'Template src not found: ' + src )
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +00:00
2020-05-07 19:08:01 +00:00
// Mostly for DX, some people prefers `filename` vs `fileName`
const fileName = template . fileName || template . filename
// Generate unique and human readable dst filename if not provided
const dst = fileName || ` ${ 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
}
2018-01-11 13:28:45 +00:00
2017-05-11 18:47:53 +00:00
this . options . build . templates . push ( templateObj )
2020-05-07 19:08:01 +00:00
2017-05-11 18:11:00 +00:00
return templateObj
}
2019-07-10 10:45:49 +00:00
addPlugin ( template ) {
2017-06-11 14:17:36 +00:00
const { dst } = this . addTemplate ( template )
2018-01-11 13:28:45 +00:00
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 ) ,
2018-12-20 09:28:10 +00:00
// TODO: remove deprecated option in Nuxt 3
ssr : template . ssr ,
mode : template . mode
2017-05-11 18:11:00 +00:00
} )
2017-05-11 10:59:08 +00:00
}
2019-07-10 10:45:49 +00:00
addLayout ( template , name ) {
2018-02-07 11:58:48 +00:00
const { dst , src } = this . addTemplate ( template )
2019-01-09 10:57:46 +00:00
const layoutName = name || path . parse ( src ) . name
const layout = this . options . layouts [ layoutName ]
if ( layout ) {
consola . warn ( ` Duplicate layout registration, " ${ layoutName } " has been registered as " ${ layout } " ` )
}
2018-02-07 11:58:48 +00:00
2018-02-14 19:31:11 +00:00
// Add to nuxt layouts
2019-01-09 10:57:46 +00:00
this . options . layouts [ layoutName ] = ` ./ ${ dst } `
2018-09-02 09:22:10 +00:00
// If error layout, set ErrorPage
if ( name === 'error' ) {
this . addErrorLayout ( dst )
}
}
2019-07-10 10:45:49 +00:00
addErrorLayout ( dst ) {
2018-09-02 09:22:10 +00:00
const relativeBuildDir = path . relative ( this . options . rootDir , this . options . buildDir )
this . options . ErrorPage = ` ~/ ${ relativeBuildDir } / ${ dst } `
2018-02-07 11:58:48 +00:00
}
2019-07-10 10:45:49 +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
}
2019-07-10 10:45:49 +00:00
extendBuild ( fn ) {
2017-05-12 07:57:24 +00:00
this . options . build . extend = chainFn ( this . options . build . extend , fn )
}
2019-07-10 10:45:49 +00:00
extendRoutes ( fn ) {
2018-01-11 13:28:45 +00:00
this . options . router . extendRoutes = chainFn (
this . options . router . extendRoutes ,
fn
)
2017-05-11 10:59:08 +00:00
}
2019-07-10 10:45:49 +00:00
requireModule ( moduleOpts ) {
2020-05-18 11:58:48 +00:00
return this . addModule ( moduleOpts )
2017-05-20 09:13:55 +00:00
}
2020-05-18 11:58:48 +00:00
async addModule ( moduleOpts ) {
2018-01-11 13:28:45 +00:00
let src
let options
let handler
2019-02-05 11:35:42 +00:00
// Type 1: String or Function
if ( typeof moduleOpts === 'string' || typeof moduleOpts === 'function' ) {
2018-01-11 13:28:45 +00:00
src = moduleOpts
2018-01-11 15:13:52 +00:00
} else if ( Array . isArray ( moduleOpts ) ) {
// Type 2: Babel style array
2019-01-17 21:18:29 +00:00
[ src , options ] = moduleOpts
2018-01-11 15:13:52 +00:00
} else if ( typeof moduleOpts === 'object' ) {
// Type 3: Pure object
2019-01-17 21:18:29 +00:00
( { src , options , handler } = moduleOpts )
2018-01-11 13:28:45 +00:00
}
2017-07-03 11:11:40 +00:00
2019-02-05 11:35:42 +00:00
// Define handler if src is a function
if ( typeof src === 'function' ) {
handler = src
}
2019-08-09 10:32:53 +00:00
// Prevent adding buildModules-listed entries in production
if ( this . options . buildModules . includes ( handler ) && this . options . _start ) {
2019-03-03 07:49:46 +00:00
return
}
2018-01-11 13:28:45 +00:00
// Resolve handler
if ( ! handler ) {
2020-03-13 20:25:53 +00:00
try {
handler = this . nuxt . resolver . requireModule ( src , { useESM : true } )
} catch ( error ) {
if ( error . code !== 'MODULE_NOT_FOUND' ) {
throw error
}
let message = 'Module `{name}` not found.'
if ( this . options . buildModules . includes ( src ) ) {
message += ' Please ensure `{name}` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used.' . replace ( '{name}' , src )
} else if ( this . options . modules . includes ( src ) ) {
message += ' Please ensure `{name}` is in `dependencies` and installed.'
}
message = message . replace ( /{name}/g , src )
if ( this . options . _cli ) {
throw new Error ( message )
} else {
// TODO: Remove in next major version
2020-04-14 09:26:40 +00:00
message += ' Silently ignoring module as programmatic usage detected.'
2020-03-13 20:25:53 +00:00
consola . warn ( message )
return
}
}
2017-05-11 10:59:08 +00:00
}
2017-06-13 22:09:03 +00:00
2018-01-11 13:28:45 +00:00
// Validate handler
if ( typeof handler !== 'function' ) {
2019-07-10 10:45:49 +00:00
throw new TypeError ( 'Module should export a function: ' + src )
2017-05-11 10:59:08 +00:00
}
2017-07-03 11:11:40 +00:00
2020-05-18 11:58:48 +00:00
// Ensure module is required once
const key = ( handler . meta && handler . meta . name ) || src
2018-01-11 13:32:31 +00:00
if ( typeof key === 'string' ) {
2020-05-18 11:58:48 +00:00
if ( this . requiredModules [ key ] ) {
2017-06-05 08:52:02 +00:00
return
}
2018-01-11 13:28:45 +00:00
this . requiredModules [ key ] = { src , options , handler }
2017-06-05 08:52:02 +00:00
}
2017-07-03 11:11:40 +00:00
2018-01-13 06:15:00 +00:00
// Default module options to empty object
if ( options === undefined ) {
options = { }
}
2019-01-16 17:50:45 +00:00
const result = await handler . call ( this , options )
return result
2017-05-11 10:04:15 +00:00
}
}