@@ -143,7 +143,8 @@ impl<'a> Context<'a> {
143
143
| OutputMode :: Node {
144
144
experimental_modules : true ,
145
145
}
146
- | OutputMode :: Web => {
146
+ | OutputMode :: Web
147
+ | OutputMode :: Deno => {
147
148
if contents. starts_with ( "function" ) {
148
149
let body = & contents[ 8 ..] ;
149
150
if export_name == definition_name {
@@ -269,6 +270,40 @@ impl<'a> Context<'a> {
269
270
reset_indentation ( & shim)
270
271
}
271
272
273
+ // generates somthing like
274
+ // ```js
275
+ // const imports = {
276
+ // __wbindgen_placeholder__: {
277
+ // __wbindgen_throw: function(..) { .. },
278
+ // ..
279
+ // }
280
+ // }
281
+ // ```
282
+ fn generate_deno_imports ( & self ) -> String {
283
+ let mut imports = "const imports = {\n " . to_string ( ) ;
284
+ imports. push_str ( & format ! ( " {}: {{\n " , crate :: PLACEHOLDER_MODULE ) ) ;
285
+
286
+ for ( id, js) in crate :: sorted_iter ( & self . wasm_import_definitions ) {
287
+ let import = self . module . imports . get ( * id) ;
288
+ imports. push_str ( & format ! ( "{}: {},\n " , & import. name, js. trim( ) ) ) ;
289
+ }
290
+
291
+ imports. push_str ( "\t }\n };\n \n " ) ;
292
+
293
+ imports
294
+ }
295
+
296
+ fn generate_deno_wasm_loading ( & self , module_name : & str ) -> String {
297
+ format ! (
298
+ "const file = new URL(import.meta.url).pathname;
299
+ const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\ \\ ' : '/') + 1) + '{}_bg.wasm';
300
+ const wasmModule = new WebAssembly.Module(Deno.readFileSync(wasmFile));
301
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
302
+ const wasm = wasmInstance.exports;" ,
303
+ module_name
304
+ )
305
+ }
306
+
272
307
/// Performs the task of actually generating the final JS module, be it
273
308
/// `--target no-modules`, `--target web`, or for bundlers. This is the very
274
309
/// last step performed in `finalize`.
@@ -331,6 +366,15 @@ impl<'a> Context<'a> {
331
366
}
332
367
}
333
368
369
+ OutputMode :: Deno => {
370
+ footer. push_str ( & self . generate_deno_imports ( ) ) ;
371
+ footer. push_str ( & self . generate_deno_wasm_loading ( module_name) ) ;
372
+
373
+ if needs_manual_start {
374
+ footer. push_str ( "wasm.__wbindgen_start();\n " ) ;
375
+ }
376
+ }
377
+
334
378
// With Bundlers and modern ES6 support in Node we can simply import
335
379
// the wasm file as if it were an ES module and let the
336
380
// bundler/runtime take care of it.
@@ -443,7 +487,8 @@ impl<'a> Context<'a> {
443
487
| OutputMode :: Node {
444
488
experimental_modules : true ,
445
489
}
446
- | OutputMode :: Web => {
490
+ | OutputMode :: Web
491
+ | OutputMode :: Deno => {
447
492
for ( module, items) in crate :: sorted_iter ( & self . js_imports ) {
448
493
imports. push_str ( "import { " ) ;
449
494
for ( i, ( item, rename) ) in items. iter ( ) . enumerate ( ) {
@@ -1247,7 +1292,7 @@ impl<'a> Context<'a> {
1247
1292
fields : Vec :: new ( ) ,
1248
1293
} ) ?;
1249
1294
self . global ( & format ! ( "let cached{} = new {}{};" , s, name, args) ) ;
1250
- } else if !self . config . mode . always_run_in_browser ( ) {
1295
+ } else if !self . config . mode . always_run_in_browser ( ) && ! self . config . mode . deno ( ) {
1251
1296
self . global ( & format ! (
1252
1297
"
1253
1298
const l{0} = typeof {0} === 'undefined' ? \
0 commit comments