mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 04:39:10 +00:00
refactor(cli): runtime compiler APIs consolidated to Deno.emit() (#8799)
Closes: #4752
This commit is contained in:
parent
5f4e1767fe
commit
012f99bd9a
14 changed files with 592 additions and 516 deletions
|
@ -6,15 +6,21 @@ import {
|
|||
} from "../../std/testing/asserts.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.compile() - sources provided",
|
||||
name: "Deno.emit() - sources provided",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.compile("/foo.ts", {
|
||||
"/foo.ts": `import * as bar from "./bar.ts";\n\nconsole.log(bar);\n`,
|
||||
"/bar.ts": `export const bar = "bar";\n`,
|
||||
});
|
||||
assert(diagnostics == null);
|
||||
assert(actual);
|
||||
const keys = Object.keys(actual).sort();
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
sources: {
|
||||
"/foo.ts": `import * as bar from "./bar.ts";\n\nconsole.log(bar);\n`,
|
||||
"/bar.ts": `export const bar = "bar";\n`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
const keys = Object.keys(files).sort();
|
||||
assert(keys[0].endsWith("/bar.ts.js"));
|
||||
assert(keys[1].endsWith("/bar.ts.js.map"));
|
||||
assert(keys[2].endsWith("/foo.ts.js"));
|
||||
|
@ -23,12 +29,15 @@ Deno.test({
|
|||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.compile() - no sources provided",
|
||||
name: "Deno.emit() - no sources provided",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.compile("./subdir/mod1.ts");
|
||||
assert(diagnostics == null);
|
||||
assert(actual);
|
||||
const keys = Object.keys(actual).sort();
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"./subdir/mod1.ts",
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
const keys = Object.keys(files).sort();
|
||||
assertEquals(keys.length, 6);
|
||||
assert(keys[0].endsWith("cli/tests/subdir/mod1.ts.js"));
|
||||
assert(keys[1].endsWith("cli/tests/subdir/mod1.ts.js.map"));
|
||||
|
@ -36,183 +45,246 @@ Deno.test({
|
|||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.compile() - compiler options effects emit",
|
||||
name: "Deno.emit() - compiler options effects emit",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.compile(
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
"/foo.ts": `export const foo = "foo";`,
|
||||
},
|
||||
{
|
||||
module: "amd",
|
||||
sourceMap: false,
|
||||
compilerOptions: {
|
||||
module: "amd",
|
||||
sourceMap: false,
|
||||
},
|
||||
sources: { "/foo.ts": `export const foo = "foo";` },
|
||||
},
|
||||
);
|
||||
assert(diagnostics == null);
|
||||
assert(actual);
|
||||
const keys = Object.keys(actual);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
const keys = Object.keys(files);
|
||||
assertEquals(keys.length, 1);
|
||||
const key = keys[0];
|
||||
assert(key.endsWith("/foo.ts.js"));
|
||||
assert(actual[key].startsWith("define("));
|
||||
assert(files[key].startsWith("define("));
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.compile() - pass lib in compiler options",
|
||||
name: "Deno.emit() - pass lib in compiler options",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.compile(
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"file:///foo.ts",
|
||||
{
|
||||
"file:///foo.ts": `console.log(document.getElementById("foo"));
|
||||
console.log(Deno.args);`,
|
||||
},
|
||||
{
|
||||
lib: ["dom", "es2018", "deno.ns"],
|
||||
compilerOptions: {
|
||||
lib: ["dom", "es2018", "deno.ns"],
|
||||
},
|
||||
sources: {
|
||||
"file:///foo.ts": `console.log(document.getElementById("foo"));
|
||||
console.log(Deno.args);`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assert(diagnostics == null);
|
||||
assert(actual);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
const keys = Object.keys(files).sort();
|
||||
assertEquals(keys, ["file:///foo.ts.js", "file:///foo.ts.js.map"]);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.emit() - import maps",
|
||||
async fn() {
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"file:///a.ts",
|
||||
{
|
||||
importMap: {
|
||||
imports: {
|
||||
"b": "./b.ts",
|
||||
},
|
||||
},
|
||||
importMapPath: "file:///import-map.json",
|
||||
sources: {
|
||||
"file:///a.ts": `import * as b from "b"
|
||||
console.log(b);`,
|
||||
"file:///b.ts": `export const b = "b";`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
const keys = Object.keys(files).sort();
|
||||
assertEquals(
|
||||
Object.keys(actual).sort(),
|
||||
["file:///foo.ts.js", "file:///foo.ts.js.map"],
|
||||
keys,
|
||||
[
|
||||
"file:///a.ts.js",
|
||||
"file:///a.ts.js.map",
|
||||
"file:///b.ts.js",
|
||||
"file:///b.ts.js.map",
|
||||
],
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
// TODO(@kitsonk) figure the "right way" to restore support for types
|
||||
// Deno.test({
|
||||
// name: "Deno.compile() - properly handles .d.ts files",
|
||||
// async fn() {
|
||||
// const [diagnostics, actual] = await Deno.compile(
|
||||
// "/foo.ts",
|
||||
// {
|
||||
// "/foo.ts": `console.log(Foo.bar);`,
|
||||
// "/foo_types.d.ts": `declare namespace Foo {
|
||||
// const bar: string;
|
||||
// }`,
|
||||
// },
|
||||
// {
|
||||
// types: ["/foo_types.d.ts"],
|
||||
// },
|
||||
// );
|
||||
// assert(diagnostics == null);
|
||||
// assert(actual);
|
||||
// assertEquals(
|
||||
// Object.keys(actual).sort(),
|
||||
// ["file:///foo.ts.js", "file:///file.ts.js.map"],
|
||||
// );
|
||||
// },
|
||||
// });
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.transpileOnly()",
|
||||
name: "Deno.emit() - no check",
|
||||
async fn() {
|
||||
const actual = await Deno.transpileOnly({
|
||||
"foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
|
||||
});
|
||||
assert(actual);
|
||||
assertEquals(Object.keys(actual), ["foo.ts"]);
|
||||
assert(actual["foo.ts"].source.startsWith("export var Foo;"));
|
||||
assert(actual["foo.ts"].map);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.transpileOnly() - config effects commit",
|
||||
async fn() {
|
||||
const actual = await Deno.transpileOnly(
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
"foo.ts": `/** This is JSDoc */\nexport enum Foo { Foo, Bar, Baz };\n`,
|
||||
},
|
||||
{
|
||||
removeComments: true,
|
||||
check: false,
|
||||
sources: {
|
||||
"/foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assert(actual);
|
||||
assertEquals(Object.keys(actual), ["foo.ts"]);
|
||||
assert(!actual["foo.ts"].source.includes("This is JSDoc"));
|
||||
assert(actual["foo.ts"].map);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 3);
|
||||
const keys = Object.keys(files).sort();
|
||||
assert(keys[0].endsWith("/foo.ts.js"));
|
||||
assert(keys[1].endsWith("/foo.ts.js.map"));
|
||||
assert(files[keys[0]].startsWith("export var Foo;"));
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.bundle() - sources passed",
|
||||
name: "Deno.emit() - no check - config effects emit",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.bundle("/foo.ts", {
|
||||
"/foo.ts": `export * from "./bar.ts";\n`,
|
||||
"/bar.ts": `export const bar = "bar";\n`,
|
||||
});
|
||||
assert(diagnostics == null);
|
||||
assert(actual.includes(`const bar = "bar"`));
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
check: false,
|
||||
compilerOptions: { removeComments: true },
|
||||
sources: {
|
||||
"/foo.ts":
|
||||
`/** This is JSDoc */\nexport enum Foo { Foo, Bar, Baz };\n`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 3);
|
||||
const keys = Object.keys(files).sort();
|
||||
assert(keys[0].endsWith("/foo.ts.js"));
|
||||
assert(keys[1].endsWith("/foo.ts.js.map"));
|
||||
assert(!files[keys[0]].includes("This is JSDoc"));
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.bundle() - no sources passed",
|
||||
name: "Deno.emit() - bundle esm - with sources",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.bundle("./subdir/mod1.ts");
|
||||
assert(diagnostics == null);
|
||||
assert(actual.length);
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
bundle: "esm",
|
||||
sources: {
|
||||
"/foo.ts": `export * from "./bar.ts";\n`,
|
||||
"/bar.ts": `export const bar = "bar";\n`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
||||
assert(files["deno:///bundle.js"].includes(`const bar = "bar"`));
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.bundle() - JS Modules included",
|
||||
name: "Deno.emit() - bundle esm - no sources",
|
||||
async fn() {
|
||||
const [diagnostics, actual] = await Deno.bundle("/foo.js", {
|
||||
"/foo.js": `export * from "./bar.js";\n`,
|
||||
"/bar.js": `export const bar = "bar";\n`,
|
||||
});
|
||||
assert(diagnostics == null);
|
||||
assert(actual.includes(`const bar = "bar"`));
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"./subdir/mod1.ts",
|
||||
{
|
||||
bundle: "esm",
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
||||
assert(files["deno:///bundle.js"].length);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "runtime compiler APIs diagnostics",
|
||||
name: "Deno.emit() - bundle esm - include js modules",
|
||||
async fn() {
|
||||
const [diagnostics] = await Deno.compile("/foo.ts", {
|
||||
"/foo.ts": `document.getElementById("foo");`,
|
||||
});
|
||||
assert(Array.isArray(diagnostics));
|
||||
assert(diagnostics.length === 1);
|
||||
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
||||
"/foo.js",
|
||||
{
|
||||
bundle: "esm",
|
||||
sources: {
|
||||
"/foo.js": `export * from "./bar.js";\n`,
|
||||
"/bar.js": `export const bar = "bar";\n`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 0);
|
||||
assert(!ignoredOptions);
|
||||
assertEquals(stats.length, 12);
|
||||
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
||||
assert(files["deno:///bundle.js"].includes(`const bar = "bar"`));
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Deno.emit() - generates diagnostics",
|
||||
async fn() {
|
||||
const { diagnostics, files } = await Deno.emit(
|
||||
"/foo.ts",
|
||||
{
|
||||
sources: {
|
||||
"/foo.ts": `document.getElementById("foo");`,
|
||||
},
|
||||
},
|
||||
);
|
||||
assertEquals(diagnostics.length, 1);
|
||||
const keys = Object.keys(files).sort();
|
||||
assert(keys[0].endsWith("/foo.ts.js"));
|
||||
assert(keys[1].endsWith("/foo.ts.js.map"));
|
||||
},
|
||||
});
|
||||
|
||||
// See https://github.com/denoland/deno/issues/6908
|
||||
Deno.test({
|
||||
name: "Deno.compile() - SWC diagnostics",
|
||||
name: "Deno.emit() - invalid syntax does not panic",
|
||||
async fn() {
|
||||
await assertThrowsAsync(async () => {
|
||||
await Deno.compile("/main.js", {
|
||||
"/main.js": `
|
||||
export class Foo {
|
||||
constructor() {
|
||||
console.log("foo");
|
||||
}
|
||||
export get() {
|
||||
console.log("bar");
|
||||
}
|
||||
}`,
|
||||
await Deno.emit("/main.js", {
|
||||
sources: {
|
||||
"/main.js": `
|
||||
export class Foo {
|
||||
constructor() {
|
||||
console.log("foo");
|
||||
}
|
||||
export get() {
|
||||
console.log("bar");
|
||||
}
|
||||
}`,
|
||||
},
|
||||
});
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: `Deno.compile() - Allows setting of "importsNotUsedAsValues"`,
|
||||
name: 'Deno.emit() - allows setting of "importsNotUsedAsValues"',
|
||||
async fn() {
|
||||
const [diagnostics] = await Deno.compile("/a.ts", {
|
||||
"/a.ts": `import { B } from "./b.ts";
|
||||
const b: B = { b: "b" };
|
||||
`,
|
||||
"/b.ts": `export interface B {
|
||||
b: string;
|
||||
};
|
||||
`,
|
||||
}, {
|
||||
importsNotUsedAsValues: "error",
|
||||
const { diagnostics } = await Deno.emit("/a.ts", {
|
||||
sources: {
|
||||
"/a.ts": `import { B } from "./b.ts";
|
||||
const b: B = { b: "b" };`,
|
||||
"/b.ts": `export interface B {
|
||||
b:string;
|
||||
};`,
|
||||
},
|
||||
compilerOptions: {
|
||||
importsNotUsedAsValues: "error",
|
||||
},
|
||||
});
|
||||
assert(diagnostics);
|
||||
assertEquals(diagnostics.length, 1);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue