Skip to content

Commit a16d893

Browse files
authored
feat: support external specifiers (#54)
1 parent c489c01 commit a16d893

File tree

12 files changed

+160
-60
lines changed

12 files changed

+160
-60
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ jobs:
3333
echo "$HOME/.deno/bin" >> $GITHUB_PATH
3434
3535
- name: Clippy
36-
run: cargo clippy --all-targets
36+
run: cargo clippy --all-targets --locked
3737

3838
- name: Build
3939
run: make
4040

4141
- name: Test
4242
run: |
43-
cargo test --all-targets
43+
cargo test --all-targets --locked
4444
make test

Cargo.lock

Lines changed: 25 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ path = "src/examples/load.rs"
3535
[dependencies]
3636
anyhow = "1"
3737
base64 = "0.13.0"
38-
deno_ast = { version = "0.10.0", features = ["transpiling"] }
39-
deno_graph = "0.21.0"
38+
deno_ast = { version = "0.11.0", features = ["transpiling"] }
39+
deno_graph = "0.23.0"
4040
futures = "0.3.19"
4141
serde = "1"
4242
serde_json = "1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ make: *** [build] Error 1
8585

8686
mean that `wasm-bindgen-cli` doesn't match version specified in `Cargo.toml`.
8787

88-
To fix it, run `cargo install wasm-bindgen-cli@VERSION` where `VERSION` matches
88+
To fix it, run `cargo install wasm-bindgen-cli --version VERSION` where `VERSION` matches
8989
what's specified in `Cargo.toml`.

lib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ crate-type = ["cdylib"]
1414
[dependencies]
1515
js-sys = { version = "0.3.55" }
1616
web-sys = { version = "0.3.56", features = ["ReadableStreamByobReader"] }
17-
deno_graph = { version = "0.21.0"}
17+
deno_graph = { version = "0.23.0" }
1818
eszip = { path = "../", version = "0.15.1" }
19-
wasm-bindgen = { version = "0.2.78", features = ["serde-serialize"] }
19+
wasm-bindgen = { version = "0.2.79", features = ["serde-serialize"] }
2020
wasm-bindgen-futures = { version = "0.4.28" }
2121
tokio = { version = "1.16", features = ["io-std", "io-util"] }
2222
console_error_panic_hook = "0.1.7"

lib/eszip_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ Deno.test("roundtrip build + parse", async () => {
88
const eszip = await build([
99
"https://example.com/mod.ts",
1010
"https://example.com/a.ts",
11+
"external:main.js",
1112
], async (specifier: string) => {
13+
if (specifier === "external:main.js") {
14+
return {
15+
kind: "external",
16+
specifier,
17+
};
18+
}
19+
1220
if (specifier === "https://example.com/a.ts") {
1321
return {
22+
kind: "module",
1423
specifier,
1524
headers: {
1625
"content-type": "text/typescript",
@@ -20,6 +29,7 @@ Deno.test("roundtrip build + parse", async () => {
2029
}
2130

2231
return {
32+
kind: "module",
2333
specifier: "https://example.com/mod.ts",
2434
headers: {
2535
"content-type": "application/typescript",

lib/eszip_wasm.generated.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ const imports = {
347347
var ret = getStringFromWasm0(arg0, arg1);
348348
return addHeapObject(ret);
349349
},
350-
__wbg_resolve_4f8f547f26b30b27: function (arg0) {
351-
var ret = Promise.resolve(getObject(arg0));
352-
return addHeapObject(ret);
353-
},
354350
__wbindgen_json_serialize: function (arg0, arg1) {
355351
const obj = getObject(arg1);
356352
var ret = JSON.stringify(obj === undefined ? null : obj);
@@ -363,14 +359,6 @@ const imports = {
363359
getInt32Memory0()[arg0 / 4 + 1] = len0;
364360
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
365361
},
366-
__wbg_new_16f24b0728c5e67b: function () {
367-
var ret = new Array();
368-
return addHeapObject(ret);
369-
},
370-
__wbg_push_a72df856079e6930: function (arg0, arg1) {
371-
var ret = getObject(arg0).push(getObject(arg1));
372-
return ret;
373-
},
374362
__wbindgen_memory: function () {
375363
var ret = wasm.memory;
376364
return addHeapObject(ret);
@@ -391,6 +379,18 @@ const imports = {
391379
var ret = new Uint8Array(getObject(arg0));
392380
return addHeapObject(ret);
393381
},
382+
__wbg_new_16f24b0728c5e67b: function () {
383+
var ret = new Array();
384+
return addHeapObject(ret);
385+
},
386+
__wbg_push_a72df856079e6930: function (arg0, arg1) {
387+
var ret = getObject(arg0).push(getObject(arg1));
388+
return ret;
389+
},
390+
__wbg_resolve_4f8f547f26b30b27: function (arg0) {
391+
var ret = Promise.resolve(getObject(arg0));
392+
return addHeapObject(ret);
393+
},
394394
__wbg_newwithlength_5f4ce114a24dfe1e: function (arg0) {
395395
var ret = new Uint8Array(arg0 >>> 0);
396396
return addHeapObject(ret);
@@ -580,8 +580,8 @@ const imports = {
580580
var ret = getObject(arg0).then(getObject(arg1));
581581
return addHeapObject(ret);
582582
},
583-
__wbindgen_closure_wrapper8780: function (arg0, arg1, arg2) {
584-
var ret = makeMutClosure(arg0, arg1, 126, __wbg_adapter_24);
583+
__wbindgen_closure_wrapper8872: function (arg0, arg1, arg2) {
584+
var ret = makeMutClosure(arg0, arg1, 129, __wbg_adapter_24);
585585
return addHeapObject(ret);
586586
},
587587
},

lib/eszip_wasm_bg.wasm

19.3 KB
Binary file not shown.

lib/loader.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Adapted from deno_graph
22
// https://github.com/denoland/deno_graph/blob/main/lib/loader.ts
33

4-
export interface LoadResponse {
4+
export interface LoadResponseModule {
5+
/** A module with code has been loaded. */
6+
kind: "module";
57
/** The string URL of the resource. If there were redirects, the final
68
* specifier should be set here, otherwise the requested specifier. */
79
specifier: string;
@@ -12,6 +14,16 @@ export interface LoadResponse {
1214
content: string;
1315
}
1416

17+
export interface LoadResponseExternalBuiltIn {
18+
/** The loaded module is either _external_ or _built-in_ to the runtime. */
19+
kind: "external" | "builtIn";
20+
/** The strung URL of the resource. If there were redirects, the final
21+
* specifier should be set here, otherwise the requested specifier. */
22+
specifier: string;
23+
}
24+
25+
export type LoadResponse = LoadResponseModule | LoadResponseExternalBuiltIn;
26+
1527
export async function load(
1628
specifier: string,
1729
): Promise<LoadResponse | undefined> {
@@ -21,6 +33,7 @@ export async function load(
2133
case "file:": {
2234
const content = await Deno.readTextFile(url);
2335
return {
36+
kind: "module",
2437
specifier,
2538
content,
2639
};
@@ -39,6 +52,7 @@ export async function load(
3952
headers[key.toLowerCase()] = value;
4053
}
4154
return {
55+
kind: "module",
4256
specifier: response.url,
4357
headers,
4458
content,

src/examples/builder.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ async fn main() {
2323
.await
2424
.unwrap()
2525
.unwrap();
26-
let import_map =
27-
import_map::parse_from_json(&resp.specifier, &resp.content).unwrap();
28-
(
29-
Some(import_map.import_map),
30-
Some((resp.specifier, resp.content)),
31-
)
26+
match resp {
27+
deno_graph::source::LoadResponse::Module {
28+
specifier, content, ..
29+
} => {
30+
let import_map =
31+
import_map::parse_from_json(&specifier, &content).unwrap();
32+
(Some(import_map.import_map), Some((specifier, content)))
33+
}
34+
_ => unimplemented!(),
35+
}
3236
} else {
3337
(None, None)
3438
};
@@ -111,7 +115,7 @@ impl deno_graph::source::Loader for Loader {
111115
tokio::fs::canonicalize(specifier.to_file_path().unwrap()).await?;
112116
let content = tokio::fs::read(&path).await?;
113117
let content = String::from_utf8(content)?;
114-
Ok(Some(deno_graph::source::LoadResponse {
118+
Ok(Some(deno_graph::source::LoadResponse::Module {
115119
specifier: Url::from_file_path(&path).unwrap(),
116120
maybe_headers: None,
117121
content: Arc::new(content),
@@ -136,7 +140,7 @@ impl deno_graph::source::Loader for Loader {
136140
}
137141
let url = resp.url().clone();
138142
let content = resp.text().await?;
139-
Ok(Some(deno_graph::source::LoadResponse {
143+
Ok(Some(deno_graph::source::LoadResponse::Module {
140144
specifier: url,
141145
maybe_headers: Some(headers),
142146
content: Arc::new(content),

0 commit comments

Comments
 (0)