Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ModuleKind to graph #107

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ ENV PATH=${DENO_INSTALL}/bin:${PATH} \
DENO_DIR=${DENO_INSTALL}/.cache/deno

RUN rustup target add wasm32-unknown-unknown
RUN cargo install -f wasm-bindgen-cli
RUN cargo install -f wasm-bindgen-cli --version 0.2.78

RUN chown -R vscode /usr/local/cargo
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: ➡️ install rust
uses: hecrj/[email protected]
with:
rust-version: 1.57.0
rust-version: 1.58.0

- name: ➡️ install clippy and rustfmt
run: |
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# deno_graph

[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https://deno.land/x/deno_graph/mod.ts)
[![Build Status - Cirrus][]][Build status] [![Twitter handle][]][Twitter badge]
[![Discord Chat](https://img.shields.io/discord/684898665143206084?logo=discord&style=social)](https://discord.gg/deno)

Expand Down Expand Up @@ -170,10 +171,6 @@ The module graph has two methods which provide the output of `deno info`. The
method `toString()` provides the text output from `deno info` and `toJSON()`
provides the JSON object structure from `deno info --json`.

> ℹ️ currently, the Deno CLI hasn't been refactored to consume the `deno_graph`
> crate and there are minor differences in the output, specifically with the
> JSON output, and the current `deno info` command.

> ℹ️ currently, there is no runtime access to the `DENO_DIR`/Deno cache, and
> there is no default implementation of the API when creating a module graph,
> therefore cache information is missing from the output. An implementation of
Expand All @@ -193,8 +190,8 @@ console.log(graph.toString());

This would output to stdout and would respect the `NO_COLOR`/`Deno.noColor`
setting. If colors are allowed, the string will include the ANSI color escape
sequences, otherwise they will be omitted. This behaviour can be explicitly
overriden by passing `true` to always remove ANSI colors or `false` to force
sequences, otherwise they will be omitted. This behavior can be explicitly
overridden by passing `true` to always remove ANSI colors or `false` to force
them.

To replicate `deno info --json https://deno.land/x/std/testing/asserts.ts`, you
Expand Down
22 changes: 16 additions & 6 deletions lib/deno_graph.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
}
function __wbg_adapter_24(arg0, arg1, arg2) {
wasm
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h2469e2a2d6d6e0a1(
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h1fd07681c8357b96(
arg0,
arg1,
addHeapObject(arg2),
Expand All @@ -223,6 +223,7 @@ function isLikeNone(x) {
* @param {any} maybe_headers
* @param {string | undefined} maybe_jsx_import_source_module
* @param {string} content
* @param {any} maybe_kind
* @param {Function | undefined} maybe_resolve
* @param {Function | undefined} maybe_resolve_types
* @returns {Module}
Expand All @@ -232,6 +233,7 @@ export function parseModule(
maybe_headers,
maybe_jsx_import_source_module,
content,
maybe_kind,
maybe_resolve,
maybe_resolve_types,
) {
Expand Down Expand Up @@ -263,6 +265,7 @@ export function parseModule(
len1,
ptr2,
len2,
addHeapObject(maybe_kind),
isLikeNone(maybe_resolve) ? 0 : addHeapObject(maybe_resolve),
isLikeNone(maybe_resolve_types) ? 0 : addHeapObject(maybe_resolve_types),
);
Expand Down Expand Up @@ -346,8 +349,8 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_76(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h73a1d270bad7e73b(
function __wbg_adapter_77(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__hc409106696154fff(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -411,6 +414,13 @@ export class Module {
var ret = wasm.module_dependencies(this.ptr);
return takeObject(ret);
}
/**
* @returns {any}
*/
get kind() {
var ret = wasm.module_kind(this.ptr);
return takeObject(ret);
}
/**
* @returns {string}
*/
Expand Down Expand Up @@ -751,7 +761,7 @@ const imports = {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_76(a, state0.b, arg0, arg1);
return __wbg_adapter_77(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand Down Expand Up @@ -815,8 +825,8 @@ const imports = {
__wbindgen_rethrow: function (arg0) {
throw takeObject(arg0);
},
__wbindgen_closure_wrapper1466: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 270, __wbg_adapter_24);
__wbindgen_closure_wrapper1538: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 287, __wbg_adapter_24);
return addHeapObject(ret);
},
},
Expand Down
Binary file modified lib/deno_graph_bg.wasm
Binary file not shown.
31 changes: 31 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface TypesDependency {
source?: Range;
}

export interface ResolveResult {
/** The string URL of the fully qualified specifier for a module. */
specifier: string;
/** The module kind of the resolved module. */
kind: ModuleKind;
}

export interface LoadResponse {
/** The string URL of the resource. If there were redirects, the final
* specifier should be set here, otherwise the requested specifier. */
Expand Down Expand Up @@ -79,6 +86,24 @@ export interface TypesDependencyJson {
dependency: ResolvedDependency;
}

/** The kind of module.
*
* For asserted modules, the value of the `asserted` property is set to the
* `type` value of the assertion.
*
* Dependency analysis is not performed for asserted, AMD, Script, SystemJS, or
* UMD modules currently. Synthetic modules were injected into the graph with
* their own dependencies provided. */
export type ModuleKind =
| "amd"
| "asserted"
| "commonJs"
| "esm"
| "script"
| "synthetic"
| "systemJs"
| "umd";

export interface DependencyJson {
/** The string specifier that was used for the dependency. */
specifier: string;
Expand All @@ -98,6 +123,9 @@ export interface ModuleJson extends CacheInfo {
specifier: string;
/** Any error encountered when attempting to load the module. */
error?: string;
/** The module kind that was determined when the module was resolved. This is
* used by loaders to indicate how a module needs to be loaded at runtime. */
kind?: ModuleKind;
/** An array of dependencies that were identified in the module. */
dependencies?: DependencyJson[];
/** If the module had a types dependency, the information about that
Expand Down Expand Up @@ -151,6 +179,9 @@ export class Module {
/** A record of the dependencies, where the key is the string specifier of
* the dependency and the value is the dependency object. */
readonly dependencies?: Record<string, Dependency>;
/** A module kind that can be used to determine how a module should be loaded
* at runtime. */
readonly kind: ModuleKind;
/** The media type assigned to the module. This determines how Deno will
* handle the module. */
readonly mediaType: MediaType;
Expand Down
53 changes: 42 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

/**
* A JavaScript/TypeScript interface to the Deno CLI's module graph logic.
*
* ### Example
*
* To build and output a graph as a JSON structure to the console:
*
* ```ts
* import { createGraph } from "https://deno.land/x/deno_graph@{VERSION}/mod.ts";
*
* const graph = await createGraph("https://deno.land/x/std/testing/asserts.ts");
*
* console.log(JSON.stringify(graph, undefined, " "));
* ```
*
* @module
*/

import {
createGraph as jsCreateGraph,
parseModule as jsParseModule,
Expand All @@ -10,6 +28,8 @@ import type {
LoadResponse,
Module,
ModuleGraph,
ModuleKind,
ResolveResult,
TypesDependency,
} from "./lib/types.d.ts";

Expand All @@ -21,6 +41,8 @@ export type {
Module,
ModuleGraph,
ModuleGraphJson,
ModuleKind,
ResolveResult,
TypesDependency,
} from "./lib/types.d.ts";

Expand Down Expand Up @@ -56,8 +78,10 @@ export interface CreateGraphOptions {
* module graph to be "overridden". This is intended to allow items like an
* import map to be used with the module graph. The callback takes the string
* of the module specifier from the referrer and the string URL of the
* referrer. The callback then returns a resolved URL string specifier. */
resolve?(specifier: string, referrer: string): string;
* referrer. The callback then returns a fully qualified resolved URL string
* specifier or an object which contains the URL string and the module kind.
* If just the string is returned, the module kind is inferred to be ESM. */
resolve?(specifier: string, referrer: string): string | ResolveResult;
kitsonk marked this conversation as resolved.
Show resolved Hide resolved
/** An optional callback that can allow custom logic of how type dependencies
* of a module to be provided. This will be called if a module is being added
* to the graph that is is non-typed source code (e.g. JavaScript/JSX) and
Expand Down Expand Up @@ -117,23 +141,24 @@ export function createGraph(
* import { createGraph } from "https://deno.land/x/deno_graph/mod.ts";
*
* const graph = await createGraph([
* "https://example.com/a.ts",
* "https://example.com/a.ts"
* ["https://example.com/a.ts", "esm"],
* ["https://example.com/a.ts", "esm"],
* ]);
*
* console.log(graph.toJSON());
* ```
*
* @param rootSpecifiers An array of URL string of the root module specifiers
* to build the graph from.
* @param rootSpecifiers An array of URL strings or tuples of URL strings and
* module kinds of the root module specifiers to build
* the graph from.
* @param options A set of options for building the graph
*/
export function createGraph(
rootSpecifiers: string[],
rootSpecifiers: string[] | [string, ModuleKind][],
options?: CreateGraphOptions,
): Promise<ModuleGraph>;
export function createGraph(
rootSpecifiers: string | string[],
rootSpecifiers: string | string[] | [string, ModuleKind][],
options: CreateGraphOptions = {},
): Promise<ModuleGraph> {
rootSpecifiers = Array.isArray(rootSpecifiers)
Expand Down Expand Up @@ -174,12 +199,16 @@ export interface ParseModuleOptions {
/** When identifying a `@jsxImportSource` pragma, what module name will be
* appended to the import source. This defaults to `jsx-runtime`. */
jsxImportSourceModule?: string;
/** The kind of module to set on the resulting parsed module. */
kind?: ModuleKind;
/** An optional callback that allows the default resolution logic of the
* module graph to be "overridden". This is intended to allow items like an
* import map to be used with the module graph. The callback takes the string
* of the module specifier from the referrer and the string URL of the
* referrer. The callback then returns a resolved URL string specifier. */
resolve?(specifier: string, referrer: string): string;
* referrer. The callback then returns a fully qualified resolved URL string
* specifier or an object which contains the URL string and the module kind.
* If just the string is returned, the module kind is inferred to be ESM. */
resolve?(specifier: string, referrer: string): string | ResolveResult;
kitsonk marked this conversation as resolved.
Show resolved Hide resolved
/** An optional callback that can allow custom logic of how type dependencies
* of a module to be provided. This will be called if a module is being added
* to the graph that is is non-typed source code (e.g. JavaScript/JSX) and
Expand All @@ -201,12 +230,14 @@ export function parseModule(
content: string,
options: ParseModuleOptions = {},
): Module {
const { headers, jsxImportSourceModule, resolve, resolveTypes } = options;
const { headers, jsxImportSourceModule, kind, resolve, resolveTypes } =
options;
return jsParseModule(
specifier,
headers,
jsxImportSourceModule,
content,
kind,
resolve,
resolveTypes,
) as Module;
Expand Down
Loading