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 / scripts / 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 } from '@skava/di'
import { exec } from '../utils'
import getConfigForEnv from '../../config/oneConfig/getConfigForEnv'
import { webpackConfigFactory } from './webpackConfigFactory'

const appRootDir = config.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
const clientOutputPath = getConfigForEnv('bundles.client.outputPath')

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,
  clientOutputPath,
  '__analyze__.json'
)

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

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

    // Run the bundle analyzer against the stats file.
    const cmd = `webpack-bundle-analyzer ${anaylzeFilePath} ${clientOutputPath}`
    exec(cmd)
  }
})