denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.03k stars 5.23k forks source link

deno bundle minification #6900

Closed ghost closed 3 days ago

ghost commented 4 years ago

Similar to how Deno uses a TypeScript compiler when running code, users should have the option when bundling to minify their compiled files for better performance. The argument after bundle could be -m or -minify. We could see if the minifier would be uglify, which from what I've heard is normally used with npm.

Any thoughts?

kitsonk commented 4 years ago

This would be a long term goal.

We couldn't use uglify as it doesn't work with modern JavaScript, uglify-es is no longer maintained, terser is the successor to uglify. Even then we wouldn't use it. We would look to swc though its minification likely needs improvement.

ghost commented 4 years ago

Well, I suppose that this would be the tool to be used! It seems more fitting upon inspection, anyhow. I believe as long as its minification process does not come with a critical error that would ruin the code itself, it can grow alongside Deno, couldn't it?

kitsonk commented 4 years ago

We already use swc in Deno. Again this is still a long term goal, as there are a load of other things we need to do (including ones related to bundling, see: #4549) before this became a priority, some of which would block this (source maps for example).

kitsonk commented 3 years ago

This potentially could be considered now that we have moved over bundling to swc.

kitsonk commented 3 years ago

minification is on the swc roadmap but not implemented yet

ebebbington commented 3 years ago

Would be great to see minification for deno bundle! I use deno bundle for my client side TS code, so it'd be great to minify it as well

pierredewilde commented 3 years ago

In the meantime, you may use esbuild, an extremely fast JavaScript minifier written in Go, to minify your Deno bundles:

$ deno bundle https://deno.land/std@0.79.0/http/file_server.ts | esbuild --minify > file_server.min.js
$ deno run --allow-net --allow-read file_server.min.js

Other JavaScript minifiers (eg terser) may have issues like Unexpected character '#' - See https://github.com/terser/terser/issues/702

ghost commented 3 years ago

Sounds like a shortcut or alias can be made from that! @pierredewilde

IllusionPerdu commented 3 years ago

For information they're some work in swc for minify https://github.com/swc-project/swc/pull/1302

David-Else commented 3 years ago

In the meantime, you may use esbuild, an extremely fast JavaScript minifier written in Go, to minify your Deno bundles:

$ deno bundle https://deno.land/std@0.79.0/http/file_server.ts | esbuild --minify > file_server.min.js
$ deno run --allow-net --allow-read file_server.min.js

Other JavaScript minifiers (eg terser) may have issues like Unexpected character '#' - See terser/terser#702

esbuild now supports deno! https://github.com/evanw/esbuild/releases/tag/v0.11.11

MichaelCurrin commented 3 years ago

Thanks @David-Else . That is a module that only works in a script though. I installed it and then found esbuild did nothing.

David-Else commented 3 years ago

@MichaelCurrin You ran this as a .js file and it worked?

import * as esbuild from 'https://deno.land/x/esbuild@v0.11.11/mod.js'
const ts = 'let hasProcess: boolean = typeof process != "null"'
const result = await esbuild.transform(ts, { loader: 'ts', logLevel: 'warning' })
console.log('result:', result)
esbuild.stop()

Does it not install the esbuild binary? I suggest you have a look at the later comments here: https://github.com/evanw/esbuild/issues/936 and comment if you have problems. You need to use the API rather than the CLI https://esbuild.github.io/api/

I have not actually sorted it myself yet, but it is in the 'in box' :)

MichaelCurrin commented 3 years ago

@David-Else I ran this

deno install -f --allow-read 'https://deno.land/x/esbuild@v0.11.11/mod.js'

Which installs to ~/.deno/bin.

Then running esbuild in the shell does nothing not even an error or version, because the mod script has no CLI entrypoint.

I could rewrite as a TS file using the API, but I have a bunch of command line flags that work already if I use npx esbuild on the main branch, so I'm not going rewrite those now. I'll wait until a CLI format ready. Makefile on feature branch

kitsonk commented 3 years ago

Please keep the information on topic. If you want to discuss esbuild under Deno, we have a discussion forum.

kurai-espoir commented 3 years ago

Any update on this?

josh-hemphill commented 2 years ago

If deno uses swc, and swc allows for config files to set things like minification, Is it possible to just expose that for configuring the compile command. (might also be a good idea to include that in the new deno.json config file like there is for tsconfig) https://swc.rs/docs/configuration/minification

kitsonk commented 2 years ago

Not really, as the way Deno interfaces with swc is not via a configuration file, but instead by directly interfacing to the Rust APIs, and the configuration is quite opinionated.

That being said, it could be something that the wider community could contribute by adding a --minify flag to deno bundle and deno compile and would require familiarity with the Rust API of swc. We would want to ensure that the source maps are right and proper for deno bundle.

KnorpelSenf commented 2 years ago

If this gets implemented, would you also add a minify?: boolean option to Deno.emit?

kamilogorek commented 1 year ago

In https://github.com/denoland/deno/issues/16149 @kitsonk explained why it's not going to happen. I'd close this issue in favor of https://github.com/denoland/deno/issues/15463

lucacasonato commented 3 days ago

deno bundle was removed in Deno 2.