module.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module.exports = function NuxtLoadingScreen () {
  2. if (!this.options.dev) {
  3. return
  4. }
  5. const defu = require('defu')
  6. const LoadingUI = require('./loading')
  7. const { nuxt } = this
  8. const baseURL = nuxt.options.router.base + '_loading'
  9. const options = this.options.build.loadingScreen = defu(this.options.build.loadingScreen, {
  10. baseURL,
  11. baseURLAlt: baseURL,
  12. altPort: false,
  13. image: undefined,
  14. colors: {}
  15. })
  16. const loading = new LoadingUI(options)
  17. nuxt.options.serverMiddleware.push({
  18. path: '/_loading',
  19. handler: (req, res) => { loading.app(req, res) }
  20. })
  21. if (options.altPort) {
  22. nuxt.hook('listen', async (_, { url }) => {
  23. await loading.initAlt({ url })
  24. })
  25. }
  26. nuxt.hook('close', async () => {
  27. await loading.close()
  28. })
  29. nuxt.hook('bundler:progress', (states) => {
  30. loading.setStates(states)
  31. })
  32. nuxt.hook('cli:buildError', (error) => {
  33. loading.setError(error)
  34. })
  35. nuxt.hook('server:nuxt:renderLoading', (req, res) => {
  36. loading.serveIndex(req, res)
  37. })
  38. }