Skip to content

Commit

Permalink
refactor: use redirect handling in deno_graph (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Apr 18, 2024
1 parent c0f6afe commit 841476b
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 128 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
[workspace.dependencies]
anyhow = "1.0.44"
base64 = "0.21.5"
deno_graph = { version = "0.72.0", default-features = false }
deno_graph = { version = "0.73.1", default-features = false }
url = { version = "2.3.1" }

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tasks": {
"test": "deno test -A",
"update-snapshots": "deno test -A -- --update",
"build": "cp LICENSE js/LICENSE && deno run -A https://deno.land/x/wasmbuild@0.15.1/main.ts --out js"
"build": "cp LICENSE js/LICENSE && deno run -A jsr:@deno/wasmbuild@0.17.1 --out js"
},
"exclude": [
"lib",
Expand Down
50 changes: 50 additions & 0 deletions js/emit.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// deno-lint-ignore-file
// deno-fmt-ignore-file

export interface InstantiateResult {
instance: WebAssembly.Instance;
exports: {
bundle: typeof bundle;
transpile: typeof transpile
};
}

/** Gets if the Wasm module has been instantiated. */
export function isInstantiated(): boolean;

/** Options for instantiating a Wasm instance. */
export interface InstantiateOptions {
/** Optional url to the Wasm file to instantiate. */
url?: URL;
/** Callback to decompress the raw Wasm file bytes before instantiating. */
decompress?: (bytes: Uint8Array) => Uint8Array;
}

/** Instantiates an instance of the Wasm module returning its functions.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object. */
export function instantiate(opts?: InstantiateOptions): Promise<InstantiateResult["exports"]>;

/** Instantiates an instance of the Wasm module along with its exports.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object. */
export function instantiateWithInstance(opts?: InstantiateOptions): Promise<InstantiateResult>;

/**
* @param {string} root
* @param {Function} load
* @param {string | undefined} maybe_bundle_type
* @param {any} maybe_import_map
* @param {any} maybe_compiler_options
* @param {boolean} minify
* @returns {Promise<any>}
*/
export function bundle(root: string, load: Function, maybe_bundle_type: string | undefined, maybe_import_map: any, maybe_compiler_options: any, minify: boolean): Promise<any>;
/**
* @param {string} root
* @param {Function} load
* @param {any} maybe_import_map
* @param {any} maybe_compiler_options
* @returns {Promise<any>}
*/
export function transpile(root: string, load: Function, maybe_import_map: any, maybe_compiler_options: any): Promise<any>;
40 changes: 30 additions & 10 deletions js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
type CacheSetting,
createCache,
type FetchCacher,
} from "https://deno.land/x/deno_cache@0.7.1/mod.ts";
} from "jsr:@deno/cache-dir@0.8";

/** The output of the {@linkcode bundle} function. */
export interface BundleEmit {
Expand Down Expand Up @@ -218,7 +218,7 @@ export async function bundle(
});
},
type,
processImportMapInput(importMap),
await processImportMapInput(importMap, bundleLoad),
compilerOptions,
minify ?? false,
);
Expand Down Expand Up @@ -275,7 +275,7 @@ export async function transpile(
},
);
},
processImportMapInput(importMap),
await processImportMapInput(importMap, transpileLoad),
compilerOptions,
);
}
Expand Down Expand Up @@ -308,13 +308,33 @@ function checkCompilerOptions(
* @param importMap The import map as provided to the JS API.
* @returns The import map that must be provided to the Rust API.
*/
function processImportMapInput(
async function processImportMapInput(
importMap: ImportMapJsLibInput,
): ImportMapRustLibInput {
load: FetchCacher["load"],
): Promise<ImportMapRustLibInput> {
if (typeof importMap === "string" || importMap instanceof URL) {
return locationToUrl(importMap).toString();
}
if (typeof importMap === "object") {
importMap = locationToUrl(importMap);
const data = await load(importMap.toString(), false, "use");
if (data == null) {
return undefined;
}
switch (data.kind) {
case "module": {
return {
baseUrl: importMap.toString(),
jsonString: data.content instanceof Uint8Array
? new TextDecoder().decode(data.content)
: data.content,
};
}
case "external":
throw new Error("External import maps are not supported.");
default: {
const _assertNever: never = data;
throw new Error("Unexpected kind.");
}
}
} else if (typeof importMap === "object") {
const { baseUrl, imports, scopes } = importMap;
const url = locationToUrl(baseUrl ?? Deno.cwd());
// Rust lib expects url to be the file URL to the import map file, but the
Expand All @@ -327,8 +347,9 @@ function processImportMapInput(
baseUrl: url.toString(),
jsonString: JSON.stringify({ imports, scopes }),
};
} else {
return undefined;
}
return undefined;
}

type ImportMapJsLibInput =
Expand All @@ -340,5 +361,4 @@ type ImportMapRustLibInput =
baseUrl: string;
jsonString: string;
}
| string
| undefined;
4 changes: 2 additions & 2 deletions rs-lib/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,14 @@ mod test {
root: S,
sources: Vec<(S, Source<S>)>,
) -> (ModuleGraph, CapturingModuleAnalyzer, ModuleSpecifier) {
let mut memory_loader = MemoryLoader::new(sources, vec![]);
let memory_loader = MemoryLoader::new(sources, vec![]);
let root = ModuleSpecifier::parse(root.as_ref()).unwrap();
let analyzer = CapturingModuleAnalyzer::default();
let mut graph = ModuleGraph::new(GraphKind::CodeOnly);
graph
.build(
vec![root.clone()],
&mut memory_loader,
&memory_loader,
BuildOptions {
module_analyzer: &analyzer,
..Default::default()
Expand Down
Loading

0 comments on commit 841476b

Please sign in to comment.