-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.ts files importing from npm module with provided .js and .ts sources do not resolve const enums #3645
Comments
Related: #3624 (const enums aren't supported).
not sure if we can do something about that... |
Is there potentially any downside to manually first executing |
I don't think so (but be sure that tsc emits modules with ES6 imports (not CommonJS) and also set the target to the newest value to leave the JS feature transpilation to Parcel) |
A work-around for now: I use "build": "tsc && parcel build index.html" And to keep Parcel's Hot Module Replacement working, the following watch-script can be used: "watch": "tsc && npm-run-all -p watch:*",
"watch:ts": "tsc -w",
"watch:js": "parcel index.html", It requires Initially the watching takes longer because tsc first must be executed to make sure Parcel does not run on work-in-progress files. So far I have not seen any downsides to this approach, other than being an undesired work-around. I will keep this issue updated if I encounter anything problematic. Also, I will keep it open as the issue itself is not fixed by it, unless a Dev decides otherwise. |
This is how I got it working in the end. Bit clunky but works for now. |
Bump (to remove stale marking), unexpected differences between standard TypeScript compilation and Parcel TypeScript compilation should be considered an issue and left open until fixed (even if it's a downstream dependency that has to apply the fix). |
Parcel 2's approach is to use This article sums up the reasons: https://ncjamieson.com/dont-export-const-enums/
So |
At the least, it can confuse people why |
It's already on the todo list for the new typescript docs: https://parcel2-docs.now.sh/recipes/typescript/ |
This also breaks transformations that need to analyze multiple files in the project and ensures that Parcel cannot notify of any Typescript semantic errors (as ts.transpileModule does not output any semantic errors). Only syntactic errors can be observed in Parcel due to this design decision. |
🐛 bug report
Given is the following example.ts file:
And given is an npm module with the following files:
my-module.ts:
The module itself is working properly.
🎛 Configuration (.babelrc, package.json, cli command)
Everything default. tsconfig.json has
preserveConstEnums: false
(intentionally).🤔 Expected Behavior
In the bundled code, the line from the example.ts should look like:
😯 Current Behavior
Currently however, it bundles it to:
Whereas MyEnum.Something must be resolved to the constant string behind it. Obviously, MyEnum is not existing at run time and thus gives me "undefined" errors.
💁 Possible Solution
I have tested the following:
First running
tsc
and then usingparcel build
on the outputted files. It worked correctly, as tsc does inline the constant enums correctly. tsc seems to process the provided .d.ts correct to use the constant strings given.Without knowing on what exact way Parcel currently compiles TypeScript files, I would suggest tsc itself must run over the entire project, and then Parcel performs it's other processing.
🔦 Context
I created an own private npm module exporting .js but also the original .ts files, to provide maximum compatibility for those not using TypeScript.
However, now I see myself unable to use const enums from my own bundle in my TypeScript files.
The only solution would be to use preserveConstEnums, which however is only a work around and no desired fix. Alternatively I could use a const variable with a set of given strings. But that also is only a work around.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: