Skip to content

Commit

Permalink
feat: vendor asserts from Deno stdlib
Browse files Browse the repository at this point in the history
Allow modules to import
https://deno.land/std@0.177.0/testing/asserts.ts

This is a temporary workaround until we implement proper support for
ES modules, see #43.

Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos committed Feb 13, 2023
1 parent a2361da commit c5f5160
Show file tree
Hide file tree
Showing 3 changed files with 857 additions and 15 deletions.
36 changes: 21 additions & 15 deletions runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,28 @@ impl ModuleLoader for ZinniaModuleLoader {
));
}

if !specifier
.as_str()
.eq_ignore_ascii_case(main_js_module.as_str())
{
let mut msg =
"Zinnia does not support importing from other modules yet. "
.to_string();
msg.push_str(specifier.as_str());
if let Some(referrer) = &maybe_referrer {
msg.push_str(" imported from ");
msg.push_str(referrer.as_str());
let spec_str = specifier.as_str();

let code = {
if spec_str.eq_ignore_ascii_case(main_js_module.as_str()) {
read_file_to_string(specifier.to_file_path().unwrap()).await?
} else if spec_str == "https://deno.land/std@0.177.0/testing/asserts.ts"
{
// Temporary workaround until we implement ES Modules
// https://github.com/filecoin-station/zinnia/issues/43
include_str!("./vendored/asserts.bundle.js").to_string()
} else {
let mut msg =
"Zinnia does not support importing from other modules yet. "
.to_string();
msg.push_str(specifier.as_str());
if let Some(referrer) = &maybe_referrer {
msg.push_str(" imported from ");
msg.push_str(referrer.as_str());
}
return Err(anyhow!(msg));
}
return Err(anyhow!(msg));
}

let code = read_file_to_string(specifier.to_file_path().unwrap()).await?;
};

let module = ModuleSource {
code: Box::from(code.as_bytes()),
Expand Down
28 changes: 28 additions & 0 deletions runtime/vendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Run this Deno script to fetch and store Deno modules
// This is a temporary workaround until we support module imports
// See https://github.com/filecoin-station/zinnia/issues/43
//
// Run this script using the following command:
// deno --allow-all runtime/vendor.js

import { fromFileUrl } from "https://deno.land/std@0.177.0/path/mod.ts";

await vendor(
"https://deno.land/std@0.177.0/testing/asserts.ts",
"asserts.bundle.js",
);

async function vendor(url, outfile) {
const outpath = fromFileUrl(import.meta.resolve(`./vendored/${outfile}`));
const cmd = ["deno", "bundle", url, "--", outpath];
const child = Deno.run({ cmd });
const status = await child.status();
child.close();
if (!status.success) {
const reason = status.code
? `code ${status.code}`
: `signal ${status.signal}`;

throw new Error(`Process failed with ${reason}: ${cmd}`);
}
}
Loading

0 comments on commit c5f5160

Please sign in to comment.