Skip to content

Commit 7a9ff69

Browse files
authored
[browser] Clean-up typescript code imported from Blazor (dotnet#89435)
1 parent 8be0ec5 commit 7a9ff69

28 files changed

+1084
-1118
lines changed

src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ Copyright (c) .NET Foundation. All rights reserved.
372372
RuntimeOptions="$(_BlazorWebAssemblyRuntimeOptions)"
373373
Extensions="@(WasmBootConfigExtension)"
374374
TargetFrameworkVersion="$(TargetFrameworkVersion)"
375-
LibraryInitializerOnRuntimeConfigLoaded="@(WasmLibraryInitializerOnRuntimeConfigLoaded)"
376-
LibraryInitializerOnRuntimeReady="@(WasmLibraryInitializerOnRuntimeReady)" />
375+
ModuleAfterConfigLoaded="@(WasmModuleAfterConfigLoaded)"
376+
ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" />
377377

378378
<ItemGroup>
379379
<FileWrites Include="$(_WasmBuildBootJsonPath)" />
@@ -563,8 +563,8 @@ Copyright (c) .NET Foundation. All rights reserved.
563563
RuntimeOptions="$(_BlazorWebAssemblyRuntimeOptions)"
564564
Extensions="@(WasmBootConfigExtension)"
565565
TargetFrameworkVersion="$(TargetFrameworkVersion)"
566-
LibraryInitializerOnRuntimeConfigLoaded="@(WasmLibraryInitializerOnRuntimeConfigLoaded)"
567-
LibraryInitializerOnRuntimeReady="@(WasmLibraryInitializerOnRuntimeReady)" />
566+
ModuleAfterConfigLoaded="@(WasmModuleAfterConfigLoaded)"
567+
ModuleAfterRuntimeReady="@(WasmModuleAfterRuntimeReady)" />
568568

569569
<ItemGroup>
570570
<FileWrites Include="$(IntermediateOutputPath)blazor.publish.boot.json" />

src/mono/wasm/Wasm.Build.Tests/ProjectProviderBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,21 @@ public void AssertBootJson(AssertBundleOptionsBase options)
390390
Assert.True(File.Exists(bootJsonPath), $"Expected to find {bootJsonPath}");
391391

392392
BootJsonData bootJson = ParseBootData(bootJsonPath);
393-
var bootJsonEntries = bootJson.resources.runtime.Keys.Where(k => k.StartsWith("dotnet.", StringComparison.Ordinal)).ToArray();
393+
var bootJsonEntries = bootJson.resources.jsModuleNative.Keys
394+
.Union(bootJson.resources.jsModuleRuntime.Keys)
395+
.Union(bootJson.resources.jsModuleWorker?.Keys ?? Enumerable.Empty<string>())
396+
.Union(bootJson.resources.jsSymbols?.Keys ?? Enumerable.Empty<string>())
397+
.Union(bootJson.resources.wasmNative.Keys)
398+
.ToArray();
394399

395400
var expectedEntries = new SortedDictionary<string, Action<string>>();
396401
IReadOnlySet<string> expected = GetDotNetFilesExpectedSet(options);
397402

398403
var knownSet = GetAllKnownDotnetFilesToFingerprintMap(options);
399404
foreach (string expectedFilename in expected)
400405
{
401-
if (Path.GetExtension(expectedFilename) == ".map")
406+
// FIXME: Find a systematic solution for skipping dotnet.js from boot json check
407+
if (expectedFilename == "dotnet.js" || Path.GetExtension(expectedFilename) == ".map")
402408
continue;
403409

404410
bool expectFingerprint = knownSet[expectedFilename];

src/mono/wasm/runtime/dotnet.d.ts

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ interface DotnetHostBuilder {
9595
run(): Promise<number>;
9696
}
9797
type MonoConfig = {
98-
/**
99-
* The subfolder containing managed assemblies and pdbs. This is relative to dotnet.js script.
100-
*/
101-
assemblyRootFolder?: string;
102-
/**
103-
* A list of assets to load along with the runtime.
104-
*/
105-
assets?: AssetEntry[];
10698
/**
10799
* Additional search locations for assets.
108100
*/
@@ -133,6 +125,10 @@ type MonoConfig = {
133125
* debugLevel < 0 enables debugging and disables debug logging.
134126
*/
135127
debugLevel?: number;
128+
/**
129+
* Gets a value that determines whether to enable caching of the 'resources' inside a CacheStorage instance within the browser.
130+
*/
131+
cacheBootResources?: boolean;
136132
/**
137133
* Enables diagnostic log messages during startup
138134
*/
@@ -151,10 +147,6 @@ type MonoConfig = {
151147
* If true, the snapshot of runtime's memory will be stored in the browser and used for faster startup next time. Default is false.
152148
*/
153149
startupMemoryCache?: boolean;
154-
/**
155-
* hash of assets
156-
*/
157-
assetsHash?: string;
158150
/**
159151
* application environment
160152
*/
@@ -167,6 +159,10 @@ type MonoConfig = {
167159
* definition of assets to load along with the runtime.
168160
*/
169161
resources?: ResourceGroups;
162+
/**
163+
* appsettings files to load to VFS
164+
*/
165+
appsettings?: string[];
170166
/**
171167
* config extensions declared in MSBuild items @(WasmBootConfigExtension)
172168
*/
@@ -178,26 +174,31 @@ type ResourceExtensions = {
178174
[extensionName: string]: ResourceList;
179175
};
180176
interface ResourceGroups {
181-
readonly hash?: string;
182-
readonly assembly?: ResourceList;
183-
readonly lazyAssembly?: ResourceList;
184-
readonly pdb?: ResourceList;
185-
readonly runtime?: ResourceList;
186-
readonly satelliteResources?: {
177+
hash?: string;
178+
assembly?: ResourceList;
179+
lazyAssembly?: ResourceList;
180+
pdb?: ResourceList;
181+
jsModuleWorker?: ResourceList;
182+
jsModuleNative: ResourceList;
183+
jsModuleRuntime: ResourceList;
184+
jsSymbols?: ResourceList;
185+
wasmNative: ResourceList;
186+
icu?: ResourceList;
187+
satelliteResources?: {
187188
[cultureName: string]: ResourceList;
188189
};
189-
readonly libraryInitializers?: ResourceList;
190-
readonly libraryStartupModules?: {
191-
readonly onRuntimeConfigLoaded?: ResourceList;
192-
readonly onRuntimeReady?: ResourceList;
193-
};
194-
readonly extensions?: ResourceExtensions;
195-
readonly vfs?: {
190+
modulesAfterConfigLoaded?: ResourceList;
191+
modulesAfterRuntimeReady?: ResourceList;
192+
extensions?: ResourceExtensions;
193+
vfs?: {
196194
[virtualPath: string]: ResourceList;
197195
};
198196
}
197+
/**
198+
* A "key" is name of the file, a "value" is optional hash for integrity check.
199+
*/
199200
type ResourceList = {
200-
[name: string]: string;
201+
[name: string]: string | null | "";
201202
};
202203
/**
203204
* Overrides the built-in boot resource loading mechanism so that boot resources can be fetched
@@ -208,12 +209,12 @@ type ResourceList = {
208209
* @param integrity The integrity string representing the expected content in the response.
209210
* @returns A URI string or a Response promise to override the loading process, or null/undefined to allow the default loading behavior.
210211
*/
211-
type LoadBootResourceCallback = (type: WebAssemblyBootResourceType, name: string, defaultUri: string, integrity: string) => string | Promise<Response> | null | undefined;
212+
type LoadBootResourceCallback = (type: AssetBehaviors | "manifest", name: string, defaultUri: string, integrity: string) => string | Promise<Response> | null | undefined;
212213
interface ResourceRequest {
213214
name: string;
214-
behavior: AssetBehaviours;
215+
behavior: AssetBehaviors;
215216
resolvedUrl?: string;
216-
hash?: string;
217+
hash?: string | null | "";
217218
}
218219
interface LoadingResource {
219220
name: string;
@@ -248,7 +249,24 @@ interface AssetEntry extends ResourceRequest {
248249
*/
249250
pendingDownload?: LoadingResource;
250251
}
251-
type AssetBehaviours =
252+
type SingleAssetBehaviors =
253+
/**
254+
* The binary of the dotnet runtime.
255+
*/
256+
"dotnetwasm"
257+
/**
258+
* The javascript module for threads.
259+
*/
260+
| "js-module-threads"
261+
/**
262+
* The javascript module for threads.
263+
*/
264+
| "js-module-runtime"
265+
/**
266+
* The javascript module for threads.
267+
*/
268+
| "js-module-native";
269+
type AssetBehaviors = SingleAssetBehaviors |
252270
/**
253271
* Load asset as a managed resource assembly.
254272
*/
@@ -273,26 +291,6 @@ type AssetBehaviours =
273291
* Load asset into the virtual filesystem (for fopen, File.Open, etc).
274292
*/
275293
| "vfs"
276-
/**
277-
* The binary of the dotnet runtime.
278-
*/
279-
| "dotnetwasm"
280-
/**
281-
* The javascript module for threads.
282-
*/
283-
| "js-module-threads"
284-
/**
285-
* The javascript module for threads.
286-
*/
287-
| "js-module-runtime"
288-
/**
289-
* The javascript module for threads.
290-
*/
291-
| "js-module-dotnet"
292-
/**
293-
* The javascript module for threads.
294-
*/
295-
| "js-module-native"
296294
/**
297295
* The javascript module that came from nuget package .
298296
*/
@@ -330,10 +328,8 @@ type DotnetModuleConfig = {
330328
onConfigLoaded?: (config: MonoConfig) => void | Promise<void>;
331329
onDotnetReady?: () => void | Promise<void>;
332330
onDownloadResourceProgress?: (resourcesLoaded: number, totalResources: number) => void;
333-
getApplicationEnvironment?: (bootConfigResponse: Response) => string | null;
334331
imports?: any;
335332
exports?: string[];
336-
downloadResource?: (request: ResourceRequest) => LoadingResource | undefined;
337333
} & Partial<EmscriptenModule>;
338334
type APIType = {
339335
runMain: (mainAssemblyName: string, args: string[]) => Promise<number>;
@@ -400,7 +396,6 @@ type ModuleAPI = {
400396
exit: (code: number, reason?: any) => void;
401397
};
402398
type CreateDotnetRuntimeType = (moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)) => Promise<RuntimeAPI>;
403-
type WebAssemblyBootResourceType = "assembly" | "pdb" | "dotnetjs" | "dotnetwasm" | "globalization" | "manifest" | "configuration";
404399

405400
interface IDisposable {
406401
dispose(): void;

src/mono/wasm/runtime/lazyLoading.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import { INTERNAL, loaderHelpers, runtimeHelpers } from "./globals";
5-
import type { WebAssemblyResourceLoader } from "./loader/blazor/WebAssemblyResourceLoader";
4+
import { loaderHelpers, runtimeHelpers } from "./globals";
5+
import { AssetEntry } from "./types";
66

77
export async function loadLazyAssembly(assemblyNameToLoad: string): Promise<boolean> {
8-
const resourceLoader: WebAssemblyResourceLoader = INTERNAL.resourceLoader;
9-
const resources = resourceLoader.bootConfig.resources;
8+
const resources = loaderHelpers.config.resources!;
109
const lazyAssemblies = resources.lazyAssembly;
1110
if (!lazyAssemblies) {
1211
throw new Error("No assemblies have been marked as lazy-loadable. Use the 'BlazorWebAssemblyLazyLoad' item group in your project file to enable lazy loading an assembly.");
1312
}
1413

15-
const assemblyMarkedAsLazy = Object.prototype.hasOwnProperty.call(lazyAssemblies, assemblyNameToLoad);
16-
if (!assemblyMarkedAsLazy) {
14+
if (!lazyAssemblies[assemblyNameToLoad]) {
1715
throw new Error(`${assemblyNameToLoad} must be marked with 'BlazorWebAssemblyLazyLoad' item group in your project file to allow lazy-loading.`);
1816
}
1917

18+
const dllAsset: AssetEntry = {
19+
name: assemblyNameToLoad,
20+
hash: lazyAssemblies[assemblyNameToLoad],
21+
behavior: "assembly",
22+
};
23+
2024
if (loaderHelpers.loadedAssemblies.some(f => f.includes(assemblyNameToLoad))) {
2125
return false;
2226
}
2327

24-
const dllNameToLoad = assemblyNameToLoad;
25-
const pdbNameToLoad = changeExtension(assemblyNameToLoad, ".pdb");
26-
const shouldLoadPdb = loaderHelpers.hasDebuggingEnabled(resourceLoader.bootConfig) && resources.pdb && Object.prototype.hasOwnProperty.call(lazyAssemblies, pdbNameToLoad);
28+
const pdbNameToLoad = changeExtension(dllAsset.name, ".pdb");
29+
const shouldLoadPdb = loaderHelpers.hasDebuggingEnabled(loaderHelpers.config) && Object.prototype.hasOwnProperty.call(lazyAssemblies, pdbNameToLoad);
2730

28-
const dllBytesPromise = resourceLoader.loadResource(dllNameToLoad, loaderHelpers.locateFile(dllNameToLoad), lazyAssemblies[dllNameToLoad], "assembly").response.then(response => response.arrayBuffer());
31+
const dllBytesPromise = loaderHelpers.retrieve_asset_download(dllAsset);
2932

3033
let dll = null;
3134
let pdb = null;
3235
if (shouldLoadPdb) {
33-
const pdbBytesPromise = await resourceLoader.loadResource(pdbNameToLoad, loaderHelpers.locateFile(pdbNameToLoad), lazyAssemblies[pdbNameToLoad], "pdb").response.then(response => response.arrayBuffer());
36+
const pdbBytesPromise = lazyAssemblies[pdbNameToLoad]
37+
? loaderHelpers.retrieve_asset_download({
38+
name: pdbNameToLoad,
39+
hash: lazyAssemblies[pdbNameToLoad],
40+
behavior: "pdb"
41+
})
42+
: Promise.resolve(null);
43+
3444
const [dllBytes, pdbBytes] = await Promise.all([dllBytesPromise, pdbBytesPromise]);
3545

3646
dll = new Uint8Array(dllBytes);
37-
pdb = new Uint8Array(pdbBytes);
47+
pdb = pdbBytes ? new Uint8Array(pdbBytes) : null;
3848
} else {
3949
const dllBytes = await dllBytesPromise;
4050
dll = new Uint8Array(dllBytes);

0 commit comments

Comments
 (0)