-
-
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
Importing typescript configured for ESM not supported by Parcel #7823
Comments
A resolution is much needed here. This issue is currently blocking us from migrating our Open Source repos to esm. |
Here is note about the reasoning: https://www.typescriptlang.org/docs/handbook/esm-node.html The TypeScript approach is actually very sensible and also in line with future Types in JavaScript: https://devblogs.microsoft.com/typescript/a-proposal-for-type-syntax-in-javascript/ |
Typescript requiring full extension in imports is fine. But I feel like it's typescript's responsibility to rewrite that from ".ts" to ".js". Otherwise, every other tool will need to have workarounds just for the sake of typescript. |
@itsfarseen No absolutely not. An import is an external source. Its part of module resolution to figure out wether it is there in JavaScript or TypeScript format. As to full paths -> that is a requirement of ES modules, so not in TypeScripts scope, especially not with moving to Types out of the box in JavaScript, and supporting TypeScript without actual type checking directly in V8. |
@philkunz Let's consider the pros vs cons. Feel free to add to this list :) If typescript decides to transpile import statements, just like other language construct it transpiles: Pros:
Cons:
|
@itsfarseen It is unnecessary transpilation. TypeScript is a superset of JavaScript. Why transpile something that does not need transpilation? In the future, ideally, you only have to write a .ts file in the module chain, if you want to use a feature, that conflicts with JavaScript syntax. Otherwise, JavaScript is TypeScript and vice versa. You are introducing complexity at the code level, that can be solved in tooling. Anyway this issue is about getting TypeScript esm support to work in parcel, not about a discussion what TypeScript deems right. |
@devongovett @mischnic Or would this be solved in swc? |
No, it's in parcel/packages/resolvers/default/src/DefaultResolver.js Lines 18 to 41 in 699f0b2
which calls this class
|
Ah I see. parcel/packages/resolvers/default/src/DefaultResolver.js Lines 34 to 41 in 699f0b2
would basically need to return the .ts file, when no .js is present. |
Any update on this issue? |
Another note on the reasoning for this on the TypeScript repo is at TypeScript#27481. This may be fixed in the future by TypeScript#37582 to allow I think the best approach here is to try to accommodate for this now rather than wait for TypeScript developers to add a flag or configuration option, which could take a long time. Maybe Parcel could add a config option for resolving I've tried looking through NodeResolver.js but editing the code personally is still a bit daunting for me and I can't figure out where to start or what to modify. |
I implemented a parcel plugin for [dirty] solving that problem You can check it here |
I moved on to esbuild. Esbuild just works with esm and is really fast. |
I’m having the same issue. @LiberQuack’s plugin lets me work around it. Thanks! |
Current situation with Parcel v2 is We need to import files using .js extension despite file is But if we change it to .js extension, then Parcel won't be able to build the project: How can we solve this with Parcel v2 currently? @devongovett @mischnic |
I have developed a plugin which allows you to use .jsx / .js extensions inside a TypeScript module file. It should also work with all other Parcel specific things (like tilde, absolute chars etc): |
I've made a tiny simple Vanilla (no dependencies) browser app in TypeScript that already runs fine in the browser after compiling TypeScript (to ESM modules). I wanted to add a bundler for minification etc, so I decided to try out Parcel for the first time. @b8kkyn parcel-resolver-typescript-esm did not work according to documentation (with the current version of parcel). For Webpack I use resolve-typescript-plugin which works fine. I find it a bit strange that native support for ESM compliant TypeScript code is not given priority for bundlers. |
Resolving |
@devongovett I have the issue, latest Parcel and everything ( We can see that Parcel is aware of the correct file My config: {
"extends": "@parcel/config-default",
"resolvers": [
"@parcel/resolver-glob",
"..."
]
} Edit: I made a trivial, 5 lines resolver plugin that remaps .js to .ts and it works 😅 |
🐛 bug report
import "foo"
should be written asimport "foo.js"
even if we are importingfoo.ts
..ts
file if it's present. Otherwise it tries to import the.js
file.foo.js
file but there isfoo.ts
, Parcel errors out sayingfoo.js
not found.More info:
🎛 Configuration (.babelrc, package.json, cli command)
.parcelrc
: Nonepackage.json
:{ .., "type": "module", .. }
🤔 Expected Behavior
In a TS file,
import "foo.js"
should try to importfoo.ts
. If not present, try to importfoo.js
.😯 Current Behavior
import "foo.js"
fails iffoo.js
is not present, regardless of the presence offoo.ts
🔦 Context
Here's my file structure:
I'm trying to import
main.ts
inindex.ts
.lib
is configured to compile to an ESM package, that can work on both client and server side.So the imports inside
main.ts
must have.js
extension.Otherwise, typescript will emit it as
import "foo"
, and the import will fail at runtime because ESM specs require an extension.🧪 Minimal Repro
https://github.com/itsfarseen/repro-parcel-ts
🌍 Your Environment
The text was updated successfully, but these errors were encountered: