Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": ["@babel/preset-react", "@babel/preset-env"],
"plugins": [
"@babel/proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import",
"@babel/proposal-class-properties"
]
}
82 changes: 82 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# NCrunch
*ncrunch*
*NCrunch*

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# dotnet
bin/
obj/
.vs/
*.user
*.csproj.DotSettings

project.lock.json
config.json

# tests
TestResult.xml

# ncrunch
*.ncrunchsolution
*.ncrunchsolution.user

# nuget
packages/

# ef helpers
int-update.sh
sys-update.sh
prod-update.sh

src/Service.Host/client/build/

src/Service.Host/.local-chromium*

config.env
local-update.sh

.vscode/

# StyleCop caches
*StyleCop.Cache

#webpack builds
wwwroot/dist/*
build/*
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function Sample({name}) {
var React = require('react');


export default function Sample({name}) {
return <div>
<h1>Hello {name} I am a react component that was rendered on the server :)</h1>
</div>;
}
}
7 changes: 7 additions & 0 deletions Content/components/expose-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';

import Sample from './Sample.jsx';

global.Components = { Sample };
20 changes: 8 additions & 12 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseHttpsRedirection();

// Initialise ReactJS.NET. Must be before static files.
app.UseReact(config =>
{
});
app.UseReact(
config =>
{
config.SetReuseJavaScriptEngines(true).SetLoadBabel(true).SetLoadReact(true)
.SetReactAppBuildPath("~/dist");
});

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});

ReactSiteConfiguration.Configuration = new ReactSiteConfiguration()
.AddScript("~/Components/Sample.jsx");
// todo - better routing?
app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}"); });
Copy link
Collaborator Author

@lewisrenfrew lewisrenfrew Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every component can be rendered on the server at its own route. This is the index route but in practise you'd use as a specific route for own componenet obviously

}
}
}
25 changes: 11 additions & 14 deletions Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
@{
Layout = null;

@model ServerSideReactComponents.Person

@using React.AspNet

@{
Layout = "_Layout";

}
@model Person

<html>
<head>
<title>Hello React</title>
</head>
<body>
@Html.React("Sample", new
{
name = Model.Name
})
</body>
</html>
@Html.React("Components.Sample", new { name = Model.Name })
Copy link
Collaborator Author

@lewisrenfrew lewisrenfrew Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you specify which component to render in the routes corresponding view. This is the index view corresponding to the index route above, and just renders a simple sample componenet with props dervied from the model (Person.cs)

@{
}
12 changes: 12 additions & 0 deletions Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@using React.AspNet
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
@RenderBody()
@Html.ReactGetScriptPaths()
@Html.ReactInitJavaScript()
</body>
</html>
10 changes: 0 additions & 10 deletions Views/_ViewImports.cshtml

This file was deleted.

16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is just for the dev server build.


<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>webpack dev server</title>
</head>

<body>
<div id="root"></div>
</body>

<script src="./build/app.js"></script>
</html>
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var React = require('react');
var ReactDOM = require('react-dom');
import Sample from './Content/components/Sample.jsx';
ReactDOM.render(
<Sample />,
document.getElementById('root')
);
Copy link
Collaborator Author

@lewisrenfrew lewisrenfrew Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is just for the dev build too. You'd swap 'Sample' out for the component you were developing. you'd pass in any test data as props here for rendering

Loading