设置Asp.Net Core应用程序以使用Webpack和HMR启动Vue SPA

时间:2019-11-04 20:48:05

标签: vue.js asp.net-core

我正在尝试使用ASP.NET Core 3.0和Vue建立新的应用程序。我一直在努力正确地设置它,以便后端服务器和前端服务器正确地集成在一起,并具有正确的路由和正常工作的HMR。我现在确实可以使用它,但是我无法确定这是否是正确的方法,而且有些作品我认为效果不佳。

我当前的设置:

Webpack已配置为将文件输出到wwwroot中。我也有HMR和 运行WriteFilePlugin时启用webpack-dev-server

 mode: 'development',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, '../wwwroot'),
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    // write files to fs instead of memory
    // so that the server can access and serve index.html
    new WriteFilePlugin(),
  ],
  devServer: {
    hot: true,
    watchOptions: {
      poll: true,
    },
    port: 9001,
  },

Startup.cs中:

 public void ConfigureServices(IServiceCollection services)
        {
          .....
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "wwwroot";
            });
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");

                if (env.IsDevelopment())
                {
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        // run the dev server 
                        endpoints.MapToVueCliProxy("{*path}", new SpaOptions { SourcePath = "ClientApp" },
                            npmScript: "serve", regex: "Compiled successfully");
                    }
                }
                // serve index.html file when no api endpoints are matched
                endpoints.MapFallbackToFile("index.html");
       }

我的设置问题:

  • 通常,HMR在内存中构建文件,但是我在这里进行了设置,以将新文件写入文件系统,以便后端服务器可以访问和服务index.html。这行得通,但是现在我已经被大量文件写入我的文件系统了。是否可以将内存HMR与ASP.NET服务器结合使用?
  • 代理并不总是可靠的;我经常在控制台中看到无法连接服务器的错误。另外,index.html有时会被缓存,因此我必须进行硬重装。可以以某种方式使它更可靠吗?

0 个答案:

没有答案