2017-12-12 09:42:29 +00:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const chokidar = require('chokidar')
|
|
|
|
const { remove, readFile, writeFile, mkdirp, existsSync } = require('fs-extra')
|
|
|
|
const hash = require('hash-sum')
|
|
|
|
const webpack = require('webpack')
|
|
|
|
const serialize = require('serialize-javascript')
|
|
|
|
const { join, resolve, basename, extname, dirname } = require('path')
|
|
|
|
const MFS = require('memory-fs')
|
|
|
|
const webpackDevMiddleware = require('webpack-dev-middleware')
|
|
|
|
const webpackHotMiddleware = require('webpack-hot-middleware')
|
|
|
|
const Debug = require('debug')
|
|
|
|
const Glob = require('glob')
|
2018-01-13 05:22:11 +00:00
|
|
|
const {
|
|
|
|
r,
|
|
|
|
wp,
|
|
|
|
wChunk,
|
|
|
|
createRoutes,
|
|
|
|
sequence,
|
|
|
|
relativeTo,
|
|
|
|
waitFor
|
|
|
|
} = require('../common/utils')
|
2017-12-29 08:56:32 +00:00
|
|
|
const { Options } = require('../common')
|
2017-12-12 09:42:29 +00:00
|
|
|
const clientWebpackConfig = require('./webpack/client.config.js')
|
|
|
|
const serverWebpackConfig = require('./webpack/server.config.js')
|
|
|
|
const dllWebpackConfig = require('./webpack/dll.config.js')
|
2018-01-02 02:36:09 +00:00
|
|
|
const upath = require('upath')
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-06-18 09:35:00 +00:00
|
|
|
const debug = Debug('nuxt:build')
|
2017-06-11 14:17:36 +00:00
|
|
|
debug.color = 2 // Force green color
|
|
|
|
|
2017-12-12 07:43:56 +00:00
|
|
|
const glob = promisify(Glob)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-12-12 09:42:29 +00:00
|
|
|
module.exports = class Builder {
|
2017-10-30 17:41:22 +00:00
|
|
|
constructor(nuxt) {
|
2017-06-11 14:17:36 +00:00
|
|
|
this.nuxt = nuxt
|
2017-08-17 12:50:39 +00:00
|
|
|
this.isStatic = false // Flag to know if the build is for a generated app
|
2017-06-11 14:17:36 +00:00
|
|
|
this.options = nuxt.options
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
// Fields that set on build
|
2017-11-07 09:46:29 +00:00
|
|
|
this.compilers = []
|
2017-11-22 12:57:39 +00:00
|
|
|
this.compilersWatching = []
|
2017-06-14 16:13:43 +00:00
|
|
|
this.webpackDevMiddleware = null
|
|
|
|
this.webpackHotMiddleware = null
|
2017-11-22 12:57:39 +00:00
|
|
|
this.filesWatcher = null
|
|
|
|
this.customFilesWatcher = null
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
// Mute stats on dev
|
2017-12-17 13:59:50 +00:00
|
|
|
this.webpackStats = this.options.dev ? false : this.options.build.stats
|
2017-06-18 15:44:01 +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
|
|
|
|
|
|
|
// Stop watching on nuxt.close()
|
2017-11-24 03:43:01 +00:00
|
|
|
if (this.options.dev) {
|
2017-11-24 08:21:08 +00:00
|
|
|
this.nuxt.hook('close', () => this.unwatch())
|
2017-11-22 12:57:39 +00:00
|
|
|
}
|
2018-01-08 15:11:03 +00:00
|
|
|
// else {
|
|
|
|
// TODO: enable again when unsafe concern resolved.(common/options.js:42)
|
|
|
|
// this.nuxt.hook('build:done', () => this.generateConfig())
|
|
|
|
// }
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
get plugins() {
|
2018-01-13 05:22:11 +00:00
|
|
|
return _.uniqBy(
|
|
|
|
this.options.plugins.map((p, i) => {
|
|
|
|
if (typeof p === 'string') p = { src: p }
|
|
|
|
const pluginBaseName = basename(p.src, extname(p.src)).replace(
|
|
|
|
/[^a-zA-Z?\d\s:]/g,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
src: this.nuxt.resolveAlias(p.src),
|
|
|
|
ssr: p.ssr !== false,
|
|
|
|
name: 'nuxt_plugin_' + pluginBaseName + '_' + hash(p.src)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
p => p.name
|
|
|
|
)
|
2017-07-10 22:53:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
vendor() {
|
2018-01-13 05:22:11 +00:00
|
|
|
return ['vue', 'vue-router', 'vue-meta', this.options.store && 'vuex']
|
|
|
|
.concat(
|
|
|
|
this.options.build.extractCSS && [
|
|
|
|
// https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/456
|
|
|
|
'vue-style-loader/lib/addStylesClient',
|
|
|
|
'css-loader/lib/css-base'
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.concat(this.options.build.vendor)
|
|
|
|
.filter(v => v)
|
2017-08-21 11:16:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
vendorEntries() {
|
2017-08-21 11:16:35 +00:00
|
|
|
// Used for dll
|
|
|
|
const vendor = this.vendor()
|
|
|
|
const vendorEntries = {}
|
|
|
|
vendor.forEach(v => {
|
2017-08-21 19:40:06 +00:00
|
|
|
try {
|
|
|
|
require.resolve(v)
|
2018-01-13 05:22:11 +00:00
|
|
|
vendorEntries[v] = [v]
|
2017-08-21 19:40:06 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
2017-08-21 11:16:35 +00:00
|
|
|
})
|
|
|
|
return vendorEntries
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
forGenerate() {
|
2017-08-17 12:50:39 +00:00
|
|
|
this.isStatic = true
|
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
|
|
|
|
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-02-03 16:04:15 +00:00
|
|
|
if (!existsSync(join(this.options.srcDir, this.options.dir.pages))) {
|
2017-06-13 20:14:51 +00:00
|
|
|
let dir = this.options.srcDir
|
2018-02-03 16:04:15 +00:00
|
|
|
if (existsSync(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-01-13 05:22:11 +00:00
|
|
|
throw new Error(
|
2018-02-03 16:04:15 +00:00
|
|
|
`Couldn't find a \`${this.options.dir.pages}\` directory in ${dir}. Please create one under the project root`
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 17:58:04 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
debug(`App root: ${this.options.srcDir}`)
|
|
|
|
debug(`Generating ${this.options.buildDir} files...`)
|
|
|
|
|
|
|
|
// Create .nuxt/, .nuxt/components and .nuxt/dist folders
|
|
|
|
await remove(r(this.options.buildDir))
|
|
|
|
await mkdirp(r(this.options.buildDir, 'components'))
|
|
|
|
if (!this.options.dev) {
|
|
|
|
await mkdirp(r(this.options.buildDir, 'dist'))
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Start webpack build
|
|
|
|
await this.webpackBuild()
|
|
|
|
|
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-12-08 14:18:37 +00:00
|
|
|
getBabelOptions({ isServer }) {
|
2018-01-13 05:22:11 +00:00
|
|
|
const options = _.defaults(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
babelrc: false,
|
|
|
|
cacheDirectory: !!this.options.dev
|
|
|
|
},
|
|
|
|
this.options.build.babel
|
|
|
|
)
|
2017-12-08 14:18:37 +00:00
|
|
|
|
|
|
|
if (typeof options.presets === 'function') {
|
|
|
|
options.presets = options.presets({ isServer })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.babelrc && !options.presets) {
|
|
|
|
options.presets = [
|
|
|
|
[
|
|
|
|
require.resolve('babel-preset-vue-app'),
|
|
|
|
{
|
|
|
|
targets: isServer ? { node: '8.0.0' } : { ie: 9, uglify: true }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:07:11 +00:00
|
|
|
getFileName(name) {
|
|
|
|
let fileName = this.options.build.filenames[name]
|
|
|
|
|
|
|
|
// Don't use hashes when watching
|
|
|
|
// https://github.com/webpack/webpack/issues/1914#issuecomment-174171709
|
|
|
|
if (this.options.dev) {
|
|
|
|
fileName = fileName.replace(/\[(chunkhash|contenthash|hash)\]\./g, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async generateRoutesAndFiles() {
|
2017-06-11 14:17:36 +00:00
|
|
|
debug('Generating files...')
|
|
|
|
// -- Templates --
|
|
|
|
let templatesFiles = [
|
2017-10-13 21:53:04 +00:00
|
|
|
'App.js',
|
2017-06-11 14:17:36 +00:00
|
|
|
'client.js',
|
|
|
|
'index.js',
|
|
|
|
'middleware.js',
|
|
|
|
'router.js',
|
|
|
|
'server.js',
|
|
|
|
'utils.js',
|
2017-07-10 22:53:06 +00:00
|
|
|
'empty.js',
|
2017-06-11 14:17:36 +00:00
|
|
|
'components/nuxt-error.vue',
|
|
|
|
'components/nuxt-loading.vue',
|
|
|
|
'components/nuxt-child.js',
|
|
|
|
'components/nuxt-link.js',
|
2017-10-13 21:53:04 +00:00
|
|
|
'components/nuxt.js',
|
2017-08-24 10:38:46 +00:00
|
|
|
'components/no-ssr.js',
|
2017-06-16 12:42:45 +00:00
|
|
|
'views/app.template.html',
|
|
|
|
'views/error.html'
|
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,
|
2017-06-11 14:17:36 +00:00
|
|
|
uniqBy: _.uniqBy,
|
|
|
|
isDev: this.options.dev,
|
2017-08-05 21:48:43 +00:00
|
|
|
debug: this.options.debug,
|
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-02-03 23:24:45 +00:00
|
|
|
middleware: existsSync(join(this.options.srcDir, this.options.dir.middleware)),
|
2017-06-14 16:13:43 +00:00
|
|
|
store: this.options.store,
|
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-02-03 18:41:43 +00:00
|
|
|
if (existsSync(resolve(this.options.srcDir, this.options.dir.layouts))) {
|
|
|
|
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
|
|
|
})
|
2017-08-14 12:15:00 +00:00
|
|
|
let hasErrorLayout = false
|
2018-01-13 05:22:11 +00:00
|
|
|
layoutsFiles.forEach(file => {
|
|
|
|
let name = file
|
|
|
|
.split('/')
|
|
|
|
.slice(1)
|
|
|
|
.join('/')
|
|
|
|
.replace(/\.(vue|js)$/, '')
|
2017-08-14 12:15:00 +00:00
|
|
|
if (name === 'error') {
|
|
|
|
hasErrorLayout = true
|
|
|
|
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
|
|
|
})
|
2017-08-14 12:15:00 +00:00
|
|
|
if (!templateVars.components.ErrorPage && hasErrorLayout) {
|
2018-01-13 05:22:11 +00:00
|
|
|
templateVars.components.ErrorPage = this.relativeToBuild(
|
|
|
|
this.options.srcDir,
|
2018-02-03 18:41:43 +00:00
|
|
|
`${this.options.dir.layouts}/error.vue`
|
2018-01-13 05:22:11 +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) {
|
|
|
|
await mkdirp(r(this.options.buildDir, 'layouts'))
|
|
|
|
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 --
|
|
|
|
debug('Generating routes...')
|
|
|
|
// If user defined a custom method to create routes
|
|
|
|
if (this._nuxtPages) {
|
|
|
|
// Use nuxt.js createRoutes bases on pages/
|
2018-01-13 05:22:11 +00:00
|
|
|
const files = {}
|
2018-02-03 16:04:15 +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-01-13 05:22:11 +00:00
|
|
|
})).forEach(f => {
|
2017-12-12 13:32:45 +00:00
|
|
|
const key = f.replace(/\.(js|vue)$/, '')
|
|
|
|
if (/\.vue$/.test(f) || !files[key]) {
|
|
|
|
files[key] = f
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
} else {
|
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-07-03 11:11:40 +00:00
|
|
|
|
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(
|
|
|
|
t => t.dst || basename(t.src || t)
|
|
|
|
)
|
|
|
|
|
|
|
|
templatesFiles = templatesFiles
|
|
|
|
.map(file => {
|
|
|
|
// Skip if custom file was already provided in build.templates[]
|
|
|
|
if (customTemplateFiles.indexOf(file) !== -1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Allow override templates using a file with same name in ${srcDir}/app
|
|
|
|
const customPath = r(this.options.srcDir, 'app', file)
|
|
|
|
const customFileExists = existsSync(customPath)
|
|
|
|
|
|
|
|
return {
|
|
|
|
src: customFileExists ? customPath : r(this.options.nuxtAppDir, file),
|
|
|
|
dst: file,
|
|
|
|
custom: customFileExists
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(i => !!i)
|
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(
|
|
|
|
this.options.build.templates.map(t => {
|
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
src: r(this.options.srcDir, t.src || t),
|
|
|
|
dst: t.dst || basename(t.src || t),
|
|
|
|
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-01-13 05:22:11 +00:00
|
|
|
const indicatorPath1 = resolve(
|
|
|
|
this.options.nuxtAppDir,
|
|
|
|
'views/loading',
|
|
|
|
this.options.loadingIndicator.name + '.html'
|
|
|
|
)
|
|
|
|
const indicatorPath2 = this.nuxt.resolveAlias(
|
|
|
|
this.options.loadingIndicator.name
|
|
|
|
)
|
|
|
|
const indicatorPath = existsSync(indicatorPath1)
|
|
|
|
? indicatorPath1
|
|
|
|
: 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
|
|
|
|
const fileContent = await readFile(src, 'utf8')
|
|
|
|
let content
|
|
|
|
try {
|
|
|
|
const template = _.template(fileContent, {
|
|
|
|
imports: {
|
|
|
|
serialize,
|
|
|
|
hash,
|
|
|
|
r,
|
|
|
|
wp,
|
|
|
|
wChunk,
|
|
|
|
resolvePath: this.nuxt.resolvePath.bind(this.nuxt),
|
|
|
|
resolveAlias: this.nuxt.resolveAlias.bind(this.nuxt),
|
|
|
|
relativeToBuild: this.relativeToBuild
|
|
|
|
}
|
|
|
|
})
|
|
|
|
content = template(
|
|
|
|
Object.assign({}, templateVars, {
|
|
|
|
options: options || {},
|
|
|
|
custom,
|
|
|
|
src,
|
|
|
|
dst
|
|
|
|
})
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
throw new Error(`Could not compile template ${src}: ${err.message}`)
|
|
|
|
}
|
|
|
|
const path = r(this.options.buildDir, dst)
|
|
|
|
// Ensure parent dir exits
|
|
|
|
await mkdirp(dirname(path))
|
|
|
|
// Write file
|
|
|
|
await writeFile(path, content, 'utf8')
|
|
|
|
})
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async webpackBuild() {
|
2017-06-14 16:13:43 +00:00
|
|
|
debug('Building files...')
|
2017-07-10 22:53:06 +00:00
|
|
|
const compilersOptions = []
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
// Client
|
2017-07-10 22:53:06 +00:00
|
|
|
const clientConfig = clientWebpackConfig.call(this)
|
2017-06-14 16:13:43 +00:00
|
|
|
compilersOptions.push(clientConfig)
|
|
|
|
|
|
|
|
// Server
|
2017-08-22 11:56:22 +00:00
|
|
|
let serverConfig = null
|
2017-07-11 00:24:39 +00:00
|
|
|
if (this.options.build.ssr) {
|
2017-08-22 11:56:22 +00:00
|
|
|
serverConfig = serverWebpackConfig.call(this)
|
2017-07-11 00:24:39 +00:00
|
|
|
compilersOptions.push(serverConfig)
|
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-07-10 22:53:06 +00:00
|
|
|
// Alias plugins to their real path
|
|
|
|
this.plugins.forEach(p => {
|
|
|
|
const src = this.relativeToBuild(p.src)
|
|
|
|
|
|
|
|
// Client config
|
|
|
|
if (!clientConfig.resolve.alias[p.name]) {
|
|
|
|
clientConfig.resolve.alias[p.name] = src
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server config
|
2017-08-22 11:56:22 +00:00
|
|
|
if (serverConfig && !serverConfig.resolve.alias[p.name]) {
|
2017-07-10 22:53:06 +00:00
|
|
|
// Alias to noop for ssr:false plugins
|
|
|
|
serverConfig.resolve.alias[p.name] = p.ssr ? src : './empty.js'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-30 14:42:59 +00:00
|
|
|
// Make a dll plugin after compile to make nuxt dev builds faster
|
2017-08-21 11:16:35 +00:00
|
|
|
if (this.options.build.dll && this.options.dev) {
|
|
|
|
compilersOptions.push(dllWebpackConfig.call(this, clientConfig))
|
|
|
|
}
|
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
// Initialize shared FS and Cache
|
|
|
|
const sharedFS = this.options.dev && new MFS()
|
|
|
|
const sharedCache = {}
|
|
|
|
|
|
|
|
// Initialize compilers
|
2017-11-07 09:46:29 +00:00
|
|
|
this.compilers = compilersOptions.map(compilersOption => {
|
2017-06-18 15:44:01 +00:00
|
|
|
const compiler = webpack(compilersOption)
|
2017-10-30 14:42:59 +00:00
|
|
|
// In dev, write files in memory FS (except for DLL)
|
2017-08-21 11:16:35 +00:00
|
|
|
if (sharedFS && !compiler.name.includes('-dll')) {
|
2017-06-18 15:44:01 +00:00
|
|
|
compiler.outputFileSystem = sharedFS
|
|
|
|
}
|
|
|
|
compiler.cache = sharedCache
|
2017-11-07 09:46:29 +00:00
|
|
|
return compiler
|
2017-06-18 15:44:01 +00:00
|
|
|
})
|
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// Start Builds
|
2018-01-13 05:22:11 +00:00
|
|
|
await sequence(
|
|
|
|
this.compilers,
|
|
|
|
compiler =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
const name = compiler.options.name
|
|
|
|
await this.nuxt.callHook('build:compile', { name, compiler })
|
|
|
|
|
|
|
|
// Resolve only when compiler emit done event
|
|
|
|
compiler.plugin('done', async stats => {
|
|
|
|
await this.nuxt.callHook('build:compiled', {
|
|
|
|
name,
|
|
|
|
compiler,
|
|
|
|
stats
|
|
|
|
})
|
|
|
|
// Reload renderer if available
|
|
|
|
this.nuxt.renderer.loadResources(sharedFS || require('fs'))
|
|
|
|
// Resolve on next tick
|
|
|
|
process.nextTick(resolve)
|
2017-06-18 11:50:43 +00:00
|
|
|
})
|
2018-01-13 05:22:11 +00:00
|
|
|
// --- Dev Build ---
|
|
|
|
if (this.options.dev) {
|
|
|
|
// Client Build, watch is started by dev-middleware
|
|
|
|
if (compiler.options.name === 'client') {
|
|
|
|
return this.webpackDev(compiler)
|
|
|
|
}
|
|
|
|
// DLL build, should run only once
|
|
|
|
if (compiler.options.name.includes('-dll')) {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
debug('[DLL] updated')
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Server, build and watch for changes
|
|
|
|
this.compilersWatching.push(
|
|
|
|
compiler.watch(this.options.watchers.webpack, err => {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (err) return reject(err)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// --- Production Build ---
|
|
|
|
compiler.run((err, stats) => {
|
2017-11-22 12:57:39 +00:00
|
|
|
/* istanbul ignore if */
|
2018-01-13 05:22:11 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err) // eslint-disable-line no-console
|
|
|
|
return reject(err)
|
|
|
|
}
|
2017-07-03 11:28:10 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
// Show build stats for production
|
|
|
|
console.log(stats.toString(this.webpackStats)) // eslint-disable-line no-console
|
2017-10-30 14:42:59 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (stats.hasErrors()) {
|
|
|
|
return reject(new Error('Webpack build exited with errors'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
webpackDev(compiler) {
|
2017-06-15 22:19:53 +00:00
|
|
|
debug('Adding webpack middleware...')
|
|
|
|
|
|
|
|
// Create webpack dev middleware
|
2018-01-13 05:22:11 +00:00
|
|
|
this.webpackDevMiddleware = promisify(
|
|
|
|
webpackDevMiddleware(
|
|
|
|
compiler,
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
publicPath: this.options.build.publicPath,
|
|
|
|
stats: this.webpackStats,
|
|
|
|
logLevel: 'silent',
|
|
|
|
watchOptions: this.options.watchers.webpack
|
|
|
|
},
|
|
|
|
this.options.build.devMiddleware
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-12-12 07:43:56 +00:00
|
|
|
this.webpackDevMiddleware.close = promisify(this.webpackDevMiddleware.close)
|
2017-11-24 03:43:01 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
this.webpackHotMiddleware = promisify(
|
|
|
|
webpackHotMiddleware(
|
|
|
|
compiler,
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
log: false,
|
|
|
|
heartbeat: 10000
|
|
|
|
},
|
|
|
|
this.options.build.hotMiddleware
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
// Inject to renderer instance
|
|
|
|
if (this.nuxt.renderer) {
|
|
|
|
this.nuxt.renderer.webpackDevMiddleware = this.webpackDevMiddleware
|
|
|
|
this.nuxt.renderer.webpackHotMiddleware = this.webpackHotMiddleware
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:19:53 +00:00
|
|
|
// Start watching files
|
2017-06-14 16:13:43 +00:00
|
|
|
this.watchFiles()
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
watchFiles() {
|
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-01-02 02:36:09 +00:00
|
|
|
patterns = _.map(patterns, p => upath.normalizeSafe(p))
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
const options = Object.assign({}, this.options.watchers.chokidar, {
|
|
|
|
ignoreInitial: true
|
|
|
|
})
|
|
|
|
/* istanbul ignore next */
|
2017-06-19 22:34:50 +00:00
|
|
|
const refreshFiles = _.debounce(() => this.generateRoutesAndFiles(), 200)
|
2017-06-15 22:19:53 +00:00
|
|
|
|
|
|
|
// Watch for src Files
|
2018-01-13 05:22:11 +00:00
|
|
|
this.filesWatcher = chokidar
|
|
|
|
.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-01-13 05:22:11 +00:00
|
|
|
const watchFiles = _.map(_.uniq(this.options.build.watch), p =>
|
|
|
|
upath.normalizeSafe(p)
|
|
|
|
)
|
|
|
|
this.customFilesWatcher = chokidar
|
|
|
|
.watch(watchFiles, options)
|
2017-06-11 14:17:36 +00:00
|
|
|
.on('change', refreshFiles)
|
|
|
|
}
|
2017-11-24 03:43:01 +00:00
|
|
|
|
|
|
|
async unwatch() {
|
|
|
|
if (this.filesWatcher) {
|
|
|
|
this.filesWatcher.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.customFilesWatcher) {
|
|
|
|
this.customFilesWatcher.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.compilersWatching.forEach(watching => watching.close())
|
|
|
|
|
|
|
|
// Stop webpack middleware
|
|
|
|
await this.webpackDevMiddleware.close()
|
|
|
|
}
|
2017-12-04 19:52:50 +00:00
|
|
|
|
2018-01-27 15:48:42 +00:00
|
|
|
// TODO: remove ignore when generateConfig enabled again
|
|
|
|
async generateConfig() /* istanbul ignore next */ {
|
2017-12-04 19:52:50 +00:00
|
|
|
const config = resolve(this.options.buildDir, 'build.config.js')
|
|
|
|
const options = _.omit(this.options, Options.unsafeKeys)
|
2018-01-13 05:22:11 +00:00
|
|
|
await writeFile(
|
|
|
|
config,
|
|
|
|
`module.exports = ${JSON.stringify(options, null, ' ')}`,
|
|
|
|
'utf8'
|
|
|
|
)
|
2017-12-04 19:52:50 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const STATUS = {
|
|
|
|
INITIAL: 1,
|
|
|
|
BUILD_DONE: 2,
|
|
|
|
BUILDING: 3
|
|
|
|
}
|