const rollup = require('rollup').rollup
const replace = require('rollup-plugin-replace')
const nodeResolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
const uglify = require('rollup-plugin-uglify')
const meta = require('../package.json')

const banner = `/*!
 * Sinai v${meta.version}
 * ${meta.homepage}
 *
 * @license
 * Copyright (c) 2017 ${meta.author}
 * Released under the MIT license
 * ${meta.homepage}/blob/master/LICENSE
 */`

const configs = [
  {
    format: 'es',
    dest: `dist/${meta.name}.esm.js`
  },
  {
    format: 'cjs',
    dest: `dist/${meta.name}.cjs.js`
  },
  {
    format: 'umd',
    env: 'development',
    dest: `dist/${meta.name}.js`
  },
  {
    format: 'umd',
    env: 'production',
    dest: `dist/${meta.name}.min.js`
  }
]

const baseConfig = {
  entry: 'lib/index.js',
  moduleName: 'Sinai',
  banner,
  external: ['vue'],
  globals: {
    vue: 'Vue'
  },
  plugins: [
    nodeResolve({
      jsnext: true,
      main: true
    }),
    commonjs({
      sourceMap: false
    })
  ]
}

function build (c) {
  const config = Object.assign({}, baseConfig, {
    format: c.format,
    dest: c.dest,
    plugins: baseConfig.plugins.slice()
  })

  if (c.env) {
    config.plugins.push(
      replace({
        'process.env.NODE_ENV': JSON.stringify(c.env)
      })
    )
  }

  if (c.env === 'production') {
    config.plugins.push(
      uglify({
        output: {
          comments (node, comment) {
            const text = comment.value;
            const type = comment.type;
            if (type == "comment2") {
              return /@preserve|@license|@cc_on/i.test(text);
            }
          }
        }
      })
    )
  }

  rollup(config)
    .then(bundle => {
      return bundle.write(config)
    })
    .catch(err => {
      console.error(err)
    })
}

configs.forEach(build)
@@ -826,13 +826,13 @@
 %7B%0A  
-entry
+input
 : 'l
@@ -851,15 +851,23 @@
 ,%0A  
-moduleN
+output: %7B%0A    n
 ame:
@@ -876,16 +876,18 @@
 Sinai',%0A
+  
   banner
@@ -894,27 +894,8 @@
 ,%0A  
-external: %5B'vue'%5D,%0A
   gl
@@ -899,24 +899,26 @@
  globals: %7B%0A
+  
     vue: 'Vu
@@ -922,17 +922,44 @@
 'Vue'%0A  
-%7D
+  %7D%0A  %7D,%0A  external: %5B'vue'%5D
 ,%0A  plug
@@ -1150,16 +1150,69 @@
 nfig, %7B%0A
+    output: Object.assign(%7B%7D, baseConfig.output, %7B%0A  
     form
@@ -1233,20 +1233,22 @@
     
-dest
+  file
 : c.dest
 ,%0A  
@@ -1243,16 +1243,23 @@
 : c.dest
+%0A    %7D)
 ,%0A    pl
@@ -1857,24 +1857,31 @@
 write(config
+.output
 )%0A    %7D)%0A   
@@ -1922,16 +1922,38 @@
 or(err)%0A
+      process.exit(1)%0A
     %7D)%0A%7D

fix build script to adapt latest rollup