Tibbs
Tibbs•5mo ago

Transpile TS to browser-safe JS with no dependencies?

Hey friends! Is there a way to combine Deno CLI commands or configs to transpile my source code from TS to browser-safe JS without pulling in a dependency like the NPM-hosted TS binary? I just want the source transpiled and concatinated, maybe minified. What's the easiest way to do this in a Deno project? Thanks!
8 Replies
Mark G
Mark G•5mo ago
https://esbuild.github.io/ seems to be the most common way to do this within the Deno ecosystem, using this plugin... https://jsr.io/@luca/esbuild-deno-loader to handle Deno imports. Here's an example, I use this to build a simple service worker script, but it's pretty similar to what you'd do to build other types of browser compatible scripts/modules... https://github.com/jollytoad/home/blob/main/scripts/build.ts
JSR
@luca/esbuild-deno-loader - JSR
@luca/esbuild-deno-loader on JSR: Deno module resolution for esbuild
GitHub
home/scripts/build.ts at main · jollytoad/home
My Deno powered homepage. Contribute to jollytoad/home development by creating an account on GitHub.
Jesse
Jesse•5mo ago
This is the de facto (and currently maybe the only) strategy because a specialized resolver is needed for Deno's module graph that understands its caching system and specifier resolution
Tibbs
TibbsOP•5mo ago
@Mark G and @Jesse , thanks for this! In my context, I'm not going to be using any Deno namespace APIs, just pure TS, because I understand the Deno runtime and APIs won't be available in the browser context. Same recommendation?
2saturdayscode
2saturdayscode•5mo ago
Hi Tibbs, yes the issue of bundling in Deno is caused by the fact that Deno uses a different "mechanism" to manage dependencies for a project. For instance you don't have a package.json which usually other bundlers use to figure out the dependencies graph. It doesn't have anything to do with Deno APIs That's why for example if you wish to use esbuild you need a plugin to instruct esbuild how to manage Deno configuration (i.e. deno.json file)
Jesse
Jesse•5mo ago
In that case, you don't really need Deno at all: esbuild should provide everything required to accomplish your goal. You can use the esbuild CLI binary directly.
Tibbs
TibbsOP•5mo ago
Ahhh I also did not know esbuild was available as a node-free binary! https://esbuild.github.io/getting-started/#download-a-build
2saturdayscode
2saturdayscode•5mo ago
Not if he is using url imports or an import map in deno config
Tibbs
TibbsOP•5mo ago
The plan is to build an HTML webcomponent as a part of a monorepo but take dependencies on shared types and interfaces from the main Deno service project, but the core bits of that should all be pure TS. This helps a lot. Thanks everyone 🙂

Did you find this page helpful?