Add analyse option

This commit is contained in:
Sébastien Chopin 2017-01-23 17:55:39 +01:00
parent e238a952e2
commit 7552da1453
5 changed files with 32 additions and 1 deletions

View File

@ -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()

View File

@ -40,6 +40,7 @@ const r = function () {
debug.color = 2
const defaults = {
analyze: false,
filenames: {
css: 'style.css',
vendor: 'vendor.bundle.js',

View File

@ -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
}

View File

@ -20,6 +20,10 @@ module.exports = {
string: 'Nuxt.js'
},
build: {
analyze: {
analyzerMode: 'disabled',
generateStatsFile: true
},
extend (config, options) {
config.devtool = 'nosources-source-map'
}

View File

@ -54,6 +54,11 @@ test('/test/about-bis (added with extendRoutes)', async t => {
t.true(html.includes('<h1>About page</h1>'))
})
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()