const {resolve} = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
//设置node环境变量
process.env.NODE_ENV = 'development'

//压缩css
//optimize-css-assets-webpack-plugin

module.exports = {
    entry: './src/js/index.js',
    output:{
        filename:'js/bulit.js',
        path: resolve(__dirname,'build')
    },
    module:{
        rules:[
            {
                test: /\.css$/,
                use: [
                    //取代style-loader,提取js中css成单独文件
                    MiniCssExtractPlugin.loader,
                    // 创建style标签，将样式放入
                    //'style-loader',
                    // 将css文件整合到js中
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                             // 旧版本配置
                            /* ident: 'postcss', 
                            plugins: () => [
                                require('postcss-preset-env')()  //读取package.json里browserslist配置，默认加载根据node_env加载production
                            ] */
                            postcssOptions: {
                                plugins: [
                                  [
                                    'postcss-preset-env'
                                  ],
                                ],
                            }
                        }
                    }
                ]
            }
        ]
    },
    plugins:[
        new HtmlWebpackPlugin({
            template:'./src/index.html'
        }),
        new MiniCssExtractPlugin({
            filename:'css/built.css'   //重命名
        }),
        new OptimizeCssAssetsWebpackPlugin()
    ],
    mode:'development',
    devServer:{
        contentBase:resolve(__dirname,'build'),
        compress:true,
        port:3000,
        open:true
    }
}