1

我使用 Microsoft 的 aspnetcore-spa 生成器使用 React+Redux 生成了一个新的 ASP.NET Core SPA,并尝试将它与 ant-design 和babel-plugin-import一起使用,并且第一次页面将成功呈现,但是一次刷新,每次都会抛出以下错误,即使重启了dotnet应用程序。

处理请求时发生未处理的异常。

异常:调用节点模块失败并出现错误:预渲染因错误而失败:错误:在 webpackMissingModule (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ 中找不到模块“../../style/index.css”) ClientApp\dist\main-server.js:595:66) 在对象。(C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:595:164) 在webpack_require (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main- server.js:20:30) 在对象。(C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:387:13) 在webpack_require (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:20:30) 在 Object.defineProperty.value (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\ dist\main-server.js:168:14) 在webpack_require (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:20:30) 在 Object. (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:65:16) 在webpack_require (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\dist\main-server.js:20:30) 在 Object.defineProperty.value (C:\Users\viccrubs\Documents\Projects\QuestionSample1\ClientApp\ dist\main-server.js:40:18) 当前目录为:C:\Users\viccrubs\Documents\Projects\QuestionSample1 Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance+d__7.MoveNext()

这个 babel 插件在纯 react+webpack+typescript 项目中运行良好。在没有 babel-plugin-import 帮助的情况下手动从 antd 导入组件也会导致同样的错误。看起来是工作目录错误的问题,../../style/index.css在包含antd组件的目录下确实存在,但它应该已经被webpack处理了。而且第一次渲染效果很好也很奇怪。

示例: https ://github.com/viccrubs/QuestionSample1

或执行以下操作来重现它:

  1. 使用 yeoman 生成器生成带有 React+Redux 项目的 ASP.NET Core
  2. npm install --save antd babel-plugin-import
  3. ThunkAction从 /ClientApp/store 下的文件中删除所有导入
  4. 添加"plugins":[["import", { "libraryName": "antd", "style": "css" }]].babelrc
  5. 在 Home.tsx 中添加任意 antd 组件
  6. 添加"allowSyntheticDefaultImports": true,tsconfig.json
  7. dotnet 运行并打开 localhost:5000
  8. 应该是第一次渲染不会报错,但是一刷新,就会出现上面的错误。
4

0 回答 0