Skip to content

Commit

Permalink
Forward arguments from JS to Rust
Browse files Browse the repository at this point in the history
Since the initial release, the bundle type, the compiler options and the
imports were not properly forwarded from JS to Rust.

We now forward them, and added some tests to verify this behaviour.

Closes denoland#83, denoland#69, denoland#29.
  • Loading branch information
yacinehmito committed Apr 4, 2023
1 parent b586977 commit 54fceab
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 5 deletions.
14 changes: 10 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export async function bundle(
cacheSetting,
cacheRoot,
allowRemote,
type,
compilerOptions,
} = options;
let bundleLoad = load;
if (!bundleLoad) {
Expand All @@ -154,13 +156,17 @@ export async function bundle(
}
root = root instanceof URL ? root : toFileUrl(resolve(root));
const { bundle: jsBundle } = await instantiate();
return jsBundle(
const result = await jsBundle(
root.toString(),
bundleLoad,
JSON.stringify(imports),
undefined,
undefined,
type,
imports,
compilerOptions,
);
return {
code: result.code,
map: result.maybe_map ?? undefined,
};
}

/** Transpile TypeScript (or JavaScript) into JavaScript, returning a promise
Expand Down

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function hello() {
return "Hello there!";
}
export { hello as default };

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function hello() {
return "Hello there!";
}
export { hello as default };

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function hello() {
return "Hello there!";
}
export { hello as default };
86 changes: 85 additions & 1 deletion tests/bundle_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertStringIncludes } from "https://deno.land/std@0.182.0/testing/asserts.ts";
import {
assert,
assertEquals,
assertExists,
assertStringIncludes,
} from "https://deno.land/std@0.182.0/testing/asserts.ts";
import { resolveFixture, testBundle } from "./utils.ts";

// FIXME: This repeats the test below. Consider supporting URLs without wrapping
Expand Down Expand Up @@ -56,3 +61,82 @@ Deno.test({
);
}),
});

Deno.test({
name: "inline source maps are enabled by default",
fn: testBundle(resolveFixture("mod.ts"), undefined, (result) => {
assertEquals(result.map, undefined);
assert(
result.code.split("\n").at(-2)?.startsWith(
"//# sourceMappingURL=data:application/json;base64,",
),
);
}),
});

Deno.test({
name: "setting inlineSourceMap to true produces inline source maps",
fn: testBundle(resolveFixture("mod.ts"), {
compilerOptions: {
inlineSourceMap: true,
},
}, (result) => {
assertEquals(result.map, undefined);
assert(
result.code.split("\n").at(-2)?.startsWith(
"//# sourceMappingURL=data:application/json;base64,",
),
);
}),
});

Deno.test({
name: "setting inlineSourceMap does not produce any source maps",
fn: testBundle(resolveFixture("mod.ts"), {
compilerOptions: {
inlineSourceMap: false,
},
}, (result) => {
assertEquals(result.map, undefined);
assert(
!result.code.includes(
"//# sourceMappingURL=data:application/json;base64,",
),
);
}),
});

Deno.test({
name:
"setting sourceMap to true is not enough to produce external source maps as inline takes precedence",
fn: testBundle(resolveFixture("mod.ts"), {
compilerOptions: {
inlineSourceMap: false,
},
}, (result) => {
assertEquals(result.map, undefined);
assert(
!result.code.includes(
"//# sourceMappingURL=data:application/json;base64,",
),
);
}),
});

Deno.test({
name:
"setting sourceMap to true and inlineSourceMap to false produces external source maps",
fn: testBundle(resolveFixture("mod.ts"), {
compilerOptions: {
sourceMap: true,
inlineSourceMap: false,
},
}, (result) => {
assertExists(result.map);
assert(
!result.code.includes(
"//# sourceMappingURL=data:application/json;base64,",
),
);
}),
});

0 comments on commit 54fceab

Please sign in to comment.