diff --git a/bin/nuxt-build b/bin/nuxt-build index 7f6e057169..50e2fbe62f 100755 --- a/bin/nuxt-build +++ b/bin/nuxt-build @@ -4,9 +4,17 @@ process.env.DEBUG = 'nuxt:*' var fs = require('fs') +var without = require('lodash').without var Nuxt = require('../') var resolve = require('path').resolve +// --analyze option +var analyzeBuild = false +if (process.argv.indexOf('--analyze') !== -1 || process.argv.indexOf('-a') !== -1) { + analyzeBuild = true + process.argv = without(process.argv, '--analyze', '-a') +} + var rootDir = resolve(process.argv.slice(2)[0] || '.') var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js') @@ -19,6 +27,9 @@ if (typeof options.rootDir !== 'string') { } options.dev = false // Create production build when calling `nuxt build` +options.build = options.build || {} +options.build.analyze = analyzeBuild + console.log('[nuxt] Building...') // eslint-disable-line no-console var nuxt = new Nuxt(options) nuxt.build() diff --git a/lib/build.js b/lib/build.js index 3635e3526b..743bb57b1d 100644 --- a/lib/build.js +++ b/lib/build.js @@ -40,6 +40,7 @@ const r = function () { debug.color = 2 const defaults = { + analyze: false, filenames: { css: 'style.css', vendor: 'vendor.bundle.js', diff --git a/lib/render.js b/lib/render.js index e5b47495cf..1b7c1f2dce 100644 --- a/lib/render.js +++ b/lib/render.js @@ -20,7 +20,7 @@ export function render (req, res) { } const self = this const context = getContext(req, res) - co(function * () { + return co(function * () { if (self.dev) { // Call webpack middlewares only in development yield self.webpackDevMiddleware(req, res) diff --git a/lib/webpack/client.config.js b/lib/webpack/client.config.js index 6c2e68549d..85f12a0e51 100644 --- a/lib/webpack/client.config.js +++ b/lib/webpack/client.config.js @@ -3,9 +3,9 @@ import { each } from 'lodash' import webpack from 'webpack' import ExtractTextPlugin from 'extract-text-webpack-plugin' +import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' import base from './base.config.js' import { resolve } from 'path' - /* |-------------------------------------------------------------------------- | Webpack Client Config @@ -79,5 +79,15 @@ export default function () { isClient: true }) } + // Webpack Bundle Analyzer + if (!this.dev && this.options.build.analyze) { + let options = {} + if (typeof this.options.build.analyze === 'object') { + options = this.options.build.analyze + } + config.plugins.push( + new BundleAnalyzerPlugin(options) + ) + } return config } diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 3d97739dad..e085010524 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -20,6 +20,10 @@ module.exports = { string: 'Nuxt.js' }, build: { + analyze: { + analyzerMode: 'disabled', + generateStatsFile: true + }, extend (config, options) { config.devtool = 'nosources-source-map' } diff --git a/test/with-config.test.js b/test/with-config.test.js index ba7e145c69..42c4da447a 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -54,6 +54,11 @@ test('/test/about-bis (added with extendRoutes)', async t => { t.true(html.includes('