const webpack = require('webpack');
const merge = require('webpack-merge');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const chokidar = require('chokidar');
const { execSync } = require('child_process');

const dirs = require('./dirs');

const common = require('./webpack.common.js');

// 监听java文件改变自动编译java
chokidar
    .watch('../constants/*.java', { ignored: /(^|[/\\])\../ })
    .on('all', (event, path) => {
        try {
            execSync('npm run const');
        } catch (e) {
            console.error(e);
        }
    });

// 监听国际化文件并编译
chokidar
    .watch('../i18n/*.properties', { ignored: /(^|[/\\])\../ })
    .on('all', (event, path) => {
        try {
            execSync('npm run i18n');
        } catch (e) {
            console.error(e);
        }
    });

module.exports = merge(common, {
    devtool: 'eval-source-map',
    entry: {
        show: ['@babel/polyfill', './src/i18n.ts', './src/index.ts'],
    },
    output: {
        path: dirs.DEST,
        filename: 'show.dev.js',
    },
    devServer: {
        contentBase: path.join(__dirname, '..'),
        port: 10002,
        liveReload: true,
        proxy: {
            '/webroot/decision/V10': {
                target: 'http://api.fanruan.design/mock/135/',
                secure: false,
            },
            '/webroot/decision/resources': {
                target: 'http://localhost:8075',
                secure: false,
            }
        },
    },
    plugins: [
        new MiniCssExtractPlugin({
            path: dirs.DEST,
            filename: 'show.dev.css',
        }),
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, '../index.html'),
        }),
    ],
});