import path from "path";
import fs from "fs";
import alias from "rollup-plugin-alias";
import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import ts from "rollup-plugin-typescript2";
import uglify from "rollup-plugin-uglify";
import { minify as minify_es } from "uglify-es";

const r = (filepath, encoding = "utf8") =>
  JSON.parse(fs.readFileSync(filepath, { encoding }));
const { version } = r("package.json");
const dev = process.env.NODE_ENV === "dev";
const bundle = process.argv.includes("--bundle");

const commonPlugins = [
  alias({
    floodgate: path.resolve(".", "src/index"),
    classes: path.resolve(".", "src/classes"),
    functions: path.resolve(".", "src/functions"),
    helpers: path.resolve(".", "src/helpers"),
    types: path.resolve(".", "src/types")
  }),
  ts({}),
  babel({
    exclude: "node_modules/**"
  }),
  commonjs({
    include: ["node_modules/**"],
    exclude: ["node_modules/process-es6/**"],
    namedExports: {
      "node_modules/prop-types/index.js": ["PropTypes"],
      "node_modules/react/index.js": ["Component"],
      "node_modules/react-dom/index.js": ["render"]
    }
  }),
  resolve()
];

const config = {
  input: "./src/index.tsx",
  output: [
    {
      file: "dist/floodgate.cjs.js",
      format: "cjs",
      name: "Floodgate",
      banner: `/** floodgate v${version} : commonjs bundle **/`,
      exports: "named"
    },
    {
      file: "dist/floodgate.esm.js",
      format: "es",
      name: "Floodgate",
      banner: `/** floodgate v${version} : es bundle **/`
    },
    {
      file: "dist/floodgate.js",
      format: "iife",
      name: "Floodgate",
      banner: `/** floodgate v${version} : iife bundle **/`
    }
  ],
  plugins: [...commonPlugins, bundle && uglify({}, minify_es)],
  external: [
    "react",
    "react-dom",
    "prop-types",
    path.resolve("./src/types.d.ts")
  ]
};

const devConfig = {
  input: "./src/index.tsx",
  output: [
    {
      file: "dist/floodgate.dev.cjs.js",
      format: "cjs",
      name: "Floodgate",
      banner: `/** floodgate v${version} : commonjs bundle **/\n/** DEVELOPMENT FILE **/`,
      exports: "named",
      sourcemap: "./dist/"
    },
    {
      file: "dist/floodgate.dev.esm.js",
      format: "es",
      name: "Floodgate",
      banner: `/** floodgate v${version} : es bundle **/\n/** DEVELOPMENT FILE **/`,
      sourcemap: "./dist/"
    },
    {
      file: "dist/floodgate.dev.js",
      format: "iife",
      name: "Floodgate",
      banner: `/** floodgate v${version} : iife bundle **/\n/** DEVELOPMENT FILE **/`,
      sourcemap: "./dist/"
    }
  ],
  plugins: [...commonPlugins],
  external: [
    "react",
    "react-dom",
    "prop-types",
    path.resolve("./src/types.d.ts")
  ]
};

export default [config, devConfig];

@@ -265,100 +265,8 @@
 t2%22;
-%0Aimport uglify from %22rollup-plugin-uglify%22;%0Aimport %7B minify as minify_es %7D from %22uglify-es%22;
 %0A%0Aco
@@ -403,102 +403,8 @@
 n%22);
-%0Aconst dev = process.env.NODE_ENV === %22dev%22;%0Aconst bundle = process.argv.includes(%22--bundle%22);
 %0A%0Aco
@@ -1267,16 +1267,40 @@
  %22named%22
+,%0A      sourcemap: false
 %0A    %7D,%0A
@@ -1440,24 +1440,48 @@
  bundle **/%60
+,%0A      sourcemap: false
 %0A    %7D,%0A    
@@ -1625,907 +1625,32 @@
 **/%60
-%0A    %7D%0A  %5D,%0A  plugins: %5B...commonPlugins, bundle && uglify(%7B%7D, minify_es)%5D,%0A  external: %5B%0A    %22react%22,%0A    %22react-dom%22,%0A    %22prop-types%22,%0A    path.resolve(%22./src/types.d.ts%22)%0A  %5D%0A%7D;%0A%0Aconst devConfig = %7B%0A  input: %22./src/index.tsx%22,%0A  output: %5B%0A    %7B%0A      file: %22dist/floodgate.dev.cjs.js%22,%0A      format: %22cjs%22,%0A      name: %22Floodgate%22,%0A      banner: %60/** floodgate v$%7Bversion%7D : commonjs bundle **/%5Cn/** DEVELOPMENT FILE **/%60,%0A      exports: %22named%22,%0A      sourcemap: %22./dist/%22%0A    %7D,%0A    %7B%0A      file: %22dist/floodgate.dev.esm.js%22,%0A      format: %22es%22,%0A      name: %22Floodgate%22,%0A      banner: %60/** floodgate v$%7Bversion%7D : es bundle **/%5Cn/** DEVELOPMENT FILE **/%60,%0A      sourcemap: %22./dist/%22%0A    %7D,%0A    %7B%0A      file: %22dist/floodgate.dev.js%22,%0A      format: %22iife%22,%0A      name: %22Floodgate%22,%0A      banner: %60/** floodgate v$%7Bversion%7D : iife bundle **/%5Cn/** DEVELOPMENT FILE **/%60,%0A      sourcemap: %22./dist/%22
+,%0A      sourcemap: false
 %0A   
@@ -1795,16 +1795,16 @@
   %5D%0A%7D;%0A%0A
+
 export d
@@ -1821,18 +1821,7 @@
 nfig
-, devConfig
 %5D;%0A

update rollup to disable minification builds