How to proxy static files with wepack devserver?

Viewed 220

My publicPath is /static/dist. How i can proxy static/dist to / when my dev server running?

I know i can:

let publicPath;
if (process.env.NODE_BUILD_DEPLOYMENT_PATH) {
    // Change process.env.NODE_BUILD_DEPLOYMENT_PATH to match the path to your files
    // in production (could be S3, CloudFront, etc.)
    publicPath = process.env.NODE_BUILD_DEPLOYMENT_PATH;
} else {
    publicPath = 'http://localhost:8099/';
}

But i need proxy. It's for pre-render. After render i need static/dist prefix for all my static files. But when my dev server running i want proxy static/dist to /.

For example: static/dist/js -> /js.

1 Answers

Solution is:

        server: {
            port: options.devServer.port,
            proxy: {
                '/static/dist/*': {
                    target: 'http://localhost:8099',
                    pathRewrite: {'^/static/dist/': ''}
                }
            }
        },
Related