text: /**
   * Created by sail on 2017/6/11.
   */
  const fs = require('fs')
  const path = require('path')
  const rollup = require('rollup')
  const uglify = require('uglify-js')
  const zlib = require('zlib')
  const configs = require('./config')
  
  if (!fs.existsSync('dist')) {
    fs.mkdirSync('dist')
  }
  
  build(Object.keys(configs).map(key => configs[key]))
  
  function build (builds) {
    let built = 0
    const total = builds.length
    const next = () => {
      buildEntry(builds[built]).then(() => {
        built++
        if (built < total) {
          next()
        }
      }).catch(logError)
    }
    
    next()
  }
  
  function buildEntry ({ input, output }) {
    const isProd = /min\.js$/.test(output.file)
    return rollup.rollup(input)
      .then(bundle => bundle.generate(output))
      .then(({ code }) => {
        if (isProd) {
          var minified = (output.banner ? output.banner + '\n' : '') + uglify.minify(code, {
            output: {
              /* eslint-disable camelcase */
              ascii_only: true
              /* eslint-enable camelcase */
            }
          }).code
          return write(output.file, minified, true)
        } else {
          return write(output.file, code)
        }
      })
  }
  
  function write (dest, code, zip) {
    return new Promise((resolve, reject) => {
      function report (extra) {
        console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
        resolve()
      }
      
      fs.writeFile(dest, code, err => {
        if (err) return reject(err)
        if (zip) {
          zlib.gzip(code, (err, zipped) => {
            if (err) return reject(err)
            report(' (gzipped: ' + getSize(zipped) + ')')
          })
        } else {
          report()
        }
      })
    })
  }
  
  function getSize (code) {
    return (code.length / 1024).toFixed(2) + 'kb'
  }
  
  function logError (e) {
    console.log(e)
  }
  
  function blue (str) {
    return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
  }
  
  // rollup.rollup({
  // 	entry: 'src/main.js',
  // 	plugins: [
  // 		json(),
  // 		babel()
  // 	]
  // })
  // .then(bundle => bundle.generate({
  // 	format: 'umd',
  // 	moduleName: 'weCropper',
  // 	dest: 'dist/weCropper.js'
  // }))
  // .then(({ code }) => {
  // 	console.log(code)
  // })
  // .catch(err => console.error(err))