66
loading...
This website collects cookies to deliver better user experience
dotnet new react -n SampleApp
SampleApp
project is created by default it will contain ClientApp
directory, which is where the SPA(in this case React App) resides. as the default SPA template doesn't fit the required scenario delete everything inside ClientApp
directory. ClientApp
directory first run the following commandnpm init -y
package.json
file which will contain the workspace information. for this example I want to create four workspaces namedClientApp
will now look like the following{
"name": "@clientapp/root",
"version": "1.0.0",
"private": true,
"scripts": {
"start:table": "npm run start -w @clientapp/table",
"start:card": "npm run start -w @clientapp/card",
"build:table": "npm run build -w @clientapp/table",
"build:card": "npm run build -w @clientapp/card"
},
"workspaces": [
"workspaces/*/**"
]
}
ClientApp\workspaces\apps
directory run the following commands consecutivelynpx create-react-app table --template typescript
name
field inside ClientApp\workspaces\apps\table\package.json
to"name": "@clientapp/table"
npx create-react-app card --template typescript
name
field inside ClientApp\workspaces\apps\card\package.json
to"name": "@clientapp/card"
@clientapp/table
& @clientapp/card
we will not be able to use the typescript libraries from other workspaces. in order to support typescript I will use craco
instead of react-scripts
. the changes in this section must be applied in both @clientapp/table
& @clientapp/card
.craco
as dev dependencynpm install craco --save-dev
craco.config.js
const path = require("path");
const { getLoader, loaderByName } = require("craco");
const packages = [];
/**
* add the typescript workspaces this project is dependent up on
*/
packages.push(path.join(__dirname, "../../libs/core"));
module.exports = {
webpack: {
configure: (webpackConfig, { env, paths }) => {
/**
* Overriding the output directory of build to fit with default configuration of .NET wrapper
*/
paths.appBuild = webpackConfig.output.path = path.resolve('../../../build');
const { isFound, match } = getLoader(webpackConfig, loaderByName("babel-loader"));
if (isFound) {
const include = Array.isArray(match.loader.include)
? match.loader.include
: [match.loader.include];
match.loader.include = include.concat(packages);
}
return webpackConfig;
},
},
};
scrpts
section inside package.json
of both @clientapp/table
& @clientapp/card
as shown below:{
...
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject"
},
...
}
ClientApp\workspaces\libs
open terminal and run the following commandnpx create-react-app core --template typescript
name
field inside ClientApp\workspaces\apps\card\package.json
to"name": "@clientapp/core"
craco
.From all application delete node_modules
directory
@clientapp/core
workspace into @clientapp/table
& @clientapp/card
run the following commands from ClientApp
directorynpm install @clientapp/core -w @clientapp/table
npm install @clientapp/core -w @clientapp/card
npm install
from ClientApp
directory.npm run start:table
npm run start:card
Configure
method inside Startup.cs
by replacingspa.UseReactDevelopmentServer(npmScript: "start");
spa.UseReactDevelopmentServer(npmScript: "run start:table");
spa.UseReactDevelopmentServer(npmScript: "run start:card");
SampleApp.csproj
by replacing<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<Error Condition="'$(SpaBuildScript)' == ''" Text="Spa build script is not specified." />
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="$(SpaBuildScript)" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
CardAppProfile.pubxml
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DeleteExistingFiles>False</DeleteExistingFiles>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\net5.0\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
<SpaBuildScript>npm run build:card</SpaBuildScript>
</PropertyGroup>
</Project>
TableAppProfile.pubxml
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DeleteExistingFiles>False</DeleteExistingFiles>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\net5.0\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
<SpaBuildScript>npm run build:table</SpaBuildScript>
</PropertyGroup>
</Project>
dotnet pubilsh /p:PublishProfile="Properties\PublishProfiles\TableAppProfile.pubxml"
dotnet pubilsh /p:PublishProfile="Properties\PublishProfiles\CardAppProfile.pubxml"