mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-01 15:43:53 +00:00
refacto: Hooks
This commit is contained in:
parent
83815de91b
commit
6648695015
@ -57,6 +57,9 @@ export default class Builder extends Tapable {
|
|||||||
this.vueLoader = vueLoaderConfig.bind(this)
|
this.vueLoader = vueLoaderConfig.bind(this)
|
||||||
|
|
||||||
this._buildStatus = STATUS.INITIAL
|
this._buildStatus = STATUS.INITIAL
|
||||||
|
|
||||||
|
// Call class hook
|
||||||
|
this.nuxt.applyPlugins('builder', this)
|
||||||
}
|
}
|
||||||
|
|
||||||
get plugins() {
|
get plugins() {
|
||||||
@ -119,7 +122,7 @@ export default class Builder extends Tapable {
|
|||||||
await this.nuxt.ready()
|
await this.nuxt.ready()
|
||||||
|
|
||||||
// Wait for build plugins
|
// Wait for build plugins
|
||||||
await this.nuxt.applyPluginsAsync('build', this)
|
await this.applyPluginsAsync('build', this.options.build)
|
||||||
|
|
||||||
// Babel options
|
// Babel options
|
||||||
this.babelOptions = _.defaults(this.options.build.babel, {
|
this.babelOptions = _.defaults(this.options.build.babel, {
|
||||||
@ -177,11 +180,11 @@ export default class Builder extends Tapable {
|
|||||||
// Start webpack build
|
// Start webpack build
|
||||||
await this.webpackBuild()
|
await this.webpackBuild()
|
||||||
|
|
||||||
await this.applyPluginsAsync('built', this)
|
|
||||||
|
|
||||||
// Flag to set that building is done
|
// Flag to set that building is done
|
||||||
this._buildStatus = STATUS.BUILD_DONE
|
this._buildStatus = STATUS.BUILD_DONE
|
||||||
|
|
||||||
|
await this.applyPluginsAsync('built')
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +267,7 @@ export default class Builder extends Tapable {
|
|||||||
templateVars.router.routes = this.options.build.createRoutes(this.options.srcDir)
|
templateVars.router.routes = this.options.build.createRoutes(this.options.srcDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.applyPluginsAsync('extendRoutes', { routes: templateVars.router.routes, templateVars, r })
|
await this.applyPluginsAsync('extendRoutes', templateVars.router.routes)
|
||||||
|
|
||||||
// router.extendRoutes method
|
// router.extendRoutes method
|
||||||
if (typeof this.options.router.extendRoutes === 'function') {
|
if (typeof this.options.router.extendRoutes === 'function') {
|
||||||
@ -332,7 +335,7 @@ export default class Builder extends Tapable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.applyPluginsAsync('generate', { builder: this, templatesFiles, templateVars })
|
await this.applyPluginsAsync('generate', { templatesFiles, templateVars, resolve: r })
|
||||||
|
|
||||||
// Interpret and move template files to .nuxt/
|
// Interpret and move template files to .nuxt/
|
||||||
await Promise.all(templatesFiles.map(async ({ src, dst, options, custom }) => {
|
await Promise.all(templatesFiles.map(async ({ src, dst, options, custom }) => {
|
||||||
@ -372,7 +375,7 @@ export default class Builder extends Tapable {
|
|||||||
return utimes(path, dateFS, dateFS)
|
return utimes(path, dateFS, dateFS)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
await this.applyPluginsAsync('generated', this)
|
await this.applyPluginsAsync('generated')
|
||||||
}
|
}
|
||||||
|
|
||||||
async webpackBuild() {
|
async webpackBuild() {
|
||||||
@ -433,11 +436,11 @@ export default class Builder extends Tapable {
|
|||||||
// Start Builds
|
// Start Builds
|
||||||
await sequence(this.compilers, (compiler) => new Promise(async (resolve, reject) => {
|
await sequence(this.compilers, (compiler) => new Promise(async (resolve, reject) => {
|
||||||
const name = compiler.options.name
|
const name = compiler.options.name
|
||||||
await this.applyPluginsAsync('compile', { builder: this, compiler, name })
|
await this.applyPluginsAsync('compile', { name, compiler })
|
||||||
|
|
||||||
// Resolve only when compiler emit done event
|
// Resolve only when compiler emit done event
|
||||||
compiler.plugin('done', async (stats) => {
|
compiler.plugin('done', async (stats) => {
|
||||||
await this.applyPluginsAsync('compiled', { builder: this, compiler, name, stats })
|
await this.applyPluginsAsync('compiled', { name, compiler, stats })
|
||||||
process.nextTick(resolve)
|
process.nextTick(resolve)
|
||||||
})
|
})
|
||||||
// --- Dev Build ---
|
// --- Dev Build ---
|
||||||
|
@ -20,6 +20,9 @@ export default class Generator extends Tapable {
|
|||||||
this.srcBuiltPath = resolve(this.options.buildDir, 'dist')
|
this.srcBuiltPath = resolve(this.options.buildDir, 'dist')
|
||||||
this.distPath = resolve(this.options.rootDir, this.options.generate.dir)
|
this.distPath = resolve(this.options.rootDir, this.options.generate.dir)
|
||||||
this.distNuxtPath = join(this.distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
this.distNuxtPath = join(this.distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
||||||
|
|
||||||
|
// Call class hook
|
||||||
|
this.nuxt.applyPlugins('generator', this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async generate({ build = true, init = true } = {}) {
|
async generate({ build = true, init = true } = {}) {
|
||||||
@ -50,10 +53,8 @@ export default class Generator extends Tapable {
|
|||||||
try {
|
try {
|
||||||
console.log('Generating routes') // eslint-disable-line no-console
|
console.log('Generating routes') // eslint-disable-line no-console
|
||||||
generateRoutes = await promisifyRoute(this.options.generate.routes || [])
|
generateRoutes = await promisifyRoute(this.options.generate.routes || [])
|
||||||
await this.applyPluginsAsync('generateRoutes', { generator: this, generateRoutes })
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Could not resolve routes') // eslint-disable-line no-console
|
console.error('Could not resolve routes') // eslint-disable-line no-console
|
||||||
console.error(e) // eslint-disable-line no-console
|
|
||||||
throw e // eslint-disable-line no-unreachable
|
throw e // eslint-disable-line no-unreachable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,7 +63,7 @@ export default class Generator extends Tapable {
|
|||||||
let routes = (this.options.router.mode === 'hash') ? ['/'] : flatRoutes(this.options.router.routes)
|
let routes = (this.options.router.mode === 'hash') ? ['/'] : flatRoutes(this.options.router.routes)
|
||||||
routes = this.decorateWithPayloads(routes, generateRoutes)
|
routes = this.decorateWithPayloads(routes, generateRoutes)
|
||||||
|
|
||||||
await this.applyPluginsAsync('generate', { generator: this, routes })
|
await this.applyPluginsAsync('generate', routes)
|
||||||
|
|
||||||
// Start generate process
|
// Start generate process
|
||||||
while (routes.length) {
|
while (routes.length) {
|
||||||
@ -86,6 +87,8 @@ export default class Generator extends Tapable {
|
|||||||
const duration = Math.round((Date.now() - s) / 100) / 10
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
||||||
debug(`HTML Files generated in ${duration}s`)
|
debug(`HTML Files generated in ${duration}s`)
|
||||||
|
|
||||||
|
await this.applyPluginsAsync('generated', errors)
|
||||||
|
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
const report = errors.map(({ type, route, error }) => {
|
const report = errors.map(({ type, route, error }) => {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
@ -98,8 +101,6 @@ export default class Generator extends Tapable {
|
|||||||
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.applyPluginsAsync('generated', this)
|
|
||||||
|
|
||||||
return { duration, errors }
|
return { duration, errors }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +149,7 @@ export default class Generator extends Tapable {
|
|||||||
})
|
})
|
||||||
// Fill routeMap with given generate.routes
|
// Fill routeMap with given generate.routes
|
||||||
generateRoutes.forEach((route) => {
|
generateRoutes.forEach((route) => {
|
||||||
// route is either a string or like {route : "/my_route/1"}
|
// route is either a string or like { route : '/my_route/1', payload: {} }
|
||||||
const path = _.isString(route) ? route : route.route
|
const path = _.isString(route) ? route : route.route
|
||||||
routeMap[path] = {
|
routeMap[path] = {
|
||||||
route: path,
|
route: path,
|
||||||
|
@ -14,11 +14,16 @@ export default class ModuleContainer extends Tapable {
|
|||||||
this.nuxt = nuxt
|
this.nuxt = nuxt
|
||||||
this.options = nuxt.options
|
this.options = nuxt.options
|
||||||
this.requiredModules = []
|
this.requiredModules = []
|
||||||
|
|
||||||
|
// Call class hook
|
||||||
|
console.log('call module container')
|
||||||
|
this.nuxt.applyPlugins('moduleContainer', this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async _ready () {
|
async ready() {
|
||||||
|
// Load every module in sequence
|
||||||
await sequence(this.options.modules, this.addModule.bind(this))
|
await sequence(this.options.modules, this.addModule.bind(this))
|
||||||
await this.applyPluginsAsync('ready', this)
|
await this.applyPluginsAsync('ready')
|
||||||
}
|
}
|
||||||
|
|
||||||
addVendor(vendor) {
|
addVendor(vendor) {
|
||||||
@ -88,8 +93,6 @@ export default class ModuleContainer extends Tapable {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.applyPluginsAsync('add', {moduleOpts, requireOnce})
|
|
||||||
|
|
||||||
// Allow using babel style array options
|
// Allow using babel style array options
|
||||||
if (Array.isArray(moduleOpts)) {
|
if (Array.isArray(moduleOpts)) {
|
||||||
moduleOpts = {
|
moduleOpts = {
|
||||||
@ -129,13 +132,13 @@ export default class ModuleContainer extends Tapable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call module with `this` context and pass options
|
// Call module with `this` context and pass options
|
||||||
return new Promise((resolve, reject) => {
|
const m = await new Promise((resolve, reject) => {
|
||||||
const result = module.call(this, options, err => {
|
const result = module.call(this, options, (err, m) => {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
resolve(module)
|
resolve(m)
|
||||||
})
|
})
|
||||||
// If module send back a promise
|
// If module send back a promise
|
||||||
if (result && result.then instanceof Function) {
|
if (result && result.then instanceof Function) {
|
||||||
@ -146,5 +149,6 @@ export default class ModuleContainer extends Tapable {
|
|||||||
return resolve(module)
|
return resolve(module)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
await this.applyPluginsAsync('module', { meta: module.meta, module: m })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,10 @@ const debug = Debug('nuxt:')
|
|||||||
debug.color = 5
|
debug.color = 5
|
||||||
|
|
||||||
export default class Nuxt extends Tapable {
|
export default class Nuxt extends Tapable {
|
||||||
constructor (_options = {}) {
|
constructor(options = {}) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.options = Options.from(_options)
|
this.options = Options.from(options)
|
||||||
|
|
||||||
// Paths for resolving requires from `rootDir`
|
// Paths for resolving requires from `rootDir`
|
||||||
this.nodeModulePaths = Module._nodeModulePaths(this.options.rootDir)
|
this.nodeModulePaths = Module._nodeModulePaths(this.options.rootDir)
|
||||||
@ -40,9 +40,9 @@ export default class Nuxt extends Tapable {
|
|||||||
return this._ready
|
return this._ready
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.moduleContainer._ready()
|
await this.moduleContainer.ready()
|
||||||
await this.applyPluginsAsync('ready')
|
await this.applyPluginsAsync('ready')
|
||||||
await this.renderer._ready()
|
await this.renderer.ready()
|
||||||
|
|
||||||
this.initialized = true
|
this.initialized = true
|
||||||
return this
|
return this
|
||||||
|
@ -49,11 +49,12 @@ export default class Renderer extends Tapable {
|
|||||||
spaTemplate: null,
|
spaTemplate: null,
|
||||||
errorTemplate: parseTemplate('Nuxt.js Internal Server Error')
|
errorTemplate: parseTemplate('Nuxt.js Internal Server Error')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call class hook
|
||||||
|
this.nuxt.applyPlugins('renderer', this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async _ready () {
|
async ready() {
|
||||||
await this.nuxt.applyPluginsAsync('renderer', this)
|
|
||||||
|
|
||||||
// Setup nuxt middleware
|
// Setup nuxt middleware
|
||||||
await this.setupMiddleware()
|
await this.setupMiddleware()
|
||||||
|
|
||||||
@ -63,7 +64,7 @@ export default class Renderer extends Tapable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call ready plugin
|
// Call ready plugin
|
||||||
await this.applyPluginsAsync('ready', this)
|
await this.applyPluginsAsync('ready')
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadResources(_fs = fs) {
|
async loadResources(_fs = fs) {
|
||||||
@ -283,6 +284,7 @@ export default class Renderer extends Tapable {
|
|||||||
async nuxtMiddleware(req, res, next) {
|
async nuxtMiddleware(req, res, next) {
|
||||||
// Get context
|
// Get context
|
||||||
const context = getContext(req, res)
|
const context = getContext(req, res)
|
||||||
|
|
||||||
res.statusCode = 200
|
res.statusCode = 200
|
||||||
try {
|
try {
|
||||||
const { html, error, redirected, resourceHints } = await this.renderRoute(req.url, context)
|
const { html, error, redirected, resourceHints } = await this.renderRoute(req.url, context)
|
||||||
@ -563,8 +565,7 @@ export default class Renderer extends Tapable {
|
|||||||
runScripts: 'dangerously',
|
runScripts: 'dangerously',
|
||||||
beforeParse(window) {
|
beforeParse(window) {
|
||||||
// Mock window.scrollTo
|
// Mock window.scrollTo
|
||||||
window.scrollTo = () => {
|
window.scrollTo = () => {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (opts.virtualConsole !== false) {
|
if (opts.virtualConsole !== false) {
|
||||||
|
Loading…
Reference in New Issue
Block a user