From 7552da1453a8079b9f321dceb6d6f22fca645951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 23 Jan 2017 17:55:39 +0100 Subject: [PATCH] Add analyse option --- bin/nuxt-build | 11 +++++++++++ lib/build.js | 1 + lib/webpack/client.config.js | 12 +++++++++++- test/fixtures/with-config/nuxt.config.js | 4 ++++ test/with-config.test.js | 5 +++++ 5 files changed, 32 insertions(+), 1 deletion(-) 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/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('

About page

')) }) +test('Check stats.json generated by build.analyze', t => { + const stats = require(resolve(__dirname, 'fixtures/with-config/.nuxt/dist/stats.json')) + t.is(stats.assets.length, 10) +}) + // Close server and ask nuxt to stop listening to file changes test.after('Closing server and nuxt.js', t => { server.close()