lsm/micromono

View on GitHub
bin/micromono-bundle

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

var path = require('path')
var program = require('cmdenv')('micromono')
var SuperPipe = require('superpipe')
var AssetPipe = require('../lib/web/asset')
var bundleAsset = require('../lib/web/asset/pipeline').bundleAsset
var LocalServicePipe = require('../lib/service/local')


program
  .usage('[OPTIONS] path')
  .description('Bundle asset files for a service')
  .option('-d --bundle-deps', 'Include dependencies when bundling. Default false.')
  .option('-o --out-file [path]', 'Set the path of the output file.')
  .option('--source-maps', 'Enable source maps. Default false.')
  .option('-m --source-map-contents', 'Enable source maps. Default false.')
  .option('--low-res-source-maps', 'Generate low resolution source maps. Default false.')
  .option('-i --inject', 'Inject bundle info into `config.js`. Default false.')
  .option('-z --minify', 'Minify the output files. Default false.')
  .option('-g --mangle', 'Default false.')
  .option('-c --bundle-css', 'Bundle CSS files. Default false.')
  .option('-s --separate-css', 'Bundle CSS into a separate file. Default false.')
  .option('--production', 'Enable all options suitable for production.')
  .option('--common-bundles', 'Bundle dependencies based on the value of the '
    + 'commonBundles property defined in package.json. Default false.')
  .parse(process.argv)

var servicePath = program.args[0]
if (servicePath) {
  servicePath = path.resolve(servicePath)

  var superpipe = new SuperPipe()
  superpipe
    .setDep(AssetPipe, '*^')
    .setDep(LocalServicePipe, '*^')
    .setDep('packagePath', servicePath)
    .setDep('service', {})
    .setDep('errorHandler', function(err, errPipeName) {
      console.error('[%s] `bundle` error', errPipeName, err && err.stack || err)
      process.exit(-1)
    })

  var options = {
    bundleDeps: program.bundleDeps || false,
    sourceMaps: program.sourceMaps || false,
    sourceMapContents: program.sourceMapContents || false,
    lowResSourceMaps: program.lowResSourceMaps || false,
    inject: program.inject || false,
    minify: program.minify || false,
    mangle: program.mangle || false,
    buildCss: program.buildCss || false,
    separateCss: program.separateCss || false,
    commonBundles: program.commonBundles || false
  }

  if (program.production) {
    options = {
      bundleDeps: program.bundleDeps || false,
      sourceMaps: program.sourceMaps || false,
      sourceMapContents: program.sourceMapContents || false,
      lowResSourceMaps: program.lowResSourceMaps || true,
      inject: program.inject || true,
      minify: program.minify || true,
      mangle: program.mangle || true,
      buildCss: program.buildCss || true,
      separateCss: program.separateCss || true,
      commonBundles: program.commonBundles || false
    }
  }

  if (program.outFile)
    options.outFile = program.outFile

  console.log('bundleOptions:', options)

  superpipe.setDep('bundleOptions', options)

  var bundlePipeline
  if (options.commonBundles) {
    var pubPath
    superpipe.setDep('bundleCommon', function(bundle, deps, outFile, assetInfo, packagePath, bundleOptions, jspmBinPath, setDep) {
      if (!deps || !Array.isArray(deps) || 0 === deps.length) {
        console.log('Nothing to bundle for %s', outFile)
        return true
      }
      var bundleCmd = 'bundle ' + deps.join(' + ')
      bundleOptions.outFile = path.join(pubPath, outFile)
      bundleCmd += AssetPipe.convertBundleOptionsToStr(bundleOptions)
      bundle(assetInfo, packagePath, jspmBinPath, bundleCmd, bundleOptions, setDep)
    })

    superpipe.setDep('cleanAssetInfo', function(assetInfo) {
      // Common bundle files should not be set as main bundle files
      assetInfo.bundleJs = ''
      assetInfo.bundleCss = ''
    })

    bundlePipeline = bundleAsset.slice(0, 4).connect(superpipe)
      .pipe(function(assetInfo, publicPath, setDep) {
        pubPath = publicPath
        setDep({
          'outCommon70': 'bundle-common70.js',
          'outCommon50': 'bundle-common50.js',
          'outCommon30': 'bundle-common30.js',
          'outCommon0': 'bundle-common0.js'
        })
        setDep(assetInfo.commonBundles)
      }, ['assetInfo', 'publicPath', 'setDep'])
      .pipe('bundleCommon', ['bundle', 'common0', 'outCommon0',
        'assetInfo', 'packagePath', 'bundleOptions', 'jspmBinPath', 'setDep'])
      .pipe('bundleCommon', ['bundle', 'common30', 'outCommon30',
        'assetInfo', 'packagePath', 'bundleOptions', 'jspmBinPath', 'setDep'])
      .pipe('bundleCommon', ['bundle', 'common50', 'outCommon50',
        'assetInfo', 'packagePath', 'bundleOptions', 'jspmBinPath', 'setDep'])
      .pipe('bundleCommon', ['bundle', 'common70', 'outCommon70',
        'assetInfo', 'packagePath', 'bundleOptions', 'jspmBinPath', 'setDep'])
      .pipe('cleanAssetInfo', 'assetInfo')
      .pipe('updatePackageJSON', ['assetInfo', 'packagePath', 'packageJSON', 'next'])
  } else {
    bundlePipeline = bundleAsset.clone(superpipe)
      .pipe(function(bundleCmd) {
        console.log('jspm ' + bundleCmd)
        console.log('Bundled successfully')
        process.exit(0)
      }, ['bundleCmd'])
  }
  bundlePipeline.error('errorHandler', ['error', 'errPipeName'])()
} else {
  program.outputHelp()
}