iJa*_*ava 4 typescript reactjs asp.net-core webpack-4
无法弄清楚,为什么当我仅npm run start从命令行运行时 ,项目启动,并且一切似乎都运行良好。。但是,如果我尝试从Visual Studio在IIS上启动它,则会启动浏览器窗口(该窗口会超时“在50秒的超时时间内,create-react-app服务器未开始侦听请求”。几秒钟后,它将在新端口上启动另一个浏览器选项卡,该选项卡将根据需要加载我的项目。
我非常相信我的StartUp.cs有问题,只是想不出原因,原因和原因。
如果需要,我可以提供任何其他需要的信息。
我的项目结构:
web
|
bin-
ClientApp
|
dist-
node_modules-
public- index.html
src - index.tsx
package.json
tsconfig.json
tslint.json
webpack.config.js
Controllers-
obj
Pages
Properties
appsettings.Development.json
appsettings.json
Program.cs
Startup.cs
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "[name].bundle.js",
path: __dirname + '/dist',
},
devtool: 'source-map',
resolve: {
extensions: ['.js', '.json', '.ts', '.tsx'],
},
module: {
rules: [{
test: /\.(ts|tsx)$/,
loader: 'awesome-typescript-loader',
},
{
test: /\.html$/,
use: [{
loader: "html-loader",
options: {
minimize: true
}
}]
},
{
test: /\.css$/,
use: [{
loader: 'style-loader',
}, {
loader: 'css-loader',
}, ],
},
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./public/index.html",
filename: "./index.html"
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
};
Run Code Online (Sandbox Code Playgroud)
package.json
{
"name": "ClientApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --mode development --open",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.0.2",
"css-loader": "^1.0.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"install": "^0.12.1",
"mini-css-extract-plugin": "^0.4.2",
"react": "^16.5.1",
"react-dom": "^16.5.1",
"style-loader": "^0.23.0",
"tslint": "^5.11.0",
"typescript": "^3.0.3",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.8"
},
"dependencies": {
"@types/react": "^16.4.14",
"@types/react-dom": "^16.0.7"
}
}
Run Code Online (Sandbox Code Playgroud)
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Web
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Run Code Online (Sandbox Code Playgroud)
启动文件
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"noImplicitAny": true,
"sourceMap": true,
"target": "es6",
"jsx": "react",
"moduleResolution":"node",
"allowSyntheticDefaultImports": true,
"module": "ESNext",
},
"exclude": [
"node_modules"
],
}
Run Code Online (Sandbox Code Playgroud)
我使用上面的配置创建一个新的react模板,发现当命令npm run build失败或npm start失败时,或者当AspNetCore无法找到Webpack Dev Server的正确URL时,它将抱怨与您描述的相同的错误:
超时“创建反应应用程序服务器未在50秒的超时时间内开始侦听请求”
如错误所描述,似乎存在问题react-scripts。但是,当我查看package.json文件时,发现根本没有create-react-app配置任何依赖项。
所以我添加了一个react-scripts依赖npm i react-scripts --save-dev
"react-scripts": "^1.1.5",
Run Code Online (Sandbox Code Playgroud)
并将您的npm start替换为:
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build"
},
Run Code Online (Sandbox Code Playgroud)
现在在Visual Studio中启动时可以正常工作。
react-scripts既然您说过,效果不错npm start。我想原因是ASP.NET Core无法找到Webpack开发服务器上提供的正确URL。
所以我用您创建了另一个新的react项目package.json:
"scripts": {
"start": "webpack-dev-server --port 3000 --mode development --open",
"build": "webpack --mode production"
},
Run Code Online (Sandbox Code Playgroud)
由于默认的开发服务器在端口3000上进行侦听,而contentBase是/public,因此我在您的服务器中添加了一个配置webpack.config.js:
devServer: {
contentBase: __dirname + "/public/",
inline: true,
port: 3000,
},
Run Code Online (Sandbox Code Playgroud)
我注意到您对babel有依赖关系,因此我添加了一个.babelrc配置和一个依赖关系@babel/plugin-proposal-class-properties:
{
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
],
"presets":["@babel/env","@babel/react"]
}
Run Code Online (Sandbox Code Playgroud)
另外,我不知道您是否有的文件jsx ,所以我只在webpack.config.js上添加一条规则:
{
test:/\.jsx?/,
loaders:['babel-loader']
},
Run Code Online (Sandbox Code Playgroud)
因为我已经有了index.html(由模板生成),所以删除了HtmlWebPackPlugin配置:
plugins: [
// new HtmlWebPackPlugin({
// // template: "public/index.html",
// // // filename: "index.html"
// }),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
Run Code Online (Sandbox Code Playgroud)
当我测试默认的react模板(已将默认模板package.json替换为您的模板)时,它现在可以正常工作。
| 归档时间: |
|
| 查看次数: |
4857 次 |
| 最近记录: |