1

How can I convert a typescript library into a single javascript file?

For example, oak is a 3rd party library that provides itself via typescript. In deno, you can import oak like this:

import { Application } from "https://deno.land/x/oak/mod.ts";

I would like to convert https://deno.land/x/oak/mod.ts to a standalone javascript file, that could be imported like this:

import { Application } from "./oak.js";

How can I accomplish this conversion?

Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
  • 1
    create a ts file in vs and copy paste mod.ts on it and push build then js file will be generated . – A Farmanbar May 20 '20 at 03:39
  • @Mr.AF Is there a deno command that can accomplish this conversion? I don't use VS. – Lonnie Best May 20 '20 at 03:41
  • 1
    i am not familiar with deno but another approach is using this https://www.typescriptlang.org/play/ paste there. – A Farmanbar May 20 '20 at 03:43
  • Oh heck. I already asked basically the same question yesterday. It was answered [here](https://stackoverflow.com/a/61883691/217867). So basically, you just do this deno command: `deno bundle https://deno.land/x/oak/mod.ts oak.bundle.js` – Lonnie Best May 20 '20 at 04:22

1 Answers1

1

You can use the bundler tool with the deno bundle command:

deno bundle https://deno.land/x/oak/mod.ts oak.js

You can now import oak.js and use it on your JavaScript code:

import { Application } from "./oak.js";

const app = new Application();

app.use((ctx) => {
  ctx.response.body = "Hello World!";
});

await app.listen({ port: 8000 });
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165