2018-03-16 19:52:17 +00:00
|
|
|
import path from 'path'
|
2020-05-07 19:08:01 +00:00
|
|
|
import chalk from 'chalk'
|
2018-03-16 16:12:06 +00:00
|
|
|
import chokidar from 'chokidar'
|
2018-10-24 16:55:18 +00:00
|
|
|
import consola from 'consola'
|
2018-03-16 19:11:24 +00:00
|
|
|
import fsExtra from 'fs-extra'
|
2018-10-24 16:55:18 +00:00
|
|
|
import Glob from 'glob'
|
2018-03-16 16:12:06 +00:00
|
|
|
import hash from 'hash-sum'
|
2018-10-24 16:55:18 +00:00
|
|
|
import pify from 'pify'
|
2018-03-16 19:52:17 +00:00
|
|
|
import upath from 'upath'
|
2019-01-02 14:17:19 +00:00
|
|
|
import semver from 'semver'
|
2018-10-24 16:55:18 +00:00
|
|
|
|
|
|
|
import debounce from 'lodash/debounce'
|
|
|
|
import omit from 'lodash/omit'
|
|
|
|
import template from 'lodash/template'
|
|
|
|
import uniq from 'lodash/uniq'
|
|
|
|
import uniqBy from 'lodash/uniqBy'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
import {
|
|
|
|
r,
|
|
|
|
createRoutes,
|
|
|
|
relativeTo,
|
|
|
|
waitFor,
|
2018-10-24 13:46:06 +00:00
|
|
|
determineGlobals,
|
2018-10-25 10:57:52 +00:00
|
|
|
stripWhitespace,
|
2019-05-10 13:03:07 +00:00
|
|
|
isIndexFileAndFolder,
|
2020-05-07 19:08:01 +00:00
|
|
|
scanRequireTree,
|
|
|
|
TARGETS,
|
|
|
|
isFullStatic
|
2018-12-22 21:05:13 +00:00
|
|
|
} from '@nuxt/utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2019-01-29 09:31:14 +00:00
|
|
|
import Ignore from './ignore'
|
2019-01-30 16:54:26 +00:00
|
|
|
import BuildContext from './context/build'
|
|
|
|
import TemplateContext from './context/template'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-06-01 12:24:37 +00:00
|
|
|
const glob = pify(Glob)
|
2018-03-16 16:12:06 +00:00
|
|
|
export default class Builder {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (nuxt, bundleBuilder) {
|
2017-06-11 14:17:36 +00:00
|
|
|
this.nuxt = nuxt
|
2018-10-24 16:55:18 +00:00
|
|
|
this.plugins = []
|
2017-06-11 14:17:36 +00:00
|
|
|
this.options = nuxt.options
|
2018-10-09 12:07:23 +00:00
|
|
|
this.globals = determineGlobals(nuxt.options.globalName, nuxt.options.globals)
|
2018-08-10 10:37:05 +00:00
|
|
|
this.watchers = {
|
|
|
|
files: null,
|
|
|
|
custom: null,
|
|
|
|
restart: null
|
|
|
|
}
|
2018-03-23 07:34:55 +00:00
|
|
|
|
2019-07-24 07:26:44 +00:00
|
|
|
this.supportedExtensions = ['vue', 'js', ...(this.options.build.additionalExtensions || [])]
|
2018-12-15 06:55:08 +00:00
|
|
|
|
2017-06-29 16:36:22 +00:00
|
|
|
// Helper to resolve build paths
|
2019-02-08 16:25:11 +00:00
|
|
|
this.relativeToBuild = (...args) => relativeTo(this.options.buildDir, ...args)
|
2017-06-29 16:36:22 +00:00
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
this._buildStatus = STATUS.INITIAL
|
2017-11-22 12:57:39 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
// Hooks for watch lifecycle
|
2017-11-24 03:43:01 +00:00
|
|
|
if (this.options.dev) {
|
2018-11-08 09:15:56 +00:00
|
|
|
// Start watching after initial render
|
|
|
|
this.nuxt.hook('build:done', () => {
|
|
|
|
consola.info('Waiting for file changes')
|
|
|
|
this.watchClient()
|
2018-12-09 10:42:22 +00:00
|
|
|
this.watchRestart()
|
2018-11-08 09:15:56 +00:00
|
|
|
})
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
// Enable HMR for serverMiddleware
|
|
|
|
this.serverMiddlewareHMR()
|
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Close hook
|
|
|
|
this.nuxt.hook('close', () => this.close())
|
2018-03-22 19:29:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 16:11:20 +00:00
|
|
|
if (this.options.build.analyze) {
|
|
|
|
this.nuxt.hook('build:done', () => {
|
2020-02-24 23:16:43 +00:00
|
|
|
consola.warn('Notice: Please do not deploy bundles built with "analyze" mode, they\'re for analysis purposes only.')
|
2018-09-30 16:11:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
// Resolve template
|
2018-10-30 20:42:53 +00:00
|
|
|
this.template = this.options.build.template || '@nuxt/vue-app'
|
2018-10-17 21:28:25 +00:00
|
|
|
if (typeof this.template === 'string') {
|
2019-01-02 14:17:19 +00:00
|
|
|
this.template = this.nuxt.resolver.requireModule(this.template).template
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Create a new bundle builder
|
2018-10-24 16:55:18 +00:00
|
|
|
this.bundleBuilder = this.getBundleBuilder(bundleBuilder)
|
2019-03-08 20:43:23 +00:00
|
|
|
|
2019-01-29 09:31:14 +00:00
|
|
|
this.ignore = new Ignore({
|
2020-03-27 10:53:23 +00:00
|
|
|
rootDir: this.options.srcDir,
|
|
|
|
ignoreArray: this.options.ignore
|
2019-01-29 09:31:14 +00:00
|
|
|
})
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
getBundleBuilder (BundleBuilder) {
|
2018-10-25 11:22:31 +00:00
|
|
|
if (typeof BundleBuilder === 'object') {
|
|
|
|
return BundleBuilder
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
2018-10-25 11:22:31 +00:00
|
|
|
|
|
|
|
const context = new BuildContext(this)
|
|
|
|
|
|
|
|
if (typeof BundleBuilder !== 'function') {
|
2019-01-17 21:18:29 +00:00
|
|
|
({ BundleBuilder } = require('@nuxt/webpack'))
|
2018-10-25 11:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new BundleBuilder(context)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
forGenerate () {
|
2020-05-07 19:08:01 +00:00
|
|
|
this.options.target = TARGETS.static
|
2018-10-24 16:55:18 +00:00
|
|
|
this.bundleBuilder.forGenerate()
|
2017-08-17 12:43:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async build () {
|
2017-06-18 15:44:01 +00:00
|
|
|
// Avoid calling build() method multiple times when dev:true
|
|
|
|
if (this._buildStatus === STATUS.BUILD_DONE && this.options.dev) {
|
2017-06-11 14:17:36 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
// If building
|
|
|
|
if (this._buildStatus === STATUS.BUILDING) {
|
2017-11-24 08:09:45 +00:00
|
|
|
await waitFor(1000)
|
|
|
|
return this.build()
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
this._buildStatus = STATUS.BUILDING
|
2017-06-13 17:58:04 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
if (this.options.dev) {
|
|
|
|
consola.info('Preparing project for development')
|
|
|
|
consola.info('Initial build may take a while')
|
|
|
|
} else {
|
|
|
|
consola.info('Production build')
|
2020-05-07 19:08:01 +00:00
|
|
|
if (this.options.render.ssr) {
|
|
|
|
consola.info(`Bundling for ${chalk.bold.yellow('server')} and ${chalk.bold.green('client')} side`)
|
|
|
|
} else {
|
|
|
|
consola.info(`Bundling only for ${chalk.bold.green('client')} side`)
|
|
|
|
}
|
|
|
|
const target = isFullStatic(this.options) ? 'full static' : this.options.target
|
|
|
|
consola.info(`Target: ${chalk.bold.cyan(target)}`)
|
2018-11-08 09:15:56 +00:00
|
|
|
}
|
2018-03-12 15:16:08 +00:00
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
// Wait for nuxt ready
|
|
|
|
await this.nuxt.ready()
|
|
|
|
|
2017-10-30 21:39:08 +00:00
|
|
|
// Call before hook
|
|
|
|
await this.nuxt.callHook('build:before', this, this.options.build)
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2019-01-21 18:40:37 +00:00
|
|
|
await this.validatePages()
|
2017-06-13 17:58:04 +00:00
|
|
|
|
2019-01-02 14:17:19 +00:00
|
|
|
// Validate template
|
|
|
|
try {
|
|
|
|
this.validateTemplate()
|
|
|
|
} catch (err) {
|
|
|
|
consola.fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.success('Builder initialized')
|
2018-03-16 06:26:23 +00:00
|
|
|
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.debug(`App root: ${this.options.srcDir}`)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2020-05-13 14:46:13 +00:00
|
|
|
// Create or empty .nuxt/, .nuxt/components and .nuxt/dist folders
|
|
|
|
await fsExtra.emptyDir(r(this.options.buildDir))
|
2018-08-24 18:20:57 +00:00
|
|
|
const buildDirs = [r(this.options.buildDir, 'components')]
|
2017-06-11 14:17:36 +00:00
|
|
|
if (!this.options.dev) {
|
2018-08-24 18:20:57 +00:00
|
|
|
buildDirs.push(
|
|
|
|
r(this.options.buildDir, 'dist', 'client'),
|
|
|
|
r(this.options.buildDir, 'dist', 'server')
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2020-05-13 14:46:13 +00:00
|
|
|
await Promise.all(buildDirs.map(dir => fsExtra.emptyDir(dir)))
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2019-09-02 13:26:21 +00:00
|
|
|
// Call ready hook
|
|
|
|
await this.nuxt.callHook('builder:prepared', this, this.options.build)
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Generate routes and interpret the template files
|
|
|
|
await this.generateRoutesAndFiles()
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2019-09-25 15:11:39 +00:00
|
|
|
// Add vue-app template dir to watchers
|
|
|
|
this.options.build.watch.push(this.globPathWithExtensions(this.template.dir))
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
await this.resolvePlugins()
|
|
|
|
|
|
|
|
// Start bundle build: webpack, rollup, parcel...
|
|
|
|
await this.bundleBuilder.build()
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Flag to set that building is done
|
|
|
|
this._buildStatus = STATUS.BUILD_DONE
|
|
|
|
|
2017-10-30 21:39:08 +00:00
|
|
|
// Call done hook
|
|
|
|
await this.nuxt.callHook('build:done', this)
|
2017-10-30 17:41:22 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:40:37 +00:00
|
|
|
// Check if pages dir exists and warn if not
|
2019-07-10 10:45:49 +00:00
|
|
|
async validatePages () {
|
2019-01-21 18:40:37 +00:00
|
|
|
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
|
|
|
|
|
|
|
|
if (
|
|
|
|
!this._nuxtPages ||
|
|
|
|
await fsExtra.exists(path.join(this.options.srcDir, this.options.dir.pages))
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const dir = this.options.srcDir
|
|
|
|
if (await fsExtra.exists(path.join(this.options.srcDir, '..', this.options.dir.pages))) {
|
|
|
|
throw new Error(
|
|
|
|
`No \`${this.options.dir.pages}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this._defaultPage = true
|
|
|
|
consola.warn(`No \`${this.options.dir.pages}\` directory found in ${dir}. Using the default built-in page.`)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
validateTemplate () {
|
2019-01-02 14:17:19 +00:00
|
|
|
// Validate template dependencies
|
|
|
|
const templateDependencies = this.template.dependencies
|
|
|
|
for (const depName in templateDependencies) {
|
|
|
|
const depVersion = templateDependencies[depName]
|
|
|
|
|
|
|
|
// Load installed version
|
|
|
|
const pkg = this.nuxt.resolver.requireModule(path.join(depName, 'package.json'))
|
|
|
|
if (pkg) {
|
|
|
|
const validVersion = semver.satisfies(pkg.version, depVersion)
|
|
|
|
if (!validVersion) {
|
2019-05-14 13:35:31 +00:00
|
|
|
consola.warn(`${depName}@${depVersion} is recommended but ${depName}@${pkg.version} is installed!`)
|
2019-01-02 14:17:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
consola.warn(`${depName}@${depVersion} is required but not installed!`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
globPathWithExtensions (path) {
|
2019-05-27 09:06:53 +00:00
|
|
|
return `${path}/**/*.{${this.supportedExtensions.join(',')}}`
|
|
|
|
}
|
|
|
|
|
2019-08-25 12:31:17 +00:00
|
|
|
createTemplateContext () {
|
|
|
|
return new TemplateContext(this, this.options)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async generateRoutesAndFiles () {
|
2019-01-30 16:54:26 +00:00
|
|
|
consola.debug('Generating nuxt files')
|
2018-03-16 06:32:07 +00:00
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
this.plugins = Array.from(await this.normalizePlugins())
|
2018-08-10 14:59:15 +00:00
|
|
|
|
2019-08-25 12:31:17 +00:00
|
|
|
const templateContext = this.createTemplateContext()
|
2019-01-30 16:54:26 +00:00
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
this.resolveLayouts(templateContext),
|
|
|
|
this.resolveRoutes(templateContext),
|
|
|
|
this.resolveStore(templateContext),
|
|
|
|
this.resolveMiddleware(templateContext)
|
|
|
|
])
|
|
|
|
|
2019-09-05 15:15:27 +00:00
|
|
|
this.addOptionalTemplates(templateContext)
|
|
|
|
|
2019-01-30 16:54:26 +00:00
|
|
|
await this.resolveCustomTemplates(templateContext)
|
|
|
|
|
|
|
|
await this.resolveLoadingIndicator(templateContext)
|
|
|
|
|
|
|
|
await this.compileTemplates(templateContext)
|
|
|
|
|
|
|
|
consola.success('Nuxt files generated')
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
async normalizePlugins () {
|
|
|
|
// options.extendPlugins allows for returning a new plugins array
|
|
|
|
if (typeof this.options.extendPlugins === 'function') {
|
|
|
|
const extendedPlugins = this.options.extendPlugins(this.options.plugins)
|
|
|
|
|
|
|
|
if (Array.isArray(extendedPlugins)) {
|
|
|
|
this.options.plugins = extendedPlugins
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extendPlugins hook only supports in-place modifying
|
|
|
|
await this.nuxt.callHook('builder:extendPlugins', this.options.plugins)
|
|
|
|
|
2019-03-07 19:33:20 +00:00
|
|
|
const modes = ['client', 'server']
|
2019-05-11 09:23:37 +00:00
|
|
|
const modePattern = new RegExp(`\\.(${modes.join('|')})(\\.\\w+)*$`)
|
2019-01-30 16:54:26 +00:00
|
|
|
return uniqBy(
|
|
|
|
this.options.plugins.map((p) => {
|
|
|
|
if (typeof p === 'string') {
|
|
|
|
p = { src: p }
|
|
|
|
}
|
|
|
|
const pluginBaseName = path.basename(p.src, path.extname(p.src)).replace(
|
|
|
|
/[^a-zA-Z?\d\s:]/g,
|
|
|
|
''
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-01-30 16:54:26 +00:00
|
|
|
if (p.ssr === false) {
|
|
|
|
p.mode = 'client'
|
|
|
|
} else if (p.mode === undefined) {
|
|
|
|
p.mode = 'all'
|
2019-03-07 19:33:20 +00:00
|
|
|
p.src.replace(modePattern, (_, mode) => {
|
|
|
|
if (modes.includes(mode)) {
|
|
|
|
p.mode = mode
|
|
|
|
}
|
|
|
|
})
|
2019-01-30 16:54:26 +00:00
|
|
|
} else if (!['client', 'server', 'all'].includes(p.mode)) {
|
|
|
|
consola.warn(`Invalid plugin mode (server/client/all): '${p.mode}'. Falling back to 'all'`)
|
|
|
|
p.mode = 'all'
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
src: this.nuxt.resolver.resolveAlias(p.src),
|
|
|
|
mode: p.mode,
|
|
|
|
name: 'nuxt_plugin_' + pluginBaseName + '_' + hash(p.src)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
p => p.name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:15:27 +00:00
|
|
|
addOptionalTemplates (templateContext) {
|
|
|
|
if (this.options.build.indicator) {
|
|
|
|
templateContext.templateFiles.push('components/nuxt-build-indicator.vue')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.loading !== false) {
|
|
|
|
templateContext.templateFiles.push('components/nuxt-loading.vue')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveFiles (dir, cwd = this.options.srcDir) {
|
2019-05-27 09:06:53 +00:00
|
|
|
return this.ignore.filter(await glob(this.globPathWithExtensions(dir), {
|
2019-01-30 16:54:26 +00:00
|
|
|
cwd,
|
2019-09-29 09:06:44 +00:00
|
|
|
follow: this.options.build.followSymlinks
|
2019-01-30 16:54:26 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveRelative (dir) {
|
2019-01-30 16:54:26 +00:00
|
|
|
const dirPrefix = new RegExp(`^${dir}/`)
|
|
|
|
return (await this.resolveFiles(dir)).map(file => ({ src: file.replace(dirPrefix, '') }))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveLayouts ({ templateVars, templateFiles }) {
|
2019-09-05 15:15:27 +00:00
|
|
|
if (!this.options.features.layouts) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:34:04 +00:00
|
|
|
if (await fsExtra.exists(path.resolve(this.options.srcDir, this.options.dir.layouts))) {
|
2019-01-30 16:54:26 +00:00
|
|
|
for (const file of await this.resolveFiles(this.options.dir.layouts)) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const name = file
|
2018-10-16 21:55:28 +00:00
|
|
|
.replace(new RegExp(`^${this.options.dir.layouts}/`), '')
|
2018-12-15 06:55:08 +00:00
|
|
|
.replace(new RegExp(`\\.(${this.supportedExtensions.join('|')})$`), '')
|
2019-01-30 16:54:26 +00:00
|
|
|
|
|
|
|
// Layout Priority: module.addLayout > .vue file > other extensions
|
2017-08-14 12:15:00 +00:00
|
|
|
if (name === 'error') {
|
2018-09-02 09:22:10 +00:00
|
|
|
if (!templateVars.components.ErrorPage) {
|
|
|
|
templateVars.components.ErrorPage = this.relativeToBuild(
|
|
|
|
this.options.srcDir,
|
|
|
|
file
|
|
|
|
)
|
|
|
|
}
|
2019-01-30 16:54:26 +00:00
|
|
|
} else if (this.options.layouts[name]) {
|
|
|
|
consola.warn(`Duplicate layout registration, "${name}" has been registered as "${this.options.layouts[name]}"`)
|
2019-01-09 10:57:46 +00:00
|
|
|
} else if (!templateVars.layouts[name] || /\.vue$/.test(file)) {
|
2018-01-13 05:22:11 +00:00
|
|
|
templateVars.layouts[name] = this.relativeToBuild(
|
|
|
|
this.options.srcDir,
|
|
|
|
file
|
|
|
|
)
|
2017-12-12 13:32:45 +00:00
|
|
|
}
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2019-01-30 16:54:26 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// If no default layout, create its folder and add the default folder
|
|
|
|
if (!templateVars.layouts.default) {
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.mkdirp(r(this.options.buildDir, 'layouts'))
|
2019-01-30 16:54:26 +00:00
|
|
|
templateFiles.push('layouts/default.vue')
|
2017-06-20 17:12:06 +00:00
|
|
|
templateVars.layouts.default = './layouts/default.vue'
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveRoutes ({ templateVars }) {
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.debug('Generating routes...')
|
2019-09-01 14:37:25 +00:00
|
|
|
const { routeNameSplitter, trailingSlash } = this.options.router
|
2018-04-06 11:27:43 +00:00
|
|
|
|
|
|
|
if (this._defaultPage) {
|
2019-06-04 19:48:01 +00:00
|
|
|
templateVars.router.routes = createRoutes({
|
|
|
|
files: ['index.vue'],
|
|
|
|
srcDir: this.template.dir + '/pages',
|
2019-09-01 14:37:25 +00:00
|
|
|
routeNameSplitter,
|
|
|
|
trailingSlash
|
2019-06-04 19:48:01 +00:00
|
|
|
})
|
2018-11-03 12:25:41 +00:00
|
|
|
} else if (this._nuxtPages) {
|
2017-06-11 14:17:36 +00:00
|
|
|
// Use nuxt.js createRoutes bases on pages/
|
2018-01-13 05:22:11 +00:00
|
|
|
const files = {}
|
2019-01-29 09:31:14 +00:00
|
|
|
const ext = new RegExp(`\\.(${this.supportedExtensions.join('|')})$`)
|
|
|
|
for (const page of await this.resolveFiles(this.options.dir.pages)) {
|
|
|
|
const key = page.replace(ext, '')
|
2019-01-07 12:14:51 +00:00
|
|
|
// .vue file takes precedence over other extensions
|
2019-01-29 09:31:14 +00:00
|
|
|
if (/\.vue$/.test(page) || !files[key]) {
|
|
|
|
files[key] = page.replace(/(['"])/g, '\\$1')
|
2017-12-12 13:32:45 +00:00
|
|
|
}
|
2019-01-29 09:31:14 +00:00
|
|
|
}
|
2019-06-04 19:48:01 +00:00
|
|
|
templateVars.router.routes = createRoutes({
|
|
|
|
files: Object.values(files),
|
|
|
|
srcDir: this.options.srcDir,
|
|
|
|
pagesDir: this.options.dir.pages,
|
2019-09-01 14:37:25 +00:00
|
|
|
routeNameSplitter,
|
|
|
|
supportedExtensions: this.supportedExtensions,
|
|
|
|
trailingSlash
|
2019-06-04 19:48:01 +00:00
|
|
|
})
|
2018-11-03 12:25:41 +00:00
|
|
|
} else { // If user defined a custom method to create routes
|
2019-09-04 10:00:58 +00:00
|
|
|
templateVars.router.routes = await this.options.build.createRoutes(
|
2018-01-13 05:22:11 +00:00
|
|
|
this.options.srcDir
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
await this.nuxt.callHook(
|
|
|
|
'build:extendRoutes',
|
|
|
|
templateVars.router.routes,
|
|
|
|
r
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
// router.extendRoutes method
|
|
|
|
if (typeof this.options.router.extendRoutes === 'function') {
|
|
|
|
// let the user extend the routes
|
2018-01-13 05:22:11 +00:00
|
|
|
const extendedRoutes = this.options.router.extendRoutes(
|
|
|
|
templateVars.router.routes,
|
|
|
|
r
|
|
|
|
)
|
2017-08-21 20:01:41 +00:00
|
|
|
// Only overwrite routes when something is returned for backwards compatibility
|
|
|
|
if (extendedRoutes !== undefined) {
|
|
|
|
templateVars.router.routes = extendedRoutes
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 07:17:56 +00:00
|
|
|
// Make routes accessible for other modules and webpack configs
|
|
|
|
this.routes = templateVars.router.routes
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2017-08-18 07:17:56 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveStore ({ templateVars, templateFiles }) {
|
2017-06-11 14:17:36 +00:00
|
|
|
// Add store if needed
|
2019-09-05 15:15:27 +00:00
|
|
|
if (!this.options.features.store || !this.options.store) {
|
2019-02-08 16:25:11 +00:00
|
|
|
return
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
|
|
|
templateVars.storeModules = (await this.resolveRelative(this.options.dir.store))
|
|
|
|
.sort(({ src: p1 }, { src: p2 }) => {
|
|
|
|
// modules are sorted from low to high priority (for overwriting properties)
|
|
|
|
let res = p1.split('/').length - p2.split('/').length
|
|
|
|
if (res === 0 && p1.includes('/index.')) {
|
|
|
|
res = -1
|
|
|
|
} else if (res === 0 && p2.includes('/index.')) {
|
|
|
|
res = 1
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
|
|
|
|
templateFiles.push('store.js')
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-09-05 15:15:27 +00:00
|
|
|
async resolveMiddleware ({ templateVars, templateFiles }) {
|
|
|
|
if (!this.options.features.middleware) {
|
|
|
|
return
|
|
|
|
}
|
2019-09-02 10:53:52 +00:00
|
|
|
|
2019-09-05 15:15:27 +00:00
|
|
|
const middleware = await this.resolveRelative(this.options.dir.middleware)
|
2019-09-02 10:53:52 +00:00
|
|
|
const extRE = new RegExp(`\\.(${this.supportedExtensions.join('|')})$`)
|
|
|
|
templateVars.middleware = middleware.map(({ src }) => {
|
|
|
|
const name = src.replace(extRE, '')
|
|
|
|
const dst = this.relativeToBuild(this.options.srcDir, this.options.dir.middleware, src)
|
|
|
|
return { name, src, dst }
|
|
|
|
})
|
2019-09-05 15:15:27 +00:00
|
|
|
|
|
|
|
templateFiles.push('middleware.js')
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2019-01-29 09:31:14 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveCustomTemplates (templateContext) {
|
2019-07-24 11:35:50 +00:00
|
|
|
// Sanitize custom template files
|
|
|
|
this.options.build.templates = this.options.build.templates.map((t) => {
|
|
|
|
const src = t.src || t
|
2019-08-24 17:10:39 +00:00
|
|
|
return {
|
|
|
|
src: r(this.options.srcDir, src),
|
|
|
|
dst: t.dst || path.basename(src),
|
|
|
|
custom: true,
|
|
|
|
...(typeof t === 'object' ? t : undefined)
|
|
|
|
}
|
2019-07-24 11:35:50 +00:00
|
|
|
})
|
2019-08-24 17:10:39 +00:00
|
|
|
|
|
|
|
const customTemplateFiles = this.options.build.templates.map(t => t.dst || path.basename(t.src || t))
|
|
|
|
|
2019-07-24 11:35:50 +00:00
|
|
|
const templatePaths = uniq([
|
|
|
|
// Modules & user provided templates
|
|
|
|
// first custom to keep their index
|
|
|
|
...customTemplateFiles,
|
|
|
|
// @nuxt/vue-app templates
|
|
|
|
...templateContext.templateFiles
|
|
|
|
])
|
2018-01-13 05:22:11 +00:00
|
|
|
|
2019-08-24 17:10:39 +00:00
|
|
|
const appDir = path.resolve(this.options.srcDir, this.options.dir.app)
|
|
|
|
|
2019-07-24 11:35:50 +00:00
|
|
|
templateContext.templateFiles = await Promise.all(templatePaths.map(async (file) => {
|
|
|
|
// Use custom file if provided in build.templates[]
|
|
|
|
const customTemplateIndex = customTemplateFiles.indexOf(file)
|
|
|
|
const customTemplate = customTemplateIndex !== -1 ? this.options.build.templates[customTemplateIndex] : null
|
|
|
|
let src = customTemplate ? (customTemplate.src || customTemplate) : r(this.template.dir, file)
|
2019-08-24 17:10:39 +00:00
|
|
|
|
2019-01-30 16:54:26 +00:00
|
|
|
// Allow override templates using a file with same name in ${srcDir}/app
|
2019-08-24 17:10:39 +00:00
|
|
|
const customAppFile = path.resolve(this.options.srcDir, this.options.dir.app, file)
|
|
|
|
const customAppFileExists = customAppFile.startsWith(appDir) && await fsExtra.exists(customAppFile)
|
|
|
|
if (customAppFileExists) {
|
|
|
|
src = customAppFile
|
|
|
|
}
|
2019-01-30 16:54:26 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-24 11:35:50 +00:00
|
|
|
src,
|
2019-01-30 16:54:26 +00:00
|
|
|
dst: file,
|
2019-08-24 17:10:39 +00:00
|
|
|
custom: Boolean(customAppFileExists || customTemplate),
|
2019-07-24 11:35:50 +00:00
|
|
|
options: (customTemplate && customTemplate.options) || {}
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async resolveLoadingIndicator ({ templateFiles }) {
|
2019-02-08 16:25:11 +00:00
|
|
|
if (!this.options.loadingIndicator.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let indicatorPath = path.resolve(
|
|
|
|
this.template.dir,
|
|
|
|
'views/loading',
|
|
|
|
this.options.loadingIndicator.name + '.html'
|
|
|
|
)
|
2019-01-09 10:48:28 +00:00
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
let customIndicator = false
|
|
|
|
if (!await fsExtra.exists(indicatorPath)) {
|
|
|
|
indicatorPath = this.nuxt.resolver.resolveAlias(
|
|
|
|
this.options.loadingIndicator.name
|
|
|
|
)
|
2019-01-09 10:48:28 +00:00
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
if (await fsExtra.exists(indicatorPath)) {
|
|
|
|
customIndicator = true
|
2017-08-18 12:23:10 +00:00
|
|
|
} else {
|
2019-02-08 16:25:11 +00:00
|
|
|
indicatorPath = null
|
2017-08-18 10:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
|
|
|
if (!indicatorPath) {
|
|
|
|
consola.error(
|
|
|
|
`Could not fetch loading indicator: ${
|
|
|
|
this.options.loadingIndicator.name
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateFiles.push({
|
|
|
|
src: indicatorPath,
|
|
|
|
dst: 'loading.html',
|
|
|
|
custom: customIndicator,
|
|
|
|
options: this.options.loadingIndicator
|
|
|
|
})
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async compileTemplates (templateContext) {
|
2019-01-30 16:54:26 +00:00
|
|
|
// Prepare template options
|
|
|
|
const { templateVars, templateFiles, templateOptions } = templateContext
|
2017-08-18 10:26:19 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
await this.nuxt.callHook('build:templates', {
|
|
|
|
templateVars,
|
2019-02-01 11:56:26 +00:00
|
|
|
templatesFiles: templateFiles,
|
2018-01-13 05:22:11 +00:00
|
|
|
resolve: r
|
|
|
|
})
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2019-01-30 16:54:26 +00:00
|
|
|
templateOptions.imports = {
|
|
|
|
...templateOptions.imports,
|
|
|
|
resolvePath: this.nuxt.resolver.resolvePath,
|
|
|
|
resolveAlias: this.nuxt.resolver.resolveAlias,
|
|
|
|
relativeToBuild: this.relativeToBuild
|
2018-11-20 09:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Interpret and move template files to .nuxt/
|
2018-01-13 05:22:11 +00:00
|
|
|
await Promise.all(
|
2019-05-21 18:11:34 +00:00
|
|
|
templateFiles.map(async (templateFile) => {
|
|
|
|
const { src, dst, custom } = templateFile
|
|
|
|
|
2019-01-09 10:48:28 +00:00
|
|
|
// Add custom templates to watcher
|
|
|
|
if (custom) {
|
|
|
|
this.options.build.watch.push(src)
|
|
|
|
}
|
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
// Render template to dst
|
2018-03-16 19:11:24 +00:00
|
|
|
const fileContent = await fsExtra.readFile(src, 'utf8')
|
2019-08-25 12:31:17 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
let content
|
|
|
|
try {
|
2018-11-20 09:21:17 +00:00
|
|
|
const templateFunction = template(fileContent, templateOptions)
|
2018-10-24 13:46:06 +00:00
|
|
|
content = stripWhitespace(
|
2019-05-21 18:11:34 +00:00
|
|
|
templateFunction({
|
|
|
|
...templateVars,
|
2019-05-23 08:49:40 +00:00
|
|
|
...templateFile
|
2019-05-21 18:11:34 +00:00
|
|
|
})
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Could not compile template ${src}: ${err.message}`)
|
|
|
|
}
|
2019-04-22 18:50:07 +00:00
|
|
|
|
2018-08-24 18:20:57 +00:00
|
|
|
// Ensure parent dir exits and write file
|
2019-04-22 18:50:07 +00:00
|
|
|
const relativePath = r(this.options.buildDir, dst)
|
|
|
|
await fsExtra.outputFile(relativePath, content, 'utf8')
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
|
|
|
)
|
2019-01-30 16:54:26 +00:00
|
|
|
}
|
2018-03-16 06:32:07 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
resolvePlugins () {
|
2019-01-30 16:54:26 +00:00
|
|
|
// Check plugins exist then set alias to their real path
|
|
|
|
return Promise.all(this.plugins.map(async (p) => {
|
|
|
|
const ext = '{?(.+([^.])),/index.+([^.])}'
|
|
|
|
const pluginFiles = await glob(`${p.src}${ext}`)
|
|
|
|
|
|
|
|
if (!pluginFiles || pluginFiles.length === 0) {
|
|
|
|
throw new Error(`Plugin not found: ${p.src}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pluginFiles.length > 1 && !isIndexFileAndFolder(pluginFiles)) {
|
|
|
|
consola.warn({
|
|
|
|
message: `Found ${pluginFiles.length} plugins that match the configuration, suggest to specify extension:`,
|
|
|
|
additional: '\n' + pluginFiles.map(x => `- ${x}`).join('\n')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
p.src = this.relativeToBuild(p.src)
|
|
|
|
}))
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// TODO: Uncomment when generateConfig enabled again
|
2019-02-04 10:34:04 +00:00
|
|
|
// async generateConfig() {
|
2018-10-30 20:42:53 +00:00
|
|
|
// const config = path.resolve(this.options.buildDir, 'build.config.js')
|
|
|
|
// const options = omit(this.options, Options.unsafeKeys)
|
|
|
|
// await fsExtra.writeFile(
|
|
|
|
// config,
|
|
|
|
// `export default ${JSON.stringify(options, null, ' ')}`,
|
|
|
|
// 'utf8'
|
|
|
|
// )
|
|
|
|
// }
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
createFileWatcher (patterns, events, listener, watcherCreatedCallback) {
|
2019-02-06 18:46:17 +00:00
|
|
|
const options = this.options.watchers.chokidar
|
|
|
|
const watcher = chokidar.watch(patterns, options)
|
|
|
|
|
|
|
|
for (const event of events) {
|
|
|
|
watcher.on(event, listener)
|
|
|
|
}
|
|
|
|
|
2019-05-27 09:06:53 +00:00
|
|
|
// TODO: due to fixes in chokidar this isnt used anymore and could be removed in Nuxt v3
|
2019-02-06 18:46:17 +00:00
|
|
|
const { rewatchOnRawEvents } = this.options.watchers
|
|
|
|
if (rewatchOnRawEvents && Array.isArray(rewatchOnRawEvents)) {
|
|
|
|
watcher.on('raw', (_event) => {
|
|
|
|
if (rewatchOnRawEvents.includes(_event)) {
|
|
|
|
watcher.close()
|
|
|
|
|
|
|
|
listener()
|
|
|
|
this.createFileWatcher(patterns, events, listener, watcherCreatedCallback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof watcherCreatedCallback === 'function') {
|
|
|
|
watcherCreatedCallback(watcher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
assignWatcher (key) {
|
2019-02-06 18:46:17 +00:00
|
|
|
return (watcher) => {
|
2020-01-19 08:34:35 +00:00
|
|
|
if (this.watchers[key]) {
|
|
|
|
this.watchers[key].close()
|
|
|
|
}
|
2019-02-06 18:46:17 +00:00
|
|
|
this.watchers[key] = watcher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
watchClient () {
|
2018-01-02 02:36:09 +00:00
|
|
|
let patterns = [
|
2019-05-27 09:06:53 +00:00
|
|
|
r(this.options.srcDir, this.options.dir.layouts),
|
|
|
|
r(this.options.srcDir, this.options.dir.middleware)
|
2017-06-11 14:17:36 +00:00
|
|
|
]
|
2019-05-27 09:06:53 +00:00
|
|
|
|
2019-05-10 13:03:07 +00:00
|
|
|
if (this.options.store) {
|
2019-05-27 09:06:53 +00:00
|
|
|
patterns.push(r(this.options.srcDir, this.options.dir.store))
|
2019-05-10 13:03:07 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2019-05-23 10:19:37 +00:00
|
|
|
if (this._nuxtPages && !this._defaultPage) {
|
2019-05-27 09:06:53 +00:00
|
|
|
patterns.push(r(this.options.srcDir, this.options.dir.pages))
|
2017-11-24 08:21:08 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2019-05-27 09:06:53 +00:00
|
|
|
patterns = patterns.map((path, ...args) => upath.normalizeSafe(this.globPathWithExtensions(path), ...args))
|
2018-01-02 02:36:09 +00:00
|
|
|
|
2018-10-18 15:43:44 +00:00
|
|
|
const refreshFiles = debounce(() => this.generateRoutesAndFiles(), 200)
|
2017-06-15 22:19:53 +00:00
|
|
|
|
|
|
|
// Watch for src Files
|
2019-02-06 18:46:17 +00:00
|
|
|
this.createFileWatcher(patterns, ['add', 'unlink'], refreshFiles, this.assignWatcher('files'))
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Watch for custom provided files
|
2018-12-09 10:42:22 +00:00
|
|
|
const customPatterns = uniq([
|
|
|
|
...this.options.build.watch,
|
|
|
|
...Object.values(omit(this.options.build.styleResources, ['options']))
|
|
|
|
]).map(upath.normalizeSafe)
|
|
|
|
|
2019-01-21 15:14:17 +00:00
|
|
|
if (customPatterns.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 10:48:28 +00:00
|
|
|
|
2019-02-06 18:46:17 +00:00
|
|
|
this.createFileWatcher(customPatterns, ['change'], refreshFiles, this.assignWatcher('custom'))
|
2019-07-24 11:35:50 +00:00
|
|
|
|
|
|
|
// Watch for app/ files
|
|
|
|
this.createFileWatcher([r(this.options.srcDir, this.options.dir.app)], ['add', 'change', 'unlink'], refreshFiles, this.assignWatcher('app'))
|
2018-08-15 11:48:34 +00:00
|
|
|
}
|
2018-08-08 18:51:57 +00:00
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
serverMiddlewareHMR () {
|
|
|
|
// Check nuxt.server dependency
|
|
|
|
if (!this.nuxt.server) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get registered server middleware with path
|
|
|
|
const entries = this.nuxt.server.serverMiddlewarePaths()
|
|
|
|
|
|
|
|
// Resolve dependency tree
|
|
|
|
const deps = new Set()
|
|
|
|
const dep2Entry = {}
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
for (const dep of scanRequireTree(entry)) {
|
|
|
|
deps.add(dep)
|
|
|
|
if (!dep2Entry[dep]) {
|
|
|
|
dep2Entry[dep] = new Set()
|
2019-05-10 13:03:07 +00:00
|
|
|
}
|
2020-01-19 08:34:35 +00:00
|
|
|
dep2Entry[dep].add(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create watcher
|
|
|
|
this.createFileWatcher(
|
|
|
|
Array.from(deps),
|
|
|
|
['all'],
|
|
|
|
debounce((event, fileName) => {
|
2020-03-18 10:05:37 +00:00
|
|
|
if (!dep2Entry[fileName]) {
|
|
|
|
return // #7097
|
|
|
|
}
|
2020-01-19 08:34:35 +00:00
|
|
|
for (const entry of dep2Entry[fileName]) {
|
|
|
|
// Reload entry
|
|
|
|
let newItem
|
|
|
|
try {
|
|
|
|
newItem = this.nuxt.server.replaceMiddleware(entry, entry)
|
|
|
|
} catch (error) {
|
|
|
|
consola.error(error)
|
|
|
|
consola.error(`[HMR Error]: ${error}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newItem) {
|
|
|
|
// Full reload if HMR failed
|
|
|
|
return this.nuxt.callHook('watch:restart', { event, path: fileName })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log
|
2020-03-27 21:30:43 +00:00
|
|
|
consola.info(`[HMR] ${chalk.cyan(newItem.route || '/')} (${chalk.grey(fileName)})`)
|
2019-05-10 13:03:07 +00:00
|
|
|
}
|
2020-01-19 08:34:35 +00:00
|
|
|
// Tree may be changed so recreate watcher
|
|
|
|
this.serverMiddlewareHMR()
|
|
|
|
}, 200),
|
|
|
|
this.assignWatcher('serverMiddleware')
|
|
|
|
)
|
2019-05-10 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
watchRestart () {
|
2018-12-09 10:42:22 +00:00
|
|
|
const nuxtRestartWatch = [
|
|
|
|
// Custom watchers
|
|
|
|
...this.options.watch
|
|
|
|
].map(this.nuxt.resolver.resolveAlias)
|
2018-08-15 11:48:34 +00:00
|
|
|
|
2019-01-29 09:31:14 +00:00
|
|
|
if (this.ignore.ignoreFile) {
|
|
|
|
nuxtRestartWatch.push(this.ignore.ignoreFile)
|
|
|
|
}
|
2020-05-18 08:21:15 +00:00
|
|
|
|
|
|
|
if (this.options._envConfig && this.options._envConfig.dotenv) {
|
|
|
|
nuxtRestartWatch.push(this.options._envConfig.dotenv)
|
|
|
|
}
|
|
|
|
|
2019-05-23 10:19:37 +00:00
|
|
|
// If default page displayed, watch for first page creation
|
|
|
|
if (this._nuxtPages && this._defaultPage) {
|
|
|
|
nuxtRestartWatch.push(path.join(this.options.srcDir, this.options.dir.pages))
|
|
|
|
}
|
2019-05-10 13:03:07 +00:00
|
|
|
// If store not activated, watch for a file in the directory
|
|
|
|
if (!this.options.store) {
|
|
|
|
nuxtRestartWatch.push(path.join(this.options.srcDir, this.options.dir.store))
|
|
|
|
}
|
2019-01-29 09:31:14 +00:00
|
|
|
|
2019-02-06 18:46:17 +00:00
|
|
|
this.createFileWatcher(
|
|
|
|
nuxtRestartWatch,
|
|
|
|
['all'],
|
2019-04-29 18:20:38 +00:00
|
|
|
async (event, fileName) => {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (['add', 'change', 'unlink'].includes(event) === false) {
|
|
|
|
return
|
|
|
|
}
|
2019-04-29 18:20:38 +00:00
|
|
|
await this.nuxt.callHook('watch:fileChanged', this, fileName) // Legacy
|
|
|
|
await this.nuxt.callHook('watch:restart', { event, path: fileName })
|
2019-02-06 18:46:17 +00:00
|
|
|
},
|
|
|
|
this.assignWatcher('restart')
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-11-24 03:43:01 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
unwatch () {
|
2018-08-10 10:37:05 +00:00
|
|
|
for (const watcher in this.watchers) {
|
2018-12-09 10:42:22 +00:00
|
|
|
this.watchers[watcher].close()
|
2018-08-08 18:51:57 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async close () {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this.__closed) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
this.__closed = true
|
|
|
|
|
|
|
|
// Unwatch
|
|
|
|
this.unwatch()
|
2018-08-08 18:51:57 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Close bundleBuilder
|
|
|
|
if (typeof this.bundleBuilder.close === 'function') {
|
|
|
|
await this.bundleBuilder.close()
|
2018-03-24 07:52:49 +00:00
|
|
|
}
|
2017-11-24 03:43:01 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const STATUS = {
|
|
|
|
INITIAL: 1,
|
|
|
|
BUILD_DONE: 2,
|
|
|
|
BUILDING: 3
|
|
|
|
}
|