const path = require('path')
const glob = require("glob")

const nodeExternals = require('webpack-node-externals')

const rootDir = process.cwd()

const rules = require('./rules')
const plugins = require('./plugins')

const RE = /\.\/src\/(components\/)?/g
const EXT_RE = /\.vue/g

const paths = glob.sync('./src/**/*.vue').map(p => [p.replace(RE, '').replace(EXT_RE, ''), p])
const pathObj = Object.fromEntries(paths)

module.exports = {
  mode: 'production',

  // target: 'node',

  entry: {
    index: './src',
    // ...pathObj,
  },

  output: {
    path: path.resolve(rootDir, './dist'),
    publicPath: '/dist/',
    filename: '[name].js',
    library: '@calipsa/ui',
    libraryTarget: 'umd',
    globalObject: 'typeof self !== \'undefined\' ? self : this',
  },

  resolve: {
    extensions: [
      '.ts',
      '.tsx',
      '.js',
      '.jsx',
      '.vue',
    ],
    modules: [
      path.resolve(rootDir, 'src'),
      // 'node_modules',
    ],
  },

  externals: [nodeExternals()],

  module: {
    rules,
  },

  optimization: {
    minimize: false,
  },

  plugins,
}
