2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-11 16:54:53 -04:00
|
|
|
import { assert, assertEquals } from "../../std/testing/asserts.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("compilerApiCompileSources", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.compile("/foo.ts", {
|
2020-01-08 09:17:44 -05:00
|
|
|
"/foo.ts": `import * as bar from "./bar.ts";\n\nconsole.log(bar);\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
"/bar.ts": `export const bar = "bar";\n`,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), [
|
|
|
|
"/bar.js.map",
|
|
|
|
"/bar.js",
|
|
|
|
"/foo.js.map",
|
2020-03-28 13:03:49 -04:00
|
|
|
"/foo.js",
|
2020-01-08 09:17:44 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("compilerApiCompileNoSources", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.compile("./subdir/mod1.ts");
|
2020-01-08 09:17:44 -05:00
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual);
|
|
|
|
const keys = Object.keys(actual);
|
|
|
|
assertEquals(keys.length, 6);
|
|
|
|
assert(keys[0].endsWith("print_hello.js.map"));
|
|
|
|
assert(keys[1].endsWith("print_hello.js"));
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("compilerApiCompileOptions", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.compile(
|
2020-01-08 09:17:44 -05:00
|
|
|
"/foo.ts",
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
"/foo.ts": `export const foo = "foo";`,
|
2020-01-08 09:17:44 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
module: "amd",
|
2020-03-28 13:03:49 -04:00
|
|
|
sourceMap: false,
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), ["/foo.js"]);
|
|
|
|
assert(actual["/foo.js"].startsWith("define("));
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("compilerApiCompileLib", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.compile(
|
2020-02-19 00:34:11 -05:00
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
"/foo.ts": `console.log(document.getElementById("foo"));
|
2020-03-28 13:03:49 -04:00
|
|
|
console.log(Deno.args);`,
|
2020-02-19 00:34:11 -05:00
|
|
|
},
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
lib: ["dom", "es2018", "deno.ns"],
|
2020-02-19 00:34:11 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), ["/foo.js.map", "/foo.js"]);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("compilerApiCompileTypes", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.compile(
|
2020-02-27 11:27:00 -05:00
|
|
|
"/foo.ts",
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
"/foo.ts": `console.log(Foo.bar);`,
|
2020-02-27 11:27:00 -05:00
|
|
|
},
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
types: ["./subdir/foo_types.d.ts"],
|
2020-02-27 11:27:00 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), ["/foo.js.map", "/foo.js"]);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("transpileOnlyApi", async function () {
|
|
|
|
const actual = await Deno.transpileOnly({
|
2020-03-28 13:03:49 -04:00
|
|
|
"foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), ["foo.ts"]);
|
|
|
|
assert(actual["foo.ts"].source.startsWith("export var Foo;"));
|
|
|
|
assert(actual["foo.ts"].map);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("transpileOnlyApiConfig", async function () {
|
|
|
|
const actual = await Deno.transpileOnly(
|
2020-01-08 09:17:44 -05:00
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
"foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
|
2020-01-08 09:17:44 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sourceMap: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
module: "amd",
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert(actual);
|
|
|
|
assertEquals(Object.keys(actual), ["foo.ts"]);
|
|
|
|
assert(actual["foo.ts"].source.startsWith("define("));
|
|
|
|
assert(actual["foo.ts"].map == null);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("bundleApiSources", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.bundle("/foo.ts", {
|
2020-01-08 09:17:44 -05:00
|
|
|
"/foo.ts": `export * from "./bar.ts";\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
"/bar.ts": `export const bar = "bar";\n`,
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
assert(diagnostics == null);
|
2020-03-04 08:26:00 -05:00
|
|
|
assert(actual.includes(`__instantiate("foo")`));
|
2020-02-12 16:41:51 -05:00
|
|
|
assert(actual.includes(`__exp["bar"]`));
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("bundleApiNoSources", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.bundle("./subdir/mod1.ts");
|
2020-01-08 09:17:44 -05:00
|
|
|
assert(diagnostics == null);
|
2020-03-04 08:26:00 -05:00
|
|
|
assert(actual.includes(`__instantiate("mod1")`));
|
2020-02-12 16:41:51 -05:00
|
|
|
assert(actual.includes(`__exp["printHello3"]`));
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("bundleApiConfig", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.bundle(
|
2020-01-08 09:17:44 -05:00
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
"/foo.ts": `// random comment\nexport * from "./bar.ts";\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
"/bar.ts": `export const bar = "bar";\n`,
|
2020-01-08 09:17:44 -05:00
|
|
|
},
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
removeComments: true,
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(!actual.includes(`random`));
|
|
|
|
});
|
2020-02-07 01:54:05 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("bundleApiJsModules", async function () {
|
|
|
|
const [diagnostics, actual] = await Deno.bundle("/foo.js", {
|
2020-03-02 16:18:27 -05:00
|
|
|
"/foo.js": `export * from "./bar.js";\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
"/bar.js": `export const bar = "bar";\n`,
|
2020-03-02 16:18:27 -05:00
|
|
|
});
|
|
|
|
assert(diagnostics == null);
|
|
|
|
assert(actual.includes(`System.register("bar",`));
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("diagnosticsTest", async function () {
|
|
|
|
const [diagnostics] = await Deno.compile("/foo.ts", {
|
2020-03-28 13:03:49 -04:00
|
|
|
"/foo.ts": `document.getElementById("foo");`,
|
2020-02-07 01:54:05 -05:00
|
|
|
});
|
|
|
|
assert(Array.isArray(diagnostics));
|
|
|
|
assert(diagnostics.length === 1);
|
|
|
|
});
|