var fs = require('fs')
var zlib = require('zlib')
var rollup = require('rollup')
var uglify = require('uglify-js')
var babel = require('rollup-plugin-babel')
var replace = require('rollup-plugin-replace')
var alias = require('rollup-plugin-alias')
var version = process.env.VERSION || require('../package.json').version

var banner =
  '/*!\n' +
  ' * Vue.js v' + version + '\n' +
  ' * (c) ' + new Date().getFullYear() + ' Evan You\n' +
  ' * Released under the MIT License.\n' +
  ' */'

// update main file
var main = fs
  .readFileSync('src/runtime/index.js', 'utf-8')
  .replace(/Vue\.version = '[\d\.]+'/, "Vue.version = '" + version + "'")
fs.writeFileSync('src/runtime/index.js', main)

// CommonJS build.
// this is used as the "main" field in package.json
// and used by bundlers like Webpack and Browserify.
// runtime only, because it's meant to be
// used with vue-loader which pre-compiles the template.
rollup.rollup({
  entry: 'src/runtime/index.js',
  plugins: [babel()]
})
.then(function (bundle) {
  var code = bundle.generate({
    format: 'cjs',
    banner: banner
  }).code
  var minified = banner + '\n' + uglify.minify(code, {
    fromString: true,
    output: {
      ascii_only: true
    }
  }).code
  return Promise.all([
    write('dist/vue.common.js', code),
    write('dist/vue.common.min.js', minified)
  ])
})
.then(zip('dist/vue.common.min.js'))
// Compiler CommonJS build.
// Used in Node loaders/transforms.
.then(function () {
  return rollup.rollup({
    entry: 'src/compiler/index.js',
    plugins: [babel()]
  })
  .then(function (bundle) {
    write('dist/compiler/compiler.js', bundle.generate({
      format: 'cjs'
    }).code)
  })
})
// Standalone Dev Build
.then(function () {
  return rollup.rollup({
    entry: 'src/runtime-with-compiler.js',
    plugins: [
      alias({
        entities: './entity-decoder'
      }),
      replace({
        'process.env.NODE_ENV': "'development'"
      }),
      babel()
    ]
  })
  .then(function (bundle) {
    return write('dist/vue.js', bundle.generate({
      format: 'umd',
      banner: banner,
      moduleName: 'Vue'
    }).code)
  })
})
.then(function () {
  // Standalone Production Build
  return rollup.rollup({
    entry: 'src/runtime-with-compiler.js',
    plugins: [
      alias({
        entities: './entity-decoder'
      }),
      replace({
        'process.env.NODE_ENV': "'production'"
      }),
      babel()
    ]
  })
  .then(function (bundle) {
    var code = bundle.generate({
      format: 'umd',
      moduleName: 'Vue'
    }).code
    var minified = banner + '\n' + uglify.minify(code, {
      fromString: true,
      output: {
        ascii_only: true
      }
    }).code
    return write('dist/vue.min.js', minified)
  })
  .then(zip('dist/vue.min.js'))
})
.catch(logError)

function write (dest, code) {
  return new Promise(function (resolve, reject) {
    fs.writeFile(dest, code, function (err) {
      if (err) return reject(err)
      console.log(blue(dest) + ' ' + getSize(code))
      resolve()
    })
  })
}

function zip (file) {
  return function () {
    return new Promise(function (resolve, reject) {
      fs.readFile(file, function (err, buf) {
        if (err) return reject(err)
        zlib.gzip(buf, function (err, buf) {
          if (err) return reject(err)
          write(file + '.gz', buf).then(resolve)
        })
      })
    })
  }
}

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

@@ -1095,247 +1095,530 @@
 e%0A  
-var minified = banner + '%5Cn' + uglify.minify(code, %7B%0A    fromString: true,%0A    output: %7B
+return write('dist/vue.common.js', code)%0A%7D)%0A// production CommonJS build, just for file size monitoring.%0A.then(function () %7B%0A  return rollup.rollup(%7B%0A    entry: 'src/runtime/index.js',%0A    plugins: %5B
 %0A      
-ascii_only: true%0A    %7D%0A  %7D).code%0A  return Promise.all(%5B%0A    write('dist/vue.common.js', code),%0A    write('dist/vue.common.min.js', minified)%0A  %5D
+replace(%7B%0A        'process.env.NODE_ENV': %22'production'%22%0A      %7D),%0A      babel()%0A    %5D%0A  %7D)%0A%7D).then(function (bundle) %7B%0A  var code = bundle.generate(%7B%0A    format: 'cjs'%0A  %7D).code%0A  return write('dist/vue.common.min.js', uglify.minify(code, %7B%0A    fromString: true,%0A    output: %7B%0A      ascii_only: true%0A    %7D%0A  %7D).code
 )%0A%7D)

update build script