Skip to content

Commit

Permalink
Merge pull request #73 from Daddoon/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Daddoon authored Nov 13, 2021
2 parents 1463ec4 + e26febd commit e24c954
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ Here are the step to install it:
var module = await js.InvokeAsync<IJSObjectReference>(
"_import_", "/js/modules/exampleJsInterop.js");
```

**However** due to some context limitation, the **\_import\_** may fail if the current app root path you are targeting is not a rooted URL.
This mean:

- This should work by default: https://127.0.0.1/[blazorpage]
- This should not work by default: https://127.0.0.1/myotherapp/managingsomeurl/[blazorpage]
In order to workaround this limitation, you may have to compute the relative path by yourself in C# before calling \_import\_, and give it in addition, as a second parameter, the webRootPath.
For our second URL example, https://127.0.0.1/myotherapp/managingsomeurl/[blazorpage], this would be instead:
```razor
var module = await js.InvokeAsync<IJSObjectReference>(
"_import_", "/js/modules/exampleJsInterop.js", "/myotherapp/managingsomeurl/");
```

You should find some hint with the **NavigationManager.BaseURI** service / property.

See the Microsoft documentation for the Blazor API usage.

Expand Down
2 changes: 1 addition & 1 deletion src/Blazor.Polyfill.Build/Blazor.Polyfill.Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="set nugetVersion=5.0.100.6&#xD;&#xA;echo NuGet artifact version is: %25nugetVersion%25&#xD;&#xA;&#xD;&#xA;nuget pack .\Blazor.Polyfill.Build.nuspec -Version &quot;%25nugetVersion%25&quot; -OutputDirectory .\artifacts&#xD;&#xA;" />
<Exec Command="set nugetVersion=6.0.100.1&#xD;&#xA;echo NuGet artifact version is: %25nugetVersion%25&#xD;&#xA;&#xD;&#xA;nuget pack .\Blazor.Polyfill.Build.nuspec -Version &quot;%25nugetVersion%25&quot; -OutputDirectory .\artifacts&#xD;&#xA;" />
</Target>

</Project>
2 changes: 1 addition & 1 deletion src/Blazor.Polyfill.Server/Blazor.Polyfill.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.0.100.0</Version>
<Version>6.0.100.1</Version>
<Authors>Guillaume ZAHRA</Authors>
<Company>Daddoon</Company>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
48 changes: 47 additions & 1 deletion src/Blazor.Polyfill.Server/dist/fake.polyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
window._import_ = function (fileName) {
window._import_ = function (fileName, webRootPath) {

if (webRootPath === undefined || webRootPath === null) {
webRootPath = "";
}

function pathJoin(path1, path2) {

if (path1 === null || path1 === undefined) {
path1 = "";
}

if (path2 === null || path2 === undefined) {
path2 = "";
}

var needTrail = true;
var path1hasTrail = false;
var path2hasTrail = false;

if (path1.length - 1 >= 0 && (path1[path1.length - 1] == '/' || path1[path1.length - 1] == '\\')) {
needTrail = false;
path1hasTrail = true;
}

if (path2.length > 0 && (path2[0] == '/' || path2[0] == '\\')) {
needTrail = false;
path2hasTrail = true;
}

if (needTrail) {
return path1 + "/" + path2;
}
else {
if (path1hasTrail && path2hasTrail) {
return path1 + path2.substring(1);
}
else if (path1hasTrail && !path2hasTrail) {
return path1 + path2;
}
else if (!path1hasTrail && path2hasTrail) {
return path1 + path2;
}
}
}

fileName = pathJoin(webRootPath, fileName);

if (fileName.length > 0 && fileName[0] !== '/') {
console.log("_import_: For compatibility reasons, assuming current path '" + fileName + "' as absolute.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.0.0" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="3.6.0">
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.4.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
43 changes: 41 additions & 2 deletions src/Daddoon.Blazor.Polyfill.JS/src/Boot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Daddoon.Blazor.Polyfill.JS/src/Boot.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 52 additions & 2 deletions src/Daddoon.Blazor.Polyfill.JS/src/Boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,46 @@ declare var File;
return stack.join("/");
}

function pathJoin(path1, path2) {

if (path1 === null || path1 === undefined) {
path1 = "";
}

if (path2 === null || path2 === undefined) {
path2 = "";
}

var needTrail = true;
var path1hasTrail = false;
var path2hasTrail = false;

if (path1.length - 1 >= 0 && (path1[path1.length - 1] == '/' || path1[path1.length - 1] == '\\')) {
needTrail = false;
path1hasTrail = true;
}

if (path2.length > 0 && (path2[0] == '/' || path2[0] == '\\')) {
needTrail = false;
path2hasTrail = true;
}

if (needTrail) {
return path1 + "/" + path2;
}
else {
if (path1hasTrail && path2hasTrail) {
return path1 + path2.substring(1);
}
else if (path1hasTrail && !path2hasTrail) {
return path1 + path2;
}
else if (!path1hasTrail && path2hasTrail) {
return path1 + path2;
}
}
}

blazorPolyfill();

//Must be set by prior by the server but sanity checking here
Expand Down Expand Up @@ -166,18 +206,28 @@ declare var File;
}
}

window._import_ = function (fileName) {
window._import_ = function (fileName, webRootPath) {
//Assuming that if this polyfill is loaded the user want a full ES5 compliant
//behavior, even with imports.

//Actually webRootPath is only use for the real import version that may not retrieve the right base path when
//trying to load the real isolated file from absolute

//The "no polyfill" version does integrate an _import_ that call the native import
return window._es5Import(fileName);
};

//After this script is finished, we must "inject" the ES5 fallback user lib, if configured from the server
if (window._es5ShouldLoadModuleAfterBoot) {
const script = document.createElement("script");
script.src = window._es5modulePath;

//We should compute the script path in adequation of the current app root executing path
//Either done automatically or overidden

var relativeBaseURI = new URL(document.baseURI).pathname;
var absoluteES5ModulePath = pathJoin(relativeBaseURI, window._es5modulePath);

script.src = absoluteES5ModulePath;
document.body.appendChild(script);
}
})();

0 comments on commit e24c954

Please sign in to comment.