Skip to content

Commit

Permalink
feat: local and remote shim modules (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Feb 18, 2022
1 parent c9fbfa7 commit aa75e18
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 108 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ await build({
scriptModule: false, // node-fetch 3+ only supports ESM
// ...etc...
shims: {
// ...etc...
custom: [{
package: {
name: "node-fetch",
Expand All @@ -239,9 +238,7 @@ await build({
}],
}, {
// this is what `blob: true` does internally
package: {
name: "buffer", // uses node's "buffer" module
},
module: "buffer", // uses node's "buffer" module
globalNames: ["Blob"],
}, {
// this is what `domException: true` does internally
Expand Down Expand Up @@ -271,6 +268,35 @@ await build({
});
```

#### Local or Remote Shim Modules

Custom shims can also refer to local or remote modules:

```ts
await build({
// ...etc...
shims: {
custom: [{
module: "./my-custom-fetch-implementation.ts",
globalNames: ["fetch"],
}, {
module: "https://deno.land/x/some_remote_shim_module/mod.ts",
globalNames: ["setTimeout"],
}],
},
});
```

Where `my-custom-fetch-implementation.ts` contains:

```ts
export function fetch(/* etc... */) {
// etc...
}
```

This is useful in situations where you want to implement your own shim.

### Specifier to Npm Package Mappings

In most cases, dnt won't know about an npm package being available for one of your dependencies and will download remote modules to include in your package. There are scenarios though where an npm package may exist and you want to use it instead. This can be done by providing a specifier to npm package mapping.
Expand Down
76 changes: 38 additions & 38 deletions lib/pkg/dnt_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,6 @@ import { fetch_specifier } from './snippets/dnt-wasm-a15ef721fa5290c5/helpers.js

let wasm;

const heap = new Array(32).fill(undefined);

heap.push(undefined, null, true, false);

function getObject(idx) { return heap[idx]; }

let heap_next = heap.length;

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();
Expand All @@ -38,6 +18,12 @@ function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

const heap = new Array(32).fill(undefined);

heap.push(undefined, null, true, false);

let heap_next = heap.length;

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
Expand All @@ -47,6 +33,8 @@ function addHeapObject(obj) {
return idx;
}

function getObject(idx) { return heap[idx]; }

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');
Expand Down Expand Up @@ -110,6 +98,18 @@ function getInt32Memory0() {
return cachegetInt32Memory0;
}

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

function debugString(val) {
// primitive types
const type = typeof val;
Expand Down Expand Up @@ -260,22 +260,6 @@ async function init(input) {
}
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
var ret = false;
return ret;
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
var ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_json_parse = function(arg0, arg1) {
var ret = JSON.parse(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
Expand All @@ -288,6 +272,18 @@ async function init(input) {
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
var ret = false;
return ret;
};
imports.wbg.__wbg_fetchspecifier_bcacee1e00bcee02 = function(arg0, arg1) {
try {
var ret = fetch_specifier(getStringFromWasm0(arg0, arg1));
Expand All @@ -309,6 +305,10 @@ async function init(input) {
var ret = typeof(val) === 'object' && val !== null;
return ret;
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
var ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbg_new_693216e109162396 = function() {
var ret = new Error();
return addHeapObject(ret);
Expand Down Expand Up @@ -407,8 +407,8 @@ async function init(input) {
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbindgen_closure_wrapper445 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 197, __wbg_adapter_24);
imports.wbg.__wbindgen_closure_wrapper510 = function(arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 208, __wbg_adapter_24);
return addHeapObject(ret);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/pkg/dnt_wasm_bg.ts

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions lib/shims.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { assertEquals } from "./test.deps.ts";
import { shimOptionsToTransformShims } from "./shims.ts";
import { PackageShim } from "../transform.ts";

Deno.test("should get when all true", () => {
const result = shimOptionsToTransformShims({
Expand Down Expand Up @@ -80,7 +81,13 @@ Deno.test("should get for inner deno namespace", () => {
});

assertEquals(result.shims.length, 1);
assertEquals(result.shims[0].package.name, "@deno/shim-deno-test");
assertEquals(
(result.shims[0] as PackageShim).package.name,
"@deno/shim-deno-test",
);
assertEquals(result.testShims.length, 1);
assertEquals(result.testShims[0].package.name, "@deno/shim-deno-test");
assertEquals(
(result.testShims[0] as PackageShim).package.name,
"@deno/shim-deno-test",
);
});
4 changes: 1 addition & 3 deletions lib/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ function getCryptoShim(): Shim {

function getBlobShim(): Shim {
return {
package: {
name: "buffer",
},
module: "buffer",
globalNames: ["Blob"],
};
}
Expand Down
Loading

0 comments on commit aa75e18

Please sign in to comment.