Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

webpack只有更改js文件才能自动刷新,更改css文件不能自动刷新?

const path = require('path');
var webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin'); //生成html
const ExtractTextPlugin = require("extract-text-webpack-plugin");  //css单独打包
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const PATHS = {
    app: path.join(__dirname, 'src/app'),
    build: path.join(__dirname, 'build'),
};

module.exports = {
    devtool: "cheap-eval-source-map",
    entry: {
        app: PATHS.app,
    },
    output: {
        path: PATHS.build,
        filename: '[name].[hash:8].js',
    },
    module: {
        rules: [
            {
                test: /.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'postcss-loader'] //使用postcss-loader 要在plugin中配置LoaderOptionsPlugin
                })
            }
        ]
    },

    devServer: {
        contentBase: './',  //本地服务器所加载的页面所在的目录
        host: 'localhost',
        port: 8080,
        historyApiFallback: true,  //不跳转
        hot: true,
        inline: true,//
        proxy: {
            '/api': {
                target: '',
                secure: false,
                changeOrigin: true,
                host: ''
            }
        }
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: '测试',//生成HTML文件的title,设置template没用
            filename: './index.html',//打包后html文件名
            template: './src/template/index.html',//
            inject: 'head',
            favicon: '',// html 文件生成一个 favicon
            hash: true,//给生成的js文件hash 值
            minify: {    //压缩HTML文件
                removeComments: true,    //移除HTML中的注释
                collapseWhitespace: true,    //删除空白符与换行符
                conservativeCollapse: true,
                minifyJS: true //js也在一行
            }
        }),
        new ExtractTextPlugin({filename: '[name].[hash:8].css'}),//样式压缩
        new OpenBrowserPlugin({    //自动打开浏览器
            url: 'http://localhost:8080'
        }),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: function () {
                    return [
                        require('autoprefixer')
                    ];
                }
            }
        }),
    ],
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

试一试

plugins.push(new webpack.HotModuleReplacementPlugin())

增加 webpack 热替换


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...