denofn / denopack

The bundling and minification toolset, made for Deno
https://denopack.mod.land
MIT License
99 stars 8 forks source link

Support for top-level await #10

Open PaulLeCam opened 4 years ago

PaulLeCam commented 4 years ago

Hey, thanks for this project, it looks like a great way to bring nice Deno code to the browser!

Is your feature request related to a problem? Please describe.

I'd like to use denopack to bundle any deno std library not using the Deno APIs.

I tried bundling a project but it fails with Uncaught Error: Terser is not supposed to return nothing!. I narrowed down the problem to having export { createHash } from 'https://deno.land/std@0.65.0/hash/mod.ts' in my deps.ts file and my assumption, comparing with other imports from std that can get compiled correctly, is that it's because the std/hash module uses WASM imports.

Describe the solution you'd like

Rollup has a WASM plugin that would be great to support in denopack, either as a plugin or in core. Alternatively, if there is an easy way to convert/wrap existing Rollup plugins to denopack ones it could be a good option.

Describe alternatives you've considered

Using deno bundle + minifier

Thanks!

jeroenptrs commented 4 years ago

Hey @PaulLeCam what version of denopack are you using? Could you share your denopack config? I'm not 100% certain, but I think the issue isn't necessarily the fact that it's using WASM, so I'd like to investigate it further.

PaulLeCam commented 4 years ago

Yes WASM is my assumption but that might be something else, this was just the most obvious difference I found between std/hash and other std libraries.

Here is a simple way I could reproduce the issue (I'm using Deno 1.3.0):

File test.ts:

export { createHash } from 'https://deno.land/std@0.65.0/hash/mod.ts'

And running:

deno run --unstable --allow-env --allow-net --allow-read --allow-write https://deno.land/x/denopack@0.5.0/cli.ts bundle -i test.ts
jeroenptrs commented 4 years ago

@PaulLeCam I have some good news and bad news: the good news is that as far as I know, this is not related to WASM, and I'm fairly certain bundling WASM should work. the bad news however is that this is related to the fact that Terser does not support top level awaits yet and this is used inside of the implementation of std/hash.

https://deno.land/std@0.65.0/hash/_wasm/hash.ts {
  error: SyntaxError: Unexpected await identifier inside strict mode

I will leave this issue open as this is fairly annoying to anyone wanting to use std/hash

PaulLeCam commented 4 years ago

Thanks for investigating! Too bad about Terser, I guess it's even a larger problem than std/hash because any module could possibly user a top-level await.

jeroenptrs commented 4 years ago

Yep, I'm thinking of a good way to support this going forward. I think I might need to change the Terser plugin back to minifying the bundle chunks instead of as a transform hook or something similar. Will test some approaches this week after I finished up the plugins I've been working on 😄

jeroenptrs commented 4 years ago

Thanks for renaming the issue btw 👍

lucacasonato commented 4 years ago

I think I fixed this in a terser update a few weeks ago.

jeroenptrs commented 4 years ago

Does Terser 5 support TLA now?

lucacasonato commented 4 years ago

I think that is why I updated it in the first place... can't remember now though. I'll check real quick.

lucacasonato commented 4 years ago

Oh never mind - I updated because optional chaining... so this is not fixed yet.

jeroenptrs commented 4 years ago

Yeah I just checked and the issue regarding awaits on Terser's github is still open :c sucks but we'll keep an eye out on it ;)