2016-11-07 01:34:58 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const debug = require('debug')('nuxt:build')
|
2017-01-11 19:14:59 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import co from 'co'
|
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import fs from 'fs-extra'
|
|
|
|
import hash from 'hash-sum'
|
|
|
|
import pify from 'pify'
|
|
|
|
import webpack from 'webpack'
|
2017-01-13 20:30:31 +00:00
|
|
|
import serialize from 'serialize-javascript'
|
2017-01-11 19:14:59 +00:00
|
|
|
import { createBundleRenderer } from 'vue-server-renderer'
|
|
|
|
import { join, resolve, sep } from 'path'
|
|
|
|
import clientWebpackConfig from './webpack/client.config.js'
|
|
|
|
import serverWebpackConfig from './webpack/server.config.js'
|
2016-11-11 14:30:11 +00:00
|
|
|
const remove = pify(fs.remove)
|
|
|
|
const readFile = pify(fs.readFile)
|
|
|
|
const writeFile = pify(fs.writeFile)
|
|
|
|
const mkdirp = pify(fs.mkdirp)
|
2016-11-24 00:47:11 +00:00
|
|
|
const glob = pify(require('glob'))
|
2016-11-20 10:37:53 +00:00
|
|
|
const reqSep = /\//g
|
|
|
|
const sysSep = _.escapeRegExp(sep)
|
|
|
|
const normalize = string => string.replace(reqSep, sysSep)
|
|
|
|
const wp = function (p) {
|
2016-12-21 18:27:36 +00:00
|
|
|
/* istanbul ignore if */
|
2016-11-20 10:37:53 +00:00
|
|
|
if (/^win/.test(process.platform)) {
|
|
|
|
p = p.replace(/\\/g, '\\\\')
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2016-11-16 17:06:54 +00:00
|
|
|
const r = function () {
|
2016-11-20 10:37:53 +00:00
|
|
|
let args = Array.from(arguments)
|
2016-11-16 17:06:54 +00:00
|
|
|
if (_.last(args).includes('~')) {
|
2016-11-20 10:37:53 +00:00
|
|
|
return wp(_.last(args))
|
2016-11-16 17:06:54 +00:00
|
|
|
}
|
2016-11-21 13:14:35 +00:00
|
|
|
args = args.map(normalize)
|
2016-11-20 10:37:53 +00:00
|
|
|
return wp(resolve.apply(null, args))
|
2016-11-16 17:06:54 +00:00
|
|
|
}
|
2017-01-11 19:14:59 +00:00
|
|
|
// force green color
|
|
|
|
debug.color = 2
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2016-11-09 01:35:13 +00:00
|
|
|
const defaults = {
|
2017-01-23 16:55:39 +00:00
|
|
|
analyze: false,
|
2017-02-13 12:34:54 +00:00
|
|
|
publicPath: '/_nuxt/',
|
2016-11-09 01:35:13 +00:00
|
|
|
filenames: {
|
|
|
|
css: 'style.css',
|
|
|
|
vendor: 'vendor.bundle.js',
|
|
|
|
app: 'nuxt.bundle.js'
|
|
|
|
},
|
|
|
|
vendor: [],
|
2016-11-17 21:12:21 +00:00
|
|
|
loaders: [],
|
2016-11-18 09:38:47 +00:00
|
|
|
plugins: [],
|
2016-11-22 23:47:31 +00:00
|
|
|
babel: {},
|
|
|
|
postcss: []
|
2016-11-09 01:35:13 +00:00
|
|
|
}
|
|
|
|
const defaultsLoaders = [
|
|
|
|
{
|
2016-11-17 21:12:21 +00:00
|
|
|
test: /\.(png|jpe?g|gif|svg)$/,
|
2016-11-14 22:59:54 +00:00
|
|
|
loader: 'url-loader',
|
2016-11-10 18:34:59 +00:00
|
|
|
query: {
|
2016-11-09 01:35:13 +00:00
|
|
|
limit: 1000, // 1KO
|
2017-01-09 14:10:22 +00:00
|
|
|
name: 'img/[name].[hash:7].[ext]'
|
2016-11-09 01:35:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
|
2016-11-14 22:59:54 +00:00
|
|
|
loader: 'url-loader',
|
2016-11-09 01:35:13 +00:00
|
|
|
query: {
|
|
|
|
limit: 1000, // 1 KO
|
|
|
|
name: 'fonts/[name].[hash:7].[ext]'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-11-22 23:47:31 +00:00
|
|
|
const defaultsPostcss = [
|
|
|
|
require('autoprefixer')({
|
|
|
|
browsers: ['last 3 versions']
|
|
|
|
})
|
|
|
|
]
|
2016-11-09 01:35:13 +00:00
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function options () {
|
2016-11-09 01:35:13 +00:00
|
|
|
// Defaults build options
|
2016-11-22 23:47:31 +00:00
|
|
|
let extraDefaults = {}
|
|
|
|
if (this.options.build && !Array.isArray(this.options.build.loaders)) extraDefaults.loaders = defaultsLoaders
|
|
|
|
if (this.options.build && !Array.isArray(this.options.build.postcss)) extraDefaults.postcss = defaultsPostcss
|
|
|
|
this.options.build = _.defaultsDeep(this.options.build, defaults, extraDefaults)
|
2017-02-16 17:16:00 +00:00
|
|
|
if (this.options.build.publicPath.indexOf('http') === 0) {
|
|
|
|
// activate only in production mode
|
|
|
|
if (this.dev) {
|
|
|
|
this.options.build.publicPath = defaults.publicPath
|
|
|
|
} else {
|
|
|
|
this.options.nuxtStatic = false
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 18:01:46 +00:00
|
|
|
// Production, create server-renderer
|
|
|
|
if (!this.dev) {
|
2016-11-09 22:59:41 +00:00
|
|
|
const serverConfig = getWebpackServerConfig.call(this)
|
|
|
|
const bundlePath = join(serverConfig.output.path, serverConfig.output.filename)
|
2016-12-07 18:01:46 +00:00
|
|
|
if (fs.existsSync(bundlePath)) {
|
|
|
|
const bundle = fs.readFileSync(bundlePath, 'utf8')
|
|
|
|
createRenderer.call(this, bundle)
|
2016-11-10 01:19:47 +00:00
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|
2016-12-07 18:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export function * build () {
|
2016-12-21 19:51:09 +00:00
|
|
|
// Check if pages dir exists and warn if not
|
2016-12-08 06:45:40 +00:00
|
|
|
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
|
|
|
|
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
|
2016-12-08 15:41:20 +00:00
|
|
|
console.error('> No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?') // eslint-disable-line no-console
|
2016-11-07 01:34:58 +00:00
|
|
|
} else {
|
2016-12-08 15:41:20 +00:00
|
|
|
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root') // eslint-disable-line no-console
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-11-10 01:19:47 +00:00
|
|
|
process.exit(1)
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-12-08 06:45:40 +00:00
|
|
|
debug(`App root: ${this.srcDir}`)
|
2016-11-07 01:34:58 +00:00
|
|
|
debug('Generating .nuxt/ files...')
|
2016-12-21 19:51:09 +00:00
|
|
|
// Create .nuxt/, .nuxt/components and .nuxt/dist folders
|
2016-11-11 14:30:11 +00:00
|
|
|
yield remove(r(this.dir, '.nuxt'))
|
2016-11-07 01:34:58 +00:00
|
|
|
yield mkdirp(r(this.dir, '.nuxt/components'))
|
2016-11-09 22:59:41 +00:00
|
|
|
if (!this.dev) {
|
2016-11-07 01:34:58 +00:00
|
|
|
yield mkdirp(r(this.dir, '.nuxt/dist'))
|
|
|
|
}
|
2016-11-10 02:38:11 +00:00
|
|
|
// Generate routes and interpret the template files
|
|
|
|
yield generateRoutesAndFiles.call(this)
|
2016-12-21 19:51:09 +00:00
|
|
|
// Generate .nuxt/dist/ files
|
|
|
|
yield buildFiles.call(this)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
function * buildFiles () {
|
2016-11-10 02:38:11 +00:00
|
|
|
if (this.dev) {
|
2017-02-03 14:09:27 +00:00
|
|
|
debug('Adding webpack middleware...')
|
|
|
|
createWebpackMiddleware.call(this)
|
2016-11-10 02:38:11 +00:00
|
|
|
webpackWatchAndUpdate.call(this)
|
|
|
|
watchPages.call(this)
|
|
|
|
} else {
|
|
|
|
debug('Building files...')
|
|
|
|
yield [
|
|
|
|
webpackRunClient.call(this),
|
|
|
|
webpackRunServer.call(this)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function * generateRoutesAndFiles () {
|
|
|
|
debug('Generating routes...')
|
2016-12-24 00:55:32 +00:00
|
|
|
// Layouts
|
|
|
|
let layouts = {}
|
|
|
|
const layoutsFiles = yield glob('layouts/*.vue', { cwd: this.srcDir })
|
|
|
|
layoutsFiles.forEach((file) => {
|
|
|
|
let name = file.split('/').slice(-1)[0].replace('.vue', '')
|
|
|
|
if (name === 'error') return
|
|
|
|
layouts[name] = r(this.srcDir, file)
|
|
|
|
})
|
2016-12-21 19:51:09 +00:00
|
|
|
// Generate routes based on files
|
2016-12-08 06:45:40 +00:00
|
|
|
const files = yield glob('pages/**/*.vue', { cwd: this.srcDir })
|
2016-12-12 20:16:12 +00:00
|
|
|
this.routes = _.uniq(_.map(files, (file) => {
|
2016-12-12 20:54:02 +00:00
|
|
|
return file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/index/g, '').replace(/_/g, ':').replace('', '/').replace(/\/{2,}/g, '/')
|
2016-12-12 20:16:12 +00:00
|
|
|
}))
|
2017-02-03 14:09:27 +00:00
|
|
|
if (typeof this.options.router.extendRoutes === 'function') {
|
|
|
|
// let the user extend the routes
|
|
|
|
this.options.router.extendRoutes(this.routes)
|
|
|
|
}
|
2016-12-21 19:51:09 +00:00
|
|
|
// Interpret and move template files to .nuxt/
|
2016-11-10 02:38:11 +00:00
|
|
|
debug('Generating files...')
|
2016-11-07 01:34:58 +00:00
|
|
|
let templatesFiles = [
|
|
|
|
'App.vue',
|
|
|
|
'client.js',
|
|
|
|
'index.js',
|
2017-02-03 14:09:27 +00:00
|
|
|
'middleware.js',
|
2016-11-07 01:34:58 +00:00
|
|
|
'router.js',
|
|
|
|
'server.js',
|
|
|
|
'utils.js',
|
2017-01-27 22:10:02 +00:00
|
|
|
'components/nuxt-error.vue',
|
2016-12-16 16:45:47 +00:00
|
|
|
'components/nuxt-loading.vue',
|
|
|
|
'components/nuxt-child.js',
|
|
|
|
'components/nuxt-link.js',
|
|
|
|
'components/nuxt.vue'
|
2016-11-07 01:34:58 +00:00
|
|
|
]
|
|
|
|
let templateVars = {
|
2016-11-20 11:23:48 +00:00
|
|
|
uniqBy: _.uniqBy,
|
2016-11-09 22:59:41 +00:00
|
|
|
isDev: this.dev,
|
2016-11-10 16:16:37 +00:00
|
|
|
router: {
|
|
|
|
base: this.options.router.base,
|
2017-02-03 14:09:27 +00:00
|
|
|
middleware: this.options.router.middleware,
|
2017-01-26 14:56:47 +00:00
|
|
|
linkActiveClass: this.options.router.linkActiveClass,
|
|
|
|
scrollBehavior: this.options.router.scrollBehavior
|
2016-11-10 16:16:37 +00:00
|
|
|
},
|
2016-12-04 18:15:11 +00:00
|
|
|
env: this.options.env,
|
2016-11-14 22:59:54 +00:00
|
|
|
head: this.options.head,
|
2017-02-03 14:09:27 +00:00
|
|
|
middleware: this.options.middleware,
|
2016-11-07 01:34:58 +00:00
|
|
|
store: this.options.store,
|
2016-11-07 22:26:53 +00:00
|
|
|
css: this.options.css,
|
2016-12-08 06:45:40 +00:00
|
|
|
plugins: this.options.plugins.map((p) => r(this.srcDir, p)),
|
2016-11-19 21:16:26 +00:00
|
|
|
appPath: './App.vue',
|
2016-12-24 00:55:32 +00:00
|
|
|
layouts: layouts,
|
2016-12-08 06:45:40 +00:00
|
|
|
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),
|
2016-11-21 18:53:11 +00:00
|
|
|
transition: this.options.transition,
|
2016-11-07 01:34:58 +00:00
|
|
|
components: {
|
2017-01-27 22:10:02 +00:00
|
|
|
ErrorPage: './nuxt-error.vue'
|
2016-11-10 16:16:37 +00:00
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-11-10 02:38:11 +00:00
|
|
|
// Format routes for the lib/app/router.js template
|
2016-12-12 20:16:12 +00:00
|
|
|
templateVars.router.routes = createRoutes(files, this.srcDir)
|
2017-01-18 16:25:38 +00:00
|
|
|
if (typeof this.options.router.extendRoutes === 'function') {
|
|
|
|
// let the user extend the routes
|
|
|
|
this.options.router.extendRoutes(templateVars.router.routes)
|
|
|
|
}
|
2016-12-24 00:55:32 +00:00
|
|
|
if (layoutsFiles.includes('layouts/error.vue')) {
|
2016-12-09 12:50:34 +00:00
|
|
|
templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue')
|
2016-12-07 12:30:25 +00:00
|
|
|
}
|
2016-12-24 00:55:32 +00:00
|
|
|
// If no default layout, create its folder and add the default folder
|
|
|
|
if (!layouts.default) {
|
|
|
|
yield mkdirp(r(this.dir, '.nuxt/layouts'))
|
|
|
|
templatesFiles.push('layouts/default.vue')
|
|
|
|
layouts.default = r(__dirname, 'app', 'layouts', 'default.vue')
|
|
|
|
}
|
2016-12-25 20:16:30 +00:00
|
|
|
// Add store if needed
|
|
|
|
if (this.options.store) {
|
|
|
|
templatesFiles.push('store.js')
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
let moveTemplates = templatesFiles.map((file) => {
|
2016-12-09 17:54:17 +00:00
|
|
|
return readFile(r(__dirname, 'app', file), 'utf8')
|
2016-11-07 01:34:58 +00:00
|
|
|
.then((fileContent) => {
|
2017-01-13 20:30:31 +00:00
|
|
|
const template = _.template(fileContent, {
|
2017-01-19 15:25:55 +00:00
|
|
|
imports: {
|
|
|
|
serialize,
|
|
|
|
hash
|
|
|
|
}
|
2017-01-13 20:30:31 +00:00
|
|
|
})
|
2016-11-07 01:34:58 +00:00
|
|
|
const content = template(templateVars)
|
|
|
|
return writeFile(r(this.dir, '.nuxt', file), content, 'utf8')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
yield moveTemplates
|
|
|
|
}
|
|
|
|
|
2016-12-12 13:16:47 +00:00
|
|
|
function createRoutes (files, srcDir) {
|
2016-12-11 00:46:04 +00:00
|
|
|
let routes = []
|
|
|
|
files.forEach((file) => {
|
|
|
|
let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
|
2017-01-19 15:25:55 +00:00
|
|
|
let route = { name: '', path: '', component: r(srcDir, file) }
|
2016-12-11 00:46:04 +00:00
|
|
|
let parent = routes
|
|
|
|
keys.forEach((key, i) => {
|
2016-12-23 16:31:42 +00:00
|
|
|
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
|
2017-02-03 19:23:28 +00:00
|
|
|
route.name += (key === '_') ? 'all' : ''
|
2016-12-11 00:46:04 +00:00
|
|
|
let child = _.find(parent, { name: route.name })
|
|
|
|
if (child) {
|
|
|
|
if (!child.children) {
|
|
|
|
child.children = []
|
|
|
|
}
|
|
|
|
parent = child.children
|
2016-12-23 16:31:42 +00:00
|
|
|
route.path = ''
|
2016-12-11 00:46:04 +00:00
|
|
|
} else {
|
2016-12-23 16:31:42 +00:00
|
|
|
if (key === 'index' && (i + 1) === keys.length) {
|
|
|
|
route.path += (i > 0 ? '' : '/')
|
|
|
|
} else {
|
2017-02-03 19:23:28 +00:00
|
|
|
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
|
|
|
|
if (key !== '_' && key.indexOf('_') !== -1) {
|
2016-12-23 16:31:42 +00:00
|
|
|
route.path += '?'
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 00:46:04 +00:00
|
|
|
}
|
|
|
|
})
|
2016-12-11 15:40:49 +00:00
|
|
|
// Order Routes path
|
2016-12-23 14:43:01 +00:00
|
|
|
parent.push(route)
|
|
|
|
parent.sort((a, b) => {
|
2017-02-09 00:18:29 +00:00
|
|
|
if (!a.path.length || a.path === '/') { return -1 }
|
|
|
|
if (!b.path.length || b.path === '/') { return 1 }
|
2017-02-03 19:23:28 +00:00
|
|
|
var res = 0
|
|
|
|
var _a = a.path.split('/')
|
|
|
|
var _b = b.path.split('/')
|
2017-02-10 20:24:17 +00:00
|
|
|
for (var i = 0; i < _a.length; i++) {
|
|
|
|
if (res !== 0) { break }
|
|
|
|
var y = (_a[i].indexOf('*') > -1) ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
|
|
|
|
var z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
|
|
|
|
res = y - z
|
|
|
|
if (i === _b.length - 1 && res === 0) {
|
|
|
|
res = 1
|
2017-02-03 19:23:28 +00:00
|
|
|
}
|
2017-02-10 20:24:17 +00:00
|
|
|
}
|
|
|
|
return res === 0 ? -1 : res
|
2016-12-23 14:43:01 +00:00
|
|
|
})
|
2016-12-11 00:46:04 +00:00
|
|
|
})
|
|
|
|
return cleanChildrenRoutes(routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanChildrenRoutes (routes, isChild = false) {
|
2017-02-10 20:24:17 +00:00
|
|
|
let start = -1
|
|
|
|
let routesIndex = []
|
2016-12-11 00:46:04 +00:00
|
|
|
routes.forEach((route) => {
|
2017-02-10 20:24:17 +00:00
|
|
|
if (/-index$/.test(route.name) || route.name === 'index') {
|
|
|
|
// Save indexOf 'index' key in name
|
|
|
|
let res = route.name.split('-')
|
|
|
|
let s = res.indexOf('index')
|
|
|
|
start = (start === -1 || s < start) ? s : start
|
|
|
|
routesIndex.push(res)
|
2017-01-11 14:03:42 +00:00
|
|
|
}
|
2017-02-10 20:24:17 +00:00
|
|
|
})
|
|
|
|
routes.forEach((route) => {
|
|
|
|
route.path = (isChild) ? route.path.replace('/', '') : route.path
|
|
|
|
if (route.path.indexOf('?') > -1) {
|
|
|
|
let names = route.name.split('-')
|
|
|
|
let paths = route.path.split('/')
|
|
|
|
if (!isChild) { paths.shift() } // clean first / for parents
|
|
|
|
routesIndex.forEach((r) => {
|
|
|
|
let i = r.indexOf('index') - start // children names
|
|
|
|
if (i < paths.length) {
|
|
|
|
for (var a = 0; a <= i; a++) {
|
|
|
|
if (a === i) { paths[a] = paths[a].replace('?', '') }
|
|
|
|
if (a < i && names[a] !== r[a]) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
route.path = (isChild ? '' : '/') + paths.join('/')
|
2016-12-20 16:30:43 +00:00
|
|
|
}
|
2016-12-23 16:31:42 +00:00
|
|
|
route.name = route.name.replace(/-index$/, '')
|
2016-12-11 00:46:04 +00:00
|
|
|
if (route.children) {
|
2017-01-11 11:52:39 +00:00
|
|
|
if (route.children.find((child) => child.path === '')) {
|
|
|
|
delete route.name
|
|
|
|
}
|
2016-12-11 00:46:04 +00:00
|
|
|
route.children = cleanChildrenRoutes(route.children, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
function getWebpackClientConfig () {
|
2016-12-09 17:54:17 +00:00
|
|
|
return clientWebpackConfig.call(this)
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getWebpackServerConfig () {
|
2016-12-09 17:54:17 +00:00
|
|
|
return serverWebpackConfig.call(this)
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:09:27 +00:00
|
|
|
function createWebpackMiddleware () {
|
2016-11-07 01:34:58 +00:00
|
|
|
const clientConfig = getWebpackClientConfig.call(this)
|
|
|
|
// setup on the fly compilation + hot-reload
|
2016-12-27 13:53:36 +00:00
|
|
|
clientConfig.entry.app = _.flatten(['webpack-hot-middleware/client?reload=true', clientConfig.entry.app])
|
2016-11-07 01:34:58 +00:00
|
|
|
clientConfig.plugins.push(
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
2017-01-11 21:18:47 +00:00
|
|
|
new webpack.NoEmitOnErrorsPlugin()
|
2016-11-07 01:34:58 +00:00
|
|
|
)
|
|
|
|
const clientCompiler = webpack(clientConfig)
|
2017-02-03 14:09:27 +00:00
|
|
|
// Add the middleware to the instance context
|
2016-11-07 01:34:58 +00:00
|
|
|
this.webpackDevMiddleware = pify(require('webpack-dev-middleware')(clientCompiler, {
|
|
|
|
publicPath: clientConfig.output.publicPath,
|
|
|
|
stats: {
|
|
|
|
colors: true,
|
|
|
|
chunks: false
|
|
|
|
},
|
2016-11-07 22:26:53 +00:00
|
|
|
quiet: false,
|
2016-11-07 01:34:58 +00:00
|
|
|
noInfo: true
|
|
|
|
}))
|
|
|
|
this.webpackHotMiddleware = pify(require('webpack-hot-middleware')(clientCompiler))
|
|
|
|
}
|
|
|
|
|
|
|
|
function webpackWatchAndUpdate () {
|
|
|
|
const MFS = require('memory-fs') // <- dependencies of webpack
|
|
|
|
const mfs = new MFS()
|
|
|
|
const serverConfig = getWebpackServerConfig.call(this)
|
|
|
|
const serverCompiler = webpack(serverConfig)
|
|
|
|
const outputPath = join(serverConfig.output.path, serverConfig.output.filename)
|
|
|
|
serverCompiler.outputFileSystem = mfs
|
|
|
|
this.webpackServerWatcher = serverCompiler.watch({}, (err, stats) => {
|
|
|
|
if (err) throw err
|
|
|
|
stats = stats.toJson()
|
2016-12-08 15:41:20 +00:00
|
|
|
stats.errors.forEach(err => console.error(err)) // eslint-disable-line no-console
|
|
|
|
stats.warnings.forEach(err => console.warn(err)) // eslint-disable-line no-console
|
2016-11-07 01:34:58 +00:00
|
|
|
createRenderer.call(this, mfs.readFileSync(outputPath, 'utf-8'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function webpackRunClient () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const clientConfig = getWebpackClientConfig.call(this)
|
|
|
|
const serverCompiler = webpack(clientConfig)
|
|
|
|
serverCompiler.run((err, stats) => {
|
|
|
|
if (err) return reject(err)
|
2016-12-08 15:41:20 +00:00
|
|
|
console.log('[nuxt:build:client]\n', stats.toString({ chunks: false, colors: true })) // eslint-disable-line no-console
|
2017-01-20 17:55:30 +00:00
|
|
|
if (stats.hasErrors()) return reject('Webpack build exited with errors')
|
2016-11-07 01:34:58 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function webpackRunServer () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const serverConfig = getWebpackServerConfig.call(this)
|
|
|
|
const serverCompiler = webpack(serverConfig)
|
|
|
|
serverCompiler.run((err, stats) => {
|
|
|
|
if (err) return reject(err)
|
2016-12-08 15:41:20 +00:00
|
|
|
console.log('[nuxt:build:server]\n', stats.toString({ chunks: false, colors: true })) // eslint-disable-line no-console
|
2017-01-20 17:55:30 +00:00
|
|
|
if (stats.hasErrors()) return reject('Webpack build exited with errors')
|
2016-11-07 01:34:58 +00:00
|
|
|
const bundlePath = join(serverConfig.output.path, serverConfig.output.filename)
|
2016-11-11 14:30:11 +00:00
|
|
|
readFile(bundlePath, 'utf8')
|
|
|
|
.then((bundle) => {
|
|
|
|
createRenderer.call(this, bundle)
|
|
|
|
resolve()
|
|
|
|
})
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRenderer (bundle) {
|
|
|
|
// Create bundle renderer to give a fresh context for every request
|
|
|
|
let cacheConfig = false
|
|
|
|
if (this.options.cache) {
|
|
|
|
this.options.cache = (typeof this.options.cache !== 'object' ? {} : this.options.cache)
|
|
|
|
cacheConfig = require('lru-cache')(_.defaults(this.options.cache, {
|
|
|
|
max: 1000,
|
|
|
|
maxAge: 1000 * 60 * 15
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
this.renderer = createBundleRenderer(bundle, {
|
|
|
|
cache: cacheConfig
|
|
|
|
})
|
|
|
|
this.renderToString = pify(this.renderer.renderToString)
|
|
|
|
this.renderToStream = this.renderer.renderToStream
|
|
|
|
}
|
2016-11-10 02:38:11 +00:00
|
|
|
|
|
|
|
function watchPages () {
|
2016-12-24 13:15:12 +00:00
|
|
|
const patterns = [
|
2016-12-24 16:58:52 +00:00
|
|
|
r(this.srcDir, 'pages'),
|
2016-12-24 13:15:12 +00:00
|
|
|
r(this.srcDir, 'pages/*.vue'),
|
|
|
|
r(this.srcDir, 'pages/**/*.vue'),
|
2016-12-24 16:58:52 +00:00
|
|
|
r(this.srcDir, 'layouts'),
|
2016-12-24 13:15:12 +00:00
|
|
|
r(this.srcDir, 'layouts/*.vue'),
|
|
|
|
r(this.srcDir, 'layouts/**/*.vue')
|
|
|
|
]
|
2016-11-10 02:38:11 +00:00
|
|
|
const options = {
|
|
|
|
ignoreInitial: true
|
|
|
|
}
|
2016-12-21 19:51:09 +00:00
|
|
|
/* istanbul ignore next */
|
2016-11-10 02:38:11 +00:00
|
|
|
const refreshFiles = _.debounce(() => {
|
|
|
|
var d = Date.now()
|
|
|
|
co(generateRoutesAndFiles.bind(this))
|
|
|
|
.then(() => {
|
2016-12-08 15:41:20 +00:00
|
|
|
console.log('Time to gen:' + (Date.now() - d) + 'ms') // eslint-disable-line no-console
|
2016-11-10 02:38:11 +00:00
|
|
|
})
|
|
|
|
}, 200)
|
|
|
|
this.pagesFilesWatcher = chokidar.watch(patterns, options)
|
|
|
|
.on('add', refreshFiles)
|
|
|
|
.on('unlink', refreshFiles)
|
|
|
|
}
|