cmorten / opine

Minimalist web framework for Deno ported from ExpressJS.
https://github.com/cmorten/opine/blob/main/.github/API/api.md
MIT License
854 stars 43 forks source link

Error while bundling opine's mod.ts #98

Closed 4ov closed 3 years ago

4ov commented 3 years ago

Issue

Setup:

I'm receiving an error when i try to use a bundled version of opine

Details

I'm trying to use a bundled version of opine to avoid errors with #97 (deno bundle https://deno.land/x/opine@1.0.0/mod.ts) the bundle generated successfully but when i try to use this bundle like :

import { opine } from "./opine.js";

const app = opine();

app.get("/", function(req, res) {
  res.send("Hello World");
});

app.listen(3000, ()=>console.log('http://localhost:3000'));

I'm getting this error

$ deno run --no-check serve.ts
error: Uncaught ReferenceError: Cannot access 'request2' before initialization
request2.get = function get(name) {
^
    at file:///home/mo/opine.js:309:1

without --no-check :

$ deno run serve.ts
Check file:///home/mo/serve.ts
error: TS2339 [ERROR]: Property 'get' does not exist on type '{ (req: any, res: Response1 | undefined, next: any): void; emit(event: any, arg: any): any; on(event: any, arg: any): any; request: any; response: any; }'.
app.get("/", function(req, res) {
    ~~~
    at file:///home/mo/serve.ts:5:5

TS7006 [ERROR]: Parameter 'req' implicitly has an 'any' type.
app.get("/", function(req, res) {
                      ~~~
    at file:///home/mo/serve.ts:5:23

TS7006 [ERROR]: Parameter 'res' implicitly has an 'any' type.
app.get("/", function(req, res) {
                           ~~~
    at file:///home/mo/serve.ts:5:28

TS2339 [ERROR]: Property 'listen' does not exist on type '{ (req: any, res: Response1 | undefined, next: any): void; emit(event: any, arg: any): any; on(event: any, arg: any): any; request: any; response: any; }'.
app.listen(3000, ()=>console.log('R...'));
    ~~~~~~
    at file:///home/mo/serve.ts:9:5

Found 4 errors.
cmorten commented 3 years ago

Hi @lowray 👋

So the first error you've reported appears to be a bug with deno bundle as far as I can see, the outputted, bundled js file appears to have placed the definition of:

request2.get = ...

on line 309, when in fact request2 is only defined later on line 23284. Hence why we get the reference error.

If you have a use-case for Opine in this bundled form then I would recommend raising an issue to report the bug with deno bundle on the main https://github.com/denoland/deno repo 😄.

...interestingly if you fix the request2 issue by moving the request2.get method to the appropriate place, then you receive the following error:

error: Uncaught ReferenceError: init is not defined
await init(read("./pkg/denoflate_bg.wasm"));
^
    at file:///~/git/asos-craigmorten/opine/opine.js:18261:1

So I sense there may be a few (or perhaps the same?) bugs with the deno bundle CLI command at the moment. This particular one is regarding a missing init method which is completely missing from the bundle, indicating that deno bundle is somehow missing the default export / import of this method within the https://github.com/hazae41/denoflate library which Opine makes use of.


The error you are seeing when not providing the --no-check is due to deno bundle stripping all type information from Opine to create your opine.js file. Without this type information Deno is unable to infer some of the properties on the app object, nor will it know what types the req and res objects have. You would either need to import the Opine types into your file and manually type the various objects, or alternatively, don't use typescript at all by changing your file extension to .js, i.e. serve.js - given you are making use of --no-check which skips typechecking, it may be simpler for you to just use plain JavaScript in the first place!

Ultimately the errors you are seeing are expected behaviour when importing objects and functions from a JS file into a TS file / project.


Given neither of these issues are (afaik) anything wrong with Opine itself I will close this - happy to re-open if something else comes to light!

cmorten commented 3 years ago

Having a read, I believe this will all be fixed in a next release following the merge of https://github.com/denoland/deno/pull/8901 which upgrades swc which is used for bundling. So probably not worth raising a new issue as likely a duplicate of all the listed ones.