Webpack dev server live reload with proxy

我的未来我决定 提交于 2021-01-28 01:16:50

问题


I'm running a PHP app at localhost:8000.
I want to use webpack-dev-server to hot reload css, react and js components.
Had set a proxy to http://localhost:8000 but webpack-dev-server isn't reloading the browser.

Here's the webpack.config.js:

var path = require( 'path' );
var autoprefixer = require( 'autoprefixer' );

module.exports = {
    entry: [
        './src/app.js'
    ],
    output: {
        path: path.join( __dirname, 'dist' ),
        publicPath: 'http://localhost:8000',
        filename: 'app.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loaders: [ 'react-hot', 'babel-loader' ]
            },
            {
                test: /\.scss$/,
                loaders: [ 'style-loader', 'css-loader', 'sass-loader' ]
            }
        ]
    },
    postcss: [
        autoprefixer(
            {
                browsers: [
                    'last 2 versions'
                ]
            }
        )
    ],
    devServer: {
        port: 3000,
        proxy: {
            '**': {
                target: 'http://localhost:8000',
                secure: false,
                changeOrigin: true
            }
        }
    }
}

I'm accessing the webpack-dev-server at http://localhost:3000/webpack-dev-server/.

Changing my react component does cause webpack-dev-server to recompile, but the browser doesn't update.

Running webpack does compile the dist/app.js file, as calling it manually and reloading the browser works.


回答1:


So my publicPath was wrong.
Here's the fix:

output: {
    path: path.join( __dirname, 'dist' ),
    publicPath: 'http://localhost:3000/dist/',
    filename: 'app.js'
},

update: But it seems to be reloading the browser ¯_(ツ)_/¯



来源:https://stackoverflow.com/questions/40903038/webpack-dev-server-live-reload-with-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!