sveltejs / svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
MIT License
1.76k stars 151 forks source link

TypeScript not being processed when no filename is given #488

Closed bfanger closed 3 months ago

bfanger commented 2 years ago

Describe the bug if (filename == null) return { code: content }; in svelte-preprocess/src/transformers/typescript.ts line 497 is the issue.

To Reproduce Call svelte.preprocess(code, processor) manually without providing the third optional parameter ( omitting the filename )

https://github.com/wangjia184/svelte-parser/blob/master/src/worker.ts (not mine, not small)

Expected behavior when inputing const x : number = 1 the output to be const x = 1, even when no filename was provided.

Additional context When you provide a weird filename like " " then the preprocessor will work and remove the type annotations.

Conduitry commented 2 years ago

Is the request that the preprocessor blindly strip types without any checking of there is no filename? I'm not sure what else it could do. I'm also not sure whether that could cause problems with enums or any other language features.

bfanger commented 2 years ago

That would be an improvement over leaving the annotation in and getting Unexpected token errors when trying to execute or parse the resulting code.

Gildedter commented 2 years ago

Not sure if my issue relates to this but when I do this:

// script
  import { Status, type Period, type IStuff} from "$lib/types"; // types.d.ts

// html
// custom component
<LabelInput
  label="Status"
  id={`${id}-status`}
  name={`${id}-status`}
  required
  value={Status[currentStatus]} // enum
  {disabled}
/>

then load the parent component, I get this error:

Loading module from “https://localhost:PORT/src/lib/types” was blocked because of a disallowed MIME type (“text/html”).

Edit 1: nvm, fixed the issue by renaming types.d.ts -> types.ts