Best way to integrate webpack builds with ASP.NET Core 3.0?

后端 未结 3 2084
执念已碎
执念已碎 2020-12-24 03:17

I\'m upgrading my ASP.NET Core app to V3, and using Visual Studio 2019 for development / debugging. The process has been smooth except for this:

public void          


        
3条回答
  •  死守一世寂寞
    2020-12-24 03:43

    In my opinion, Kram's answer should be marked as accepted as it really gives what's needed. I've spent some time setting up a .NET Core/React/Webpack project recently, and I could not get the spa.UseReactDevelopmentServer to work, but the spa.UseProxyToSpaDevelopmentServer works like a charm. Thanks Kram!

    Here are my few gotchas they might help someone:

    webpack.config.js (excerpt):

    const path = require('path');
    const webpack = require('webpack');
    
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'build.[hash].js',
    },
    
    devServer: {
        contentBase: path.resolve(__dirname, 'public'),
        publicPath: '/dist',
        open: false,
        hot: true
    },
    
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
    
    1. DevServer sets its root by publicPath property and completely ignores the output.path property in the root. So even though your output files (for prod) will go to dist folder, under webpack server they will be served under a root by default. If you want to preserve the same url, you have to set publicPath: '/dist'. This means that your default page will be under http://localhost:8083/dist. I could not figure out a way to place assets under subfolder while keeping index in the root (other than hardcode the path for assets).

    2. You need HotModuleReplacementPlugin in order to watch mode to work and also hot: true setting in the devServer configuration (or pass it as a parameter upon start).

    3. If you (like me) copy some dev server configuration with open: true (default is false), you will finish up with two browsers opened upon application start as another one is opened by the launchSettings "launchBrowser": true. It was a bit of a surprise at the first moment.

    Also, you will have to enable CORS for your project otherwise your backend calls will get blocked:

    Startup.cs (excerpt):

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options => 
        {
            options.AddPolicy(name: "developOrigins",
                builder =>
                {
                    builder.WithOrigins("http://localhost:8083");
                });
        });
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
            app.UseCors("developOrigins");
    }
    

    If anything else will come to my mind I will update the answer, hope this helps to someone.

    Webpack 5 update

    webpack-dev-server command doesn't work anymore. Use:

    "build:hotdev": webpack serve --config webpack.config.development.js
    

    You might also need to add target: 'web' to your webpack.config.js in Webpack 5 to enable hot module reload to work:

    module.exports = {
        target: 'web'
    }
    

    Alternatively you might need to create a browserlist file. Hope this issue gets fixed soon.

提交回复
热议问题