esm-dev / esm.sh

A fast, smart, & global CDN for modern(es2015+) web development.
https://esm.sh
MIT License
2.95k stars 143 forks source link

"esmLexer: invalid syntax, require javascript/typescript" on valid file #803

Closed limbic-derek closed 4 months ago

limbic-derek commented 4 months ago

I have a basic TypeScript file like this:

// file.ts
import { MyType, MyFunc } from "somemodule";
export const Foo = {} as const satisfies MyType;
export default MyFunc(Foo);

esm.sh (v135, running locally in docker) fails with this message:

2024/02/28 22:41:24 [error] build 'v135/../file.js': esmLexer: invalid syntax, require javascript/typescript

I'm wondering if the satisfies keyword is causing issues? I am not getting much else with loglevel=debug. Strangely, files that depend on those exports are working fine, and I see Foo being inlined into them (which I don't want, but that's OK for now). I am expecting this file to still be addressable/exist.

limbic-derek commented 4 months ago

I tried removing as const satisfies MyType and I'm still getting this failure. Other files seem fine, but I'm not sure what is unique about this one.

limbic-derek commented 4 months ago

OK it looks like this package (js_parser) is being invoked here: https://github.com/esm-dev/esm.sh/blob/main/server/utils.go#L205

It looks like that only validates JavaScript files using this: https://github.com/ije/esbuild-internal/blob/master/js_parser/js_parser.go

Although, it seems to allow TypeScript files to be "found" and passed in. Writing a small test, I think this is the root cause:

--- FAIL: TestStrictMode (0.00s)
    --- FAIL: TestStrictMode/import{A,B}from"C";export_const_D={}_as_const_satisfies_A;export_default_B(D); (0.00s)
        js_parser_test.go:286: 
            +<stdin>: ERROR: Expected ";" but found "as"

The util should probably make use of the ts_parser if one of the ts extensions is selected: https://github.com/ije/esbuild-internal/blob/master/js_parser/ts_parser.go

This was a confusing error because the message states require either javascript OR typescript, so it made me assume both were valid here. Upon inspection, I believe TS isn't properly supported.

Wishing I could see the actual <stdin> errors that are being produced in situ to confirm further.

limbic-derek commented 4 months ago

Confirmed locally that enabling TS parsing allows these files to be properly served via esm.sh.

ije commented 4 months ago

i just pushed docker image to v135_2 with your patch, thanks for contributing

bennypowers commented 1 month ago

Please release this fix, as 135 still fails, for example:

https://esm.sh/v135/@rhds/elements@1.4.1/es2022/lib/context/color/context-color.css.js

/* esm.sh - error */
throw new Error("[esm.sh] " + "esmLexer: invalid syntax, require javascript/typescript");
export default null;

and https://esm.sh/v135_2/@rhds/elements@1.4.1/es2022/lib/context/color/context-color.css.js 404s

Thank you for making this helpful service available.