import fg from 'fast-glob'
import { resolve } from 'path'
// import webpack from 'webpack'
import ProgressBarPlugin from 'progress-bar-webpack-plugin'
import VueLoaderPlugin from 'vue-loader/lib/plugin'
import type { Configuration } from 'webpack'

const DIR_SRC = resolve(__dirname, '..', 'packages')
const DIR_COMP = resolve(__dirname, DIR_SRC, 'components')

const components = fg.sync('*/index.{ts,js}', { cwd: DIR_COMP, onlyFiles: true }).map((i) => i.split('/')[0])

const entry = Object.fromEntries(
  [['index', './packages/components/index.ts']].concat(
    components.map((c) => [c, `./packages/components/${c}/index.js`])
  )
)

const config: Configuration = {
  mode: 'production',
  entry,
  devtool: 'source-map',
  output: {
    path: resolve(DIR_COMP, 'dist'),
    filename: '[name].js',
    chunkFilename: '[id].js',
    libraryTarget: 'commonjs2',
  },
  resolve: {
    modules: ['node_modules'],
    extensions: ['.js', '.ts', '.vue'],
  },
  optimization: {
    minimize: false,
  },
  performance: {
    hints: false,
  },
  externals: ['@datago/utils', 'vue', 'element-ui'],
  // stats: 'none', // 错误告警提示
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader',
      },
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
            },
          },
        ],
      },
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: ['vue-style-loader', 'css-loader'],
      },
      {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
      },
      {
        test: /\.(svg|otf|ttf|woff2?|eot|gif|png|jpe?g)(\?\S*)?$/,
        use: 'url-loader',
      },
    ],
  },
  plugins: [new ProgressBarPlugin(), new VueLoaderPlugin()],
}

export default config
