Webpackプラグインのclean-webpack-pluginファイルをクリア

2539 ワード

  • パラメータ詳細clean-webpack-plugin詳細
  • 用法webpack.config.js
  • const path = require('path');
    // html
    const htmlWebpackPlugin = require('html-webpack-plugin');
    // css  
    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    //     
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    module.exports = {
        //   
        mode: 'production',
        //     
        entry: {
            'app': './src/app.js'
        },
        //     
        output: {
            //       
            path: path.resolve(__dirname, 'dist'),
            //        
            filename: '[name].[chunkhash].js'
        },
        //   loader  webpack4.0  
        module: {
            rules: [
                //    css-loader 
                {
                    test: /\.css$/,
                    include: [
                        path.resolve(__dirname, 'src')
                    ],
                    // loader: ['style-loader', 'css-loader'],
                    use: ExtractTextPlugin.extract({
                        fallback: "style-loader",
                        use: "css-loader"
                    }),
                    //   node_modules  
                    exclude: [
                        path.resolve(__dirname, "node_modules")
                    ]
                },
                {
                    test: /\.scss$/,
                    include: [
                        path.resolve(__dirname, 'src')
                    ],
                    // loader: ['style-loader', 'css-loader', 'sass-loader'],
                    use: ExtractTextPlugin.extract({
                        fallback: "style-loader",
                        use: ["css-loader", "sass-loader"]
                    }),
                    exclude: [
                        path.resolve(__dirname, "node_modules")
                    ]
                }
            ]
        },
        // server   
        devServer: {
            port: 8848,
            open: false
        },
        //   
        plugins: [
            // 
            new htmlWebpackPlugin({
                //   
                title: '   webpack',
                //   
                template: 'src/index.html',
                //          
                minify: {
                    collapseWhitespace: true //false | true
                },
                //   hash
                hash: true
            }),
            // css  
            new ExtractTextPlugin('style.css'),
            //           
            new CleanWebpackPlugin(['dist']),
        ]
    }