2018-03-16 19:52:17 +00:00
|
|
|
import path from 'path'
|
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 16:12:06 +00:00
|
|
|
import serialize from 'serialize-javascript'
|
2018-03-16 19:52:17 +00:00
|
|
|
import upath from 'upath'
|
2018-10-24 16:55:18 +00:00
|
|
|
|
|
|
|
import concat from 'lodash/concat'
|
|
|
|
import debounce from 'lodash/debounce'
|
|
|
|
import map from 'lodash/map'
|
|
|
|
import omit from 'lodash/omit'
|
|
|
|
import template from 'lodash/template'
|
|
|
|
import uniq from 'lodash/uniq'
|
|
|
|
import uniqBy from 'lodash/uniqBy'
|
|
|
|
import values from 'lodash/values'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
import devalue from '@nuxtjs/devalue'
|
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
import {
|
|
|
|
r,
|
|
|
|
wp,
|
|
|
|
wChunk,
|
|
|
|
createRoutes,
|
|
|
|
relativeTo,
|
|
|
|
waitFor,
|
2018-10-24 13:46:06 +00:00
|
|
|
determineGlobals,
|
2018-10-25 10:57:52 +00:00
|
|
|
stripWhitespace,
|
|
|
|
isString
|
2018-10-17 21:28:25 +00:00
|
|
|
} from '@nuxt/common'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
import BuildContext from './context'
|
|
|
|
|
2018-06-01 12:24:37 +00:00
|
|
|
const glob = pify(Glob)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export default class Builder {
|
2018-10-24 16:55:18 +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
|
|
|
|
2017-06-29 16:36:22 +00:00
|
|
|
// Helper to resolve build paths
|
2018-01-13 05:22: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()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Stop watching on nuxt.close()
|
2018-07-25 15:57:43 +00:00
|
|
|
this.nuxt.hook('close', () => this.unwatch())
|
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', () => {
|
2018-11-01 03:50:07 +00:00
|
|
|
consola.warn('Notice: Please do not deploy bundles built with analyze mode, it\'s only for analyzing purpose.')
|
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') {
|
|
|
|
this.template = this.nuxt.resolver.requireModule(this.template)
|
|
|
|
}
|
|
|
|
|
2018-03-22 19:29:05 +00:00
|
|
|
// if(!this.options.dev) {
|
2018-01-08 15:11:03 +00:00
|
|
|
// TODO: enable again when unsafe concern resolved.(common/options.js:42)
|
2018-07-25 15:57:43 +00:00
|
|
|
// this.nuxt.hook('build:done', () => this.generateConfig())
|
2018-01-08 15:11:03 +00:00
|
|
|
// }
|
2018-10-24 16:55:18 +00:00
|
|
|
|
|
|
|
this.bundleBuilder = this.getBundleBuilder(bundleBuilder)
|
|
|
|
}
|
|
|
|
|
2018-10-25 11:22:31 +00:00
|
|
|
getBundleBuilder(BundleBuilder) {
|
|
|
|
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') {
|
|
|
|
BundleBuilder = require('@nuxt/webpack').BundleBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
return new BundleBuilder(context)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 14:59:15 +00:00
|
|
|
normalizePlugins() {
|
2018-10-18 15:43:44 +00:00
|
|
|
return uniqBy(
|
2018-08-06 00:12:44 +00:00
|
|
|
this.options.plugins.map((p) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
if (typeof p === 'string') p = { src: p }
|
2018-03-16 19:11:24 +00:00
|
|
|
const pluginBaseName = path.basename(p.src, path.extname(p.src)).replace(
|
2018-01-13 05:22:11 +00:00
|
|
|
/[^a-zA-Z?\d\s:]/g,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
return {
|
2018-10-17 21:28:25 +00:00
|
|
|
src: this.nuxt.resolver.resolveAlias(p.src),
|
2018-01-13 05:22:11 +00:00
|
|
|
ssr: p.ssr !== false,
|
|
|
|
name: 'nuxt_plugin_' + pluginBaseName + '_' + hash(p.src)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
p => p.name
|
|
|
|
)
|
2017-07-10 22:53:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
resolvePlugins() {
|
|
|
|
// Check plugins exist then set alias to their real path
|
|
|
|
return Promise.all(this.plugins.map(async (p) => {
|
|
|
|
const ext = path.extname(p.src) ? '' : '{.+([^.]),/index.+([^.])}'
|
|
|
|
const pluginFiles = await glob(`${p.src}${ext}`)
|
|
|
|
|
|
|
|
if (!pluginFiles || pluginFiles.length === 0) {
|
|
|
|
throw new Error(`Plugin not found: ${p.src}`)
|
|
|
|
} else if (pluginFiles.length > 1) {
|
|
|
|
consola.warn({
|
|
|
|
message: `Found ${pluginFiles.length} plugins that match the configuration, suggest to specify extension:`,
|
2018-11-08 09:15:56 +00:00
|
|
|
additional: '\n' + pluginFiles.map(x => `- ${x}`).join('\n')
|
2018-10-24 16:55:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
p.src = this.relativeToBuild(p.src)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
forGenerate() {
|
2018-10-24 16:55:18 +00:00
|
|
|
this.bundleBuilder.forGenerate()
|
2017-08-17 12:43:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async build() {
|
2017-06-18 15:44:01 +00:00
|
|
|
// Avoid calling build() method multiple times when dev:true
|
2017-06-19 15:47:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-18 15:44:01 +00:00
|
|
|
if (this._buildStatus === STATUS.BUILD_DONE && this.options.dev) {
|
2017-06-11 14:17:36 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
// If building
|
2017-06-19 15:47:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-11 14:17:36 +00:00
|
|
|
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')
|
|
|
|
}
|
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
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Check if pages dir exists and warn if not
|
|
|
|
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
|
|
|
|
if (this._nuxtPages) {
|
2018-03-16 19:11:24 +00:00
|
|
|
if (!fsExtra.existsSync(path.join(this.options.srcDir, this.options.dir.pages))) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const dir = this.options.srcDir
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fsExtra.existsSync(path.join(this.options.srcDir, '..', this.options.dir.pages))) {
|
2018-01-13 05:22:11 +00:00
|
|
|
throw new Error(
|
2018-02-03 16:04:15 +00:00
|
|
|
`No \`${this.options.dir.pages}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?`
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
} else {
|
2018-04-06 11:27:43 +00:00
|
|
|
this._defaultPage = true
|
2018-11-01 03:50:07 +00:00
|
|
|
consola.warn(`No \`${this.options.dir.pages}\` directory found in ${dir}. Using the default built-in page.`)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 17:58:04 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
// Create .nuxt/, .nuxt/components and .nuxt/dist folders
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.remove(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
|
|
|
}
|
2018-08-24 18:20:57 +00:00
|
|
|
await Promise.all(buildDirs.map(dir => fsExtra.mkdirp(dir)))
|
2017-06-14 16:13:43 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async generateRoutesAndFiles() {
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.debug(`Generating nuxt files`)
|
2018-03-16 06:32:07 +00:00
|
|
|
|
2018-11-06 12:53:15 +00:00
|
|
|
// Plugins
|
|
|
|
this.plugins = Array.from(this.normalizePlugins())
|
2018-08-10 14:59:15 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// -- Templates --
|
2018-10-17 21:28:25 +00:00
|
|
|
let templatesFiles = Array.from(this.template.templatesFiles)
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
const templateVars = {
|
|
|
|
options: this.options,
|
2018-01-13 05:22:11 +00:00
|
|
|
extensions: this.options.extensions
|
|
|
|
.map(ext => ext.replace(/^\./, ''))
|
|
|
|
.join('|'),
|
2017-09-01 16:30:49 +00:00
|
|
|
messages: this.options.messages,
|
2018-03-19 18:40:33 +00:00
|
|
|
splitChunks: this.options.build.splitChunks,
|
2018-10-18 15:43:44 +00:00
|
|
|
uniqBy,
|
2017-06-11 14:17:36 +00:00
|
|
|
isDev: this.options.dev,
|
2018-10-24 13:46:06 +00:00
|
|
|
isTest: this.options.test,
|
2017-08-05 21:48:43 +00:00
|
|
|
debug: this.options.debug,
|
2018-08-08 19:05:23 +00:00
|
|
|
vue: { config: this.options.vue.config },
|
2017-08-20 13:13:42 +00:00
|
|
|
mode: this.options.mode,
|
2017-06-13 17:58:04 +00:00
|
|
|
router: this.options.router,
|
2017-06-11 14:17:36 +00:00
|
|
|
env: this.options.env,
|
|
|
|
head: this.options.head,
|
2018-03-16 19:11:24 +00:00
|
|
|
middleware: fsExtra.existsSync(path.join(this.options.srcDir, this.options.dir.middleware)),
|
2017-06-14 16:13:43 +00:00
|
|
|
store: this.options.store,
|
2018-10-09 12:07:23 +00:00
|
|
|
globalName: this.options.globalName,
|
|
|
|
globals: this.globals,
|
2017-06-11 14:17:36 +00:00
|
|
|
css: this.options.css,
|
2017-07-10 22:53:06 +00:00
|
|
|
plugins: this.plugins,
|
2017-10-13 21:53:04 +00:00
|
|
|
appPath: './App.js',
|
2017-12-18 15:53:36 +00:00
|
|
|
ignorePrefix: this.options.ignorePrefix,
|
2017-06-11 14:17:36 +00:00
|
|
|
layouts: Object.assign({}, this.options.layouts),
|
2018-01-13 05:22:11 +00:00
|
|
|
loading:
|
|
|
|
typeof this.options.loading === 'string'
|
|
|
|
? this.relativeToBuild(this.options.srcDir, this.options.loading)
|
|
|
|
: this.options.loading,
|
2017-06-11 14:17:36 +00:00
|
|
|
transition: this.options.transition,
|
2017-09-08 10:42:00 +00:00
|
|
|
layoutTransition: this.options.layoutTransition,
|
2018-02-03 23:24:45 +00:00
|
|
|
dir: this.options.dir,
|
2017-06-11 14:17:36 +00:00
|
|
|
components: {
|
2018-01-13 05:22:11 +00:00
|
|
|
ErrorPage: this.options.ErrorPage
|
|
|
|
? this.relativeToBuild(this.options.ErrorPage)
|
|
|
|
: null
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Layouts --
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fsExtra.existsSync(path.resolve(this.options.srcDir, this.options.dir.layouts))) {
|
2018-02-03 18:41:43 +00:00
|
|
|
const layoutsFiles = await glob(`${this.options.dir.layouts}/**/*.{vue,js}`, {
|
2018-01-13 05:22:11 +00:00
|
|
|
cwd: this.options.srcDir,
|
2018-01-15 09:44:44 +00:00
|
|
|
ignore: this.options.ignore
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2018-08-06 00:12:44 +00:00
|
|
|
layoutsFiles.forEach((file) => {
|
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-01-13 05:22:11 +00:00
|
|
|
.replace(/\.(vue|js)$/, '')
|
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
|
|
|
|
)
|
|
|
|
}
|
2017-08-14 12:15:00 +00:00
|
|
|
return
|
|
|
|
}
|
2017-12-12 13:32:45 +00:00
|
|
|
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
|
|
|
}
|
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'))
|
2017-06-11 14:17:36 +00:00
|
|
|
templatesFiles.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
|
|
|
}
|
|
|
|
|
|
|
|
// -- Routes --
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.debug('Generating routes...')
|
2018-04-06 11:27:43 +00:00
|
|
|
|
|
|
|
if (this._defaultPage) {
|
|
|
|
templateVars.router.routes = createRoutes(
|
|
|
|
['index.vue'],
|
2018-10-17 21:28:25 +00:00
|
|
|
this.template.templatesDir + '/pages'
|
2018-04-06 11:27:43 +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 = {}
|
2018-10-25 10:55:05 +00:00
|
|
|
; (await glob(`${this.options.dir.pages}/**/*.{vue,js}`, {
|
2018-01-13 05:22:11 +00:00
|
|
|
cwd: this.options.srcDir,
|
2018-01-15 09:44:44 +00:00
|
|
|
ignore: this.options.ignore
|
2018-08-06 00:12:44 +00:00
|
|
|
})).forEach((f) => {
|
2017-12-12 13:32:45 +00:00
|
|
|
const key = f.replace(/\.(js|vue)$/, '')
|
|
|
|
if (/\.vue$/.test(f) || !files[key]) {
|
2018-09-05 15:13:35 +00:00
|
|
|
files[key] = f.replace(/('|")/g, '\\$1')
|
2017-12-12 13:32:45 +00:00
|
|
|
}
|
|
|
|
})
|
2018-01-13 05:22:11 +00:00
|
|
|
templateVars.router.routes = createRoutes(
|
|
|
|
Object.values(files),
|
2018-02-02 16:58:51 +00:00
|
|
|
this.options.srcDir,
|
2018-02-03 16:04:15 +00:00
|
|
|
this.options.dir.pages
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2018-11-03 12:25:41 +00:00
|
|
|
} else { // If user defined a custom method to create routes
|
2018-01-13 05:22:11 +00:00
|
|
|
templateVars.router.routes = this.options.build.createRoutes(
|
|
|
|
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
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// -- Store --
|
|
|
|
// Add store if needed
|
|
|
|
if (this.options.store) {
|
|
|
|
templatesFiles.push('store.js')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve template files
|
2018-01-13 05:22:11 +00:00
|
|
|
const customTemplateFiles = this.options.build.templates.map(
|
2018-03-16 19:11:24 +00:00
|
|
|
t => t.dst || path.basename(t.src || t)
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
templatesFiles = templatesFiles
|
2018-08-06 00:12:44 +00:00
|
|
|
.map((file) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
// Skip if custom file was already provided in build.templates[]
|
2018-10-12 22:31:19 +00:00
|
|
|
if (customTemplateFiles.includes(file)) {
|
2018-01-13 05:22:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Allow override templates using a file with same name in ${srcDir}/app
|
|
|
|
const customPath = r(this.options.srcDir, 'app', file)
|
2018-03-16 19:11:24 +00:00
|
|
|
const customFileExists = fsExtra.existsSync(customPath)
|
2018-01-13 05:22:11 +00:00
|
|
|
|
|
|
|
return {
|
2018-10-17 21:28:25 +00:00
|
|
|
src: customFileExists ? customPath : r(this.template.templatesDir, file),
|
2018-01-13 05:22:11 +00:00
|
|
|
dst: file,
|
|
|
|
custom: customFileExists
|
|
|
|
}
|
|
|
|
})
|
2018-10-25 10:57:52 +00:00
|
|
|
.filter(Boolean)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
|
|
|
// -- Custom templates --
|
|
|
|
// Add custom template files
|
2018-01-13 05:22:11 +00:00
|
|
|
templatesFiles = templatesFiles.concat(
|
2018-08-06 00:12:44 +00:00
|
|
|
this.options.build.templates.map((t) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
src: r(this.options.srcDir, t.src || t),
|
2018-03-16 19:11:24 +00:00
|
|
|
dst: t.dst || path.basename(t.src || t),
|
2018-01-13 05:22:11 +00:00
|
|
|
custom: true
|
|
|
|
},
|
|
|
|
t
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-08-18 10:26:19 +00:00
|
|
|
// -- Loading indicator --
|
|
|
|
if (this.options.loadingIndicator.name) {
|
2018-03-16 19:11:24 +00:00
|
|
|
const indicatorPath1 = path.resolve(
|
2018-10-17 21:28:25 +00:00
|
|
|
this.template.templatesDir,
|
2018-01-13 05:22:11 +00:00
|
|
|
'views/loading',
|
|
|
|
this.options.loadingIndicator.name + '.html'
|
|
|
|
)
|
2018-10-17 21:28:25 +00:00
|
|
|
const indicatorPath2 = this.nuxt.resolver.resolveAlias(
|
2018-01-13 05:22:11 +00:00
|
|
|
this.options.loadingIndicator.name
|
|
|
|
)
|
2018-03-16 19:11:24 +00:00
|
|
|
const indicatorPath = fsExtra.existsSync(indicatorPath1)
|
2018-01-13 05:22:11 +00:00
|
|
|
? indicatorPath1
|
2018-03-16 19:11:24 +00:00
|
|
|
: fsExtra.existsSync(indicatorPath2) ? indicatorPath2 : null
|
2017-08-18 10:26:19 +00:00
|
|
|
if (indicatorPath) {
|
|
|
|
templatesFiles.push({
|
|
|
|
src: indicatorPath,
|
|
|
|
dst: 'loading.html',
|
|
|
|
options: this.options.loadingIndicator
|
|
|
|
})
|
2017-08-18 12:23:10 +00:00
|
|
|
} else {
|
2017-11-24 15:44:07 +00:00
|
|
|
/* istanbul ignore next */
|
2018-01-13 05:22:11 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
|
|
|
`Could not fetch loading indicator: ${
|
|
|
|
this.options.loadingIndicator.name
|
|
|
|
}`
|
|
|
|
)
|
2017-08-18 10:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
await this.nuxt.callHook('build:templates', {
|
|
|
|
templatesFiles,
|
|
|
|
templateVars,
|
|
|
|
resolve: r
|
|
|
|
})
|
2017-07-03 11:11:40 +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(
|
|
|
|
templatesFiles.map(async ({ src, dst, options, custom }) => {
|
|
|
|
// Add template to watchers
|
|
|
|
this.options.build.watch.push(src)
|
|
|
|
// Render template to dst
|
2018-03-16 19:11:24 +00:00
|
|
|
const fileContent = await fsExtra.readFile(src, 'utf8')
|
2018-01-13 05:22:11 +00:00
|
|
|
let content
|
|
|
|
try {
|
2018-10-18 15:43:44 +00:00
|
|
|
const templateFunction = template(fileContent, {
|
2018-01-13 05:22:11 +00:00
|
|
|
imports: {
|
|
|
|
serialize,
|
2018-09-25 08:51:07 +00:00
|
|
|
devalue,
|
2018-01-13 05:22:11 +00:00
|
|
|
hash,
|
|
|
|
r,
|
|
|
|
wp,
|
|
|
|
wChunk,
|
2018-10-17 21:28:25 +00:00
|
|
|
resolvePath: this.nuxt.resolver.resolvePath,
|
|
|
|
resolveAlias: this.nuxt.resolver.resolveAlias,
|
2018-01-13 05:22:11 +00:00
|
|
|
relativeToBuild: this.relativeToBuild
|
2018-08-17 08:55:24 +00:00
|
|
|
},
|
|
|
|
interpolate: /<%=([\s\S]+?)%>/g
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2018-10-24 13:46:06 +00:00
|
|
|
content = stripWhitespace(
|
|
|
|
templateFunction(
|
|
|
|
Object.assign({}, templateVars, {
|
|
|
|
options: options || {},
|
|
|
|
custom,
|
|
|
|
src,
|
|
|
|
dst
|
|
|
|
})
|
|
|
|
)
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
throw new Error(`Could not compile template ${src}: ${err.message}`)
|
|
|
|
}
|
2018-03-16 19:11:24 +00:00
|
|
|
const _path = r(this.options.buildDir, dst)
|
2018-08-24 18:20:57 +00:00
|
|
|
// Ensure parent dir exits and write file
|
|
|
|
await fsExtra.outputFile(_path, content, 'utf8')
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
|
|
|
)
|
2018-03-16 06:32:07 +00:00
|
|
|
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.success('Nuxt files generated')
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// TODO: Uncomment when generateConfig enabled again
|
|
|
|
// async generateConfig() /* istanbul ignore next */ {
|
|
|
|
// 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
|
|
|
|
2018-08-15 11:48:34 +00:00
|
|
|
watchClient() {
|
2017-11-24 03:43:01 +00:00
|
|
|
const src = this.options.srcDir
|
2018-01-02 02:36:09 +00:00
|
|
|
let patterns = [
|
2018-02-03 18:41:43 +00:00
|
|
|
r(src, this.options.dir.layouts),
|
2018-02-04 09:31:03 +00:00
|
|
|
r(src, this.options.dir.store),
|
2018-02-03 23:24:45 +00:00
|
|
|
r(src, this.options.dir.middleware),
|
2018-02-03 18:41:43 +00:00
|
|
|
r(src, `${this.options.dir.layouts}/*.{vue,js}`),
|
|
|
|
r(src, `${this.options.dir.layouts}/**/*.{vue,js}`)
|
2017-06-11 14:17:36 +00:00
|
|
|
]
|
2017-11-24 08:21:08 +00:00
|
|
|
if (this._nuxtPages) {
|
|
|
|
patterns.push(
|
2018-02-03 16:04:15 +00:00
|
|
|
r(src, this.options.dir.pages),
|
|
|
|
r(src, `${this.options.dir.pages}/*.{vue,js}`),
|
|
|
|
r(src, `${this.options.dir.pages}/**/*.{vue,js}`)
|
2017-11-24 08:21:08 +00:00
|
|
|
)
|
|
|
|
}
|
2018-10-18 15:43:44 +00:00
|
|
|
patterns = map(patterns, upath.normalizeSafe)
|
2018-01-02 02:36:09 +00:00
|
|
|
|
2018-08-15 11:48:34 +00:00
|
|
|
const options = this.options.watchers.chokidar
|
2017-06-11 14:17:36 +00:00
|
|
|
/* istanbul ignore next */
|
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
|
2018-08-10 10:37:05 +00:00
|
|
|
this.watchers.files = chokidar
|
2018-01-13 05:22:11 +00:00
|
|
|
.watch(patterns, options)
|
2017-06-11 14:17:36 +00:00
|
|
|
.on('add', refreshFiles)
|
|
|
|
.on('unlink', refreshFiles)
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Watch for custom provided files
|
2018-10-18 15:43:44 +00:00
|
|
|
let customPatterns = concat(
|
2018-02-26 13:30:15 +00:00
|
|
|
this.options.build.watch,
|
2018-10-18 15:43:44 +00:00
|
|
|
...values(omit(this.options.build.styleResources, ['options']))
|
2018-02-22 21:20:07 +00:00
|
|
|
)
|
2018-10-18 15:43:44 +00:00
|
|
|
customPatterns = map(uniq(customPatterns), upath.normalizeSafe)
|
2018-08-10 10:37:05 +00:00
|
|
|
this.watchers.custom = chokidar
|
2018-02-22 21:20:07 +00:00
|
|
|
.watch(customPatterns, options)
|
2017-06-11 14:17:36 +00:00
|
|
|
.on('change', refreshFiles)
|
2018-08-15 11:48:34 +00:00
|
|
|
}
|
2018-08-08 18:51:57 +00:00
|
|
|
|
2018-08-15 11:48:34 +00:00
|
|
|
watchServer() {
|
2018-10-18 15:43:44 +00:00
|
|
|
const nuxtRestartWatch = concat(
|
2018-08-20 14:43:23 +00:00
|
|
|
this.options.serverMiddleware
|
2018-10-25 10:57:52 +00:00
|
|
|
.filter(isString)
|
2018-10-17 21:28:25 +00:00
|
|
|
.map(this.nuxt.resolver.resolveAlias),
|
|
|
|
this.options.watch.map(this.nuxt.resolver.resolveAlias),
|
2018-08-08 18:51:57 +00:00
|
|
|
path.join(this.options.rootDir, 'nuxt.config.js')
|
|
|
|
)
|
2018-08-15 11:48:34 +00:00
|
|
|
|
2018-08-10 10:37:05 +00:00
|
|
|
this.watchers.restart = chokidar
|
2018-08-15 11:48:34 +00:00
|
|
|
.watch(nuxtRestartWatch, this.options.watchers.chokidar)
|
2018-08-11 20:43:25 +00:00
|
|
|
.on('change', (_path) => {
|
2018-08-15 11:48:34 +00:00
|
|
|
this.watchers.restart.close()
|
2018-08-31 20:34:12 +00:00
|
|
|
const { name, ext } = path.parse(_path)
|
2018-08-15 11:48:34 +00:00
|
|
|
this.nuxt.callHook('watch:fileChanged', this, `${name}${ext}`)
|
2018-08-08 18:51:57 +00:00
|
|
|
})
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-11-24 03:43:01 +00:00
|
|
|
|
|
|
|
async unwatch() {
|
2018-08-10 10:37:05 +00:00
|
|
|
for (const watcher in this.watchers) {
|
|
|
|
if (this.watchers[watcher]) {
|
|
|
|
this.watchers[watcher].close()
|
|
|
|
}
|
2018-08-08 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
if (this.bundleBuilder.unwatch) {
|
|
|
|
await this.bundleBuilder.unwatch()
|
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
|
|
|
|
}
|