const path = require('path');
const fs = require('fs');
const webpack = require('webpack');

const projectRoot = path.resolve(__dirname, '..');
const vendorDir = path.join(projectRoot, 'public', 'vendor');

fs.mkdirSync(vendorDir, { recursive: true });
fs.copyFileSync(
  path.join(projectRoot, 'node_modules', 'jszip', 'dist', 'jszip.min.js'),
  path.join(vendorDir, 'jszip.min.js')
);
fs.copyFileSync(
  path.join(projectRoot, 'node_modules', '@mescius', 'wijmo.styles', 'wijmo.css'),
  path.join(vendorDir, 'wijmo.css')
);

webpack(
  {
    mode: 'development',
    entry: path.join(projectRoot, 'src', 'wijmo-vendor.js'),
    output: {
      path: vendorDir,
      filename: 'wijmo-vendor.js'
    }
  },
  (error, stats) => {
    if (error) {
      console.error(error);
      process.exit(1);
    }

    if (stats?.hasErrors()) {
      console.error(stats.toString({ colors: true, errors: true, warnings: false }));
      process.exit(1);
    }

    console.log(stats?.toString({ colors: true, assets: true, chunks: false, modules: false }));
  }
);
