<commit_before>/* eslint-disable global-require */
"use strict";

const { rollup } = require("rollup");

const commonJs = require("rollup-plugin-commonjs");
const postcss = require("rollup-plugin-postcss");
const resolve = require("rollup-plugin-node-resolve");
const postcssUrl = require("postcss-url");

const TEMPLATE = require("./plugin/template-types");

let args = require("yargs")
  .strict()
  .option("all", {
    describe: "Build all templates",
    boolean: true
  })
  .option("open", { describe: "Open browser with stat files", boolean: true })
  .option("json", { describe: "Generate json", boolean: true });

for (const t of TEMPLATE) {
  args = args.option(t, {
    describe: `Build ${t} template`,
    boolean: true
  });
}

args = args.help();

const argv = args.argv;

const fileExt = argv.json ? ".json" : ".html";

const templatesToBuild = [];
if (argv.all) {
  templatesToBuild.push(...TEMPLATE);
} else {
  for (const t of TEMPLATE) {
    if (argv[t]) {
      templatesToBuild.push(t);
    }
  }
}

const open = argv.open;

const COMMON_PLUGINS = () => [
  resolve(),
  commonJs({
    ignoreGlobal: true,
    include: "node_modules/**"
  }),
  postcss({
    extract: true,
    plugins: [
      postcssUrl({
        url: "inline"
      })
    ]
  })
];

const onwarn = warning => {
  const { code } = warning;
  if (code === "CIRCULAR_DEPENDENCY" || code === "CIRCULAR" || code === "THIS_IS_UNDEFINED") {
    return;
  }
  // eslint-disable-next-line no-console
  console.warn("WARNING: ", warning.toString());
};

const runBuild = async template => {
  const inputOptions = {
    input: `./src/script-${template}.js`,
    plugins: [...COMMON_PLUGINS()],
    onwarn
  };
  const outputOptions = {
    format: "iife",
    dir: "./lib/"
  };

  const bundle = await rollup(inputOptions);

  await bundle.write(outputOptions);
};

const runBuildDev = async template => {
  const input = {};
  for (const t of TEMPLATE) {
    input[t] = `./src/script-${t}.js`;
  }
  const inputOptions = {
    input,
    plugins: [
      ...COMMON_PLUGINS(),
      require("./")({
        open,
        title: `test ${template}`,
        filename: `stats.${template}${fileExt}`,
        json: argv.json,
        template
      })
    ],
    onwarn
  };
  const outputOptions = {
    format: "es",
    dir: "./temp/"
  };

  const bundle = await rollup(inputOptions);

  await bundle.write(outputOptions);
};
const run = async () => {
  await Promise.all(TEMPLATE.map(t => runBuild(t)));
  //await Promise.all(templatesToBuild.map(t => runBuildDev(t)));
};

run();<commit_msg>Just in case uncoment it back<commit_after>/* eslint-disable global-require */
"use strict";

const { rollup } = require("rollup");

const commonJs = require("rollup-plugin-commonjs");
const postcss = require("rollup-plugin-postcss");
const resolve = require("rollup-plugin-node-resolve");
const postcssUrl = require("postcss-url");

const TEMPLATE = require("./plugin/template-types");

let args = require("yargs")
  .strict()
  .option("all", {
    describe: "Build all templates",
    boolean: true
  })
  .option("open", { describe: "Open browser with stat files", boolean: true })
  .option("json", { describe: "Generate json", boolean: true });

for (const t of TEMPLATE) {
  args = args.option(t, {
    describe: `Build ${t} template`,
    boolean: true
  });
}

args = args.help();

const argv = args.argv;

const fileExt = argv.json ? ".json" : ".html";

const templatesToBuild = [];
if (argv.all) {
  templatesToBuild.push(...TEMPLATE);
} else {
  for (const t of TEMPLATE) {
    if (argv[t]) {
      templatesToBuild.push(t);
    }
  }
}

const open = argv.open;

const COMMON_PLUGINS = () => [
  resolve(),
  commonJs({
    ignoreGlobal: true,
    include: "node_modules/**"
  }),
  postcss({
    extract: true,
    plugins: [
      postcssUrl({
        url: "inline"
      })
    ]
  })
];

const onwarn = warning => {
  const { code } = warning;
  if (code === "CIRCULAR_DEPENDENCY" || code === "CIRCULAR" || code === "THIS_IS_UNDEFINED") {
    return;
  }
  // eslint-disable-next-line no-console
  console.warn("WARNING: ", warning.toString());
};

const runBuild = async template => {
  const inputOptions = {
    input: `./src/script-${template}.js`,
    plugins: [...COMMON_PLUGINS()],
    onwarn
  };
  const outputOptions = {
    format: "iife",
    dir: "./lib/"
  };

  const bundle = await rollup(inputOptions);

  await bundle.write(outputOptions);
};

const runBuildDev = async template => {
  const input = {};
  for (const t of TEMPLATE) {
    input[t] = `./src/script-${t}.js`;
  }
  const inputOptions = {
    input,
    plugins: [
      ...COMMON_PLUGINS(),
      require("./")({
        open,
        title: `test ${template}`,
        filename: `stats.${template}${fileExt}`,
        json: argv.json,
        template
      })
    ],
    onwarn
  };
  const outputOptions = {
    format: "es",
    dir: "./temp/"
  };

  const bundle = await rollup(inputOptions);

  await bundle.write(outputOptions);
};
const run = async () => {
  await Promise.all(TEMPLATE.map(t => runBuild(t)));
  await Promise.all(templatesToBuild.map(t => runBuildDev(t)));
};

run();
{'fraction_non_alphanumeric': 0.17763157894736842, 'fraction_numerical': 0.0, 'mean_word_length': 3.0007739938080493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3411523', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1748, 'n_words': 396}