Skip to content

Commit 8246f55

Browse files
Partially-complete support for "import" with JSObjectReference. (#25198)
1 parent abdeb0d commit 8246f55

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ export module DotNet {
6161
0: new JSObject(window, 0),
6262
};
6363

64+
cachedJSObjectsById[0]._cachedFunctions.set('import', (url: any) => {
65+
// In most cases developers will want to resolve dynamic imports relative to the base HREF.
66+
// However since we're the one calling the import keyword, they would be resolved relative to
67+
// this framework bundle URL. Fix this by providing an absolute URL.
68+
if (typeof url === 'string' && url.startsWith('./')) {
69+
url = document.baseURI + url.substr(2);
70+
}
71+
72+
return import(/* webpackIgnore: true */ url);
73+
});
74+
6475
let nextAsyncCallId = 1; // Start at 1 because zero signals "no response needed"
6576
let nextJsObjectId = 1; // Start at 1 because zero is reserved for "window"
6677

@@ -111,13 +122,13 @@ export module DotNet {
111122

112123
/**
113124
* Creates a JavaScript object reference that can be passed to .NET via interop calls.
114-
*
125+
*
115126
* @param jsObject The JavaScript Object used to create the JavaScript object reference.
116127
* @returns The JavaScript object reference (this will be the same instance as the given object).
117128
* @throws Error if the given value is not an Object.
118129
*/
119130
export function createJSObjectReference(jsObject: any): any {
120-
if (jsObject instanceof Object) {
131+
if (jsObject && typeof jsObject === 'object') {
121132
if (!jsObject.hasOwnProperty(JSObject.ID_KEY) || cachedJSObjectsById[nextJsObjectId] !== jsObject) {
122133
// The return value is not cached as a JSObjectReference, or is copied from an object that was, so we cache it as a new one
123134
cachedJSObjectsById[nextJsObjectId] = new JSObject(jsObject, nextJsObjectId);
@@ -132,7 +143,7 @@ export module DotNet {
132143

133144
/**
134145
* Disposes the JavaScript object reference associated with the given object.
135-
*
146+
*
136147
* @param jsObject The JavaScript Object associated with the JavaScript object reference.
137148
*/
138149
export function disposeJSObjectReference(jsObject: any): void {
@@ -260,7 +271,7 @@ export module DotNet {
260271

261272
/**
262273
* Disposes the JavaScript object reference with the specified object ID.
263-
*
274+
*
264275
* @param id The ID of the JavaScript object reference.
265276
*/
266277
disposeJSObjectReferenceById,

src/JSInterop/Microsoft.JSInterop.JS/src/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"lib": ["es2015", "dom", "es2015.promise"],
99
"strict": true,
1010
"declaration": true,
11-
"outDir": "dist"
11+
"outDir": "dist",
12+
"module": "ESNext",
1213
},
1314
"include": [
1415
"src/**/*.ts"

0 commit comments

Comments
 (0)