Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/bs / src / bundle / analyze.ts
Size: Mime:
/**
 * This script creates a webpack stats file on our production build of the
 * client bundle and then launches the webpack-bundle-analyzer tool allowing
 * you to easily see what is being included within your bundle.
 *
 * @see https://github.com/th0r/webpack-bundle-analyzer
 */

import { resolve } from 'path'
import { writeFileSync } from 'fs'
import webpack from 'webpack'
import { config as oneConfig } from '@skava/di'
import { exec } from '../utils'
import config from '../oneConfig/getConfigForEnv'
import webpackConfigFactory from './webpackConfigFactory'

const appRootDir = oneConfig.get('appRootDir')
// eslint-disable-next-line no-unused-vars
const [x, y, ...args] = process.argv
const analyzeServer = args.findIndex(arg => arg === '--server') !== -1
const analyzeClient = args.findIndex(arg => arg === '--client') !== -1

let target

if (analyzeServer) {
  target = 'server'
} else if (analyzeClient) {
  target = 'client'
} else {
  throw new Error('Please specify --server OR --client as target')
}

const anaylzeFilePath = resolve(appRootDir, config('bundles.client.outputPath'), '__analyze__.json')

const clientCompiler = webpack(webpackConfigFactory({ target, optimize: true }))

clientCompiler.run((compilationError, stats) => {
  if (compilationError) {
    console.error(compilationError)
  } else {
    // Write out the json stats file.
    writeFileSync(anaylzeFilePath, JSON.stringify(stats.toJson('detailed'), null, 4))

    // Run the bundle analyzer against the stats file.
    const cmd = `webpack-bundle-analyzer ${anaylzeFilePath} ${config('bundles.client.outputPath')}`
    exec(cmd)
  }
})