-
-
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
[TypeScript] baseUrl
inside tsconfig.json not supporting.
#202
Comments
Also, related, paths in tsconfig does not seem be resolving correctly.. "paths": {
"@store/*": [
"src/client/app/store/*"
],
"@shared/*": [
"src/client/shared/*"
],
"@client/*": [
"src/client/*"
],
"*": [
"*"
]
}, |
Can someone make a simple reproduction repo triggering the error? |
Yep, here we go: $ parcel index.html
⏳ Building...
Server running at http://localhost:1234
🚨 /Users/ty3uk/projects/js/parcel-test/src/Main.ts:3:23: Cannot resolve dependency '@components/Custom'
1 | import hello from '@components/Custom';
2 | import { bye } from 'components/NewCustom';
> 3 |
| ^
4 | hello();
5 | bye();
6 | |
Note that this shouldn't be considered a bug, but a feature or enhancement, since the baseUrl and paths options in the tsconfig are only meant to affect type checking. Actual path resolution is the job of the bundler/module loader. With that said, as a possible solution, maybe we could make use of this, by translating the options from the tsconfig into the babel plugin's config accordingly. Unless anyone else has a better idea, I could try making a solution out of that. |
Not right. If you run |
That's what I'm talking about. If you run $ tsc src/Main.ts
src/Main.ts(1,19): error TS2307: Cannot find module '@components/Custom'.
src/Main.ts(2,21): error TS2307: Cannot find module 'components/NewCustom'.
$ tsc
$ |
Huh, I must be mistaken then, since it's never outputted the resolved module paths for me in the past unless I configured it separately outside of the tsconfig. I'll look into it some more later. EDIT: No, I tested to make sure, and I'm not mistaken. What I meant was that typescript itself will not edit the import paths in the compiled output. All |
@kingdaro Yes, the typescript compiler will not handle the path. That's why I mentioned I tried to point this out in my post, but maybe not so clear: |
Yes, and I fully agree! Just wanted to mention that it'd be a feature instead of a bug as an aside, but I suppose that's somewhat pedantic, so ignore that part of my comment I guess 😅 |
@shunia I just finished implementing it on |
@fathyb Sorry, but since I'm new to parcel, I may need a |
Update: I just published it, you can just use the latest version on npm of Parcel and the plugin : |
@shunia you are using the test version I have built for Angular AOT compilation (I'm not good with npm tags, sorry). Can you use the 0.2.0 version? ( Also let's keep this issue about Parcel support of this feature and not my plugin bugs, please continue on the plugin issue tracker, or in Slack. |
Is there a solution currently? |
@atilkan it's only supported with |
@fathyb is there any example? Appritiate it. |
@atilkan Just install the right version of the plugin (mostly the latest) and it's done. |
I opened a separate issue #4014 but later closed it because it was basically a duplicate of this one. I thought I would mention here though that my newer issue included a link to a repro case that I created: https://github.com/uglycoyote/parcel-import-problem This might make it easier for Parcel developers to test that the |
We can make use of Typescript's internal module resolve, especially now that Parcel 2 supports customization of resolve. The following code successfully resolves the target component:
The output is:
I think TypeScript's built-in resolver would also handle I'll be happy to take up implementing this as a resolver for parcel 2, but will need someone to help guiding me since I'm not familiar with parcel yet. I'm looking into moving my project from webpack to parcel 2 for performance and missing |
Simple solution using babel-plugin-module-resolver: $ yarn add --dev babel-plugin-module-resolver Then create (or modify) a {
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@components": "./src/components",
"@features": "./src/features"
}
}
]
]
} |
Note that in Parcel 2 (currently in alpha), |
@mbrowne Hey, thanks for pointing that out. I'll edit my comment accordingly, but pretty much that was just a personal token choice. We can exchange |
The "simple" setup by @chiefGui worked for me: parcel-bundler: .babelrc {
"plugins": [
[
"module-resolver",
{
"root": [
"./src"
],
"alias": {
"~": "./src"
}
}
]
]
} tsconfig.json {
// .......
"compilerOptions": {
// .......
"baseUrl": "./",
"paths": {
"~/*": [
"src/*"
]
}
}
} A React/Typescript component example: import * as React from "react"
import * as ReactDOM from "react-dom"
import { WorldMap } from "~/core/worldmap"
import "~/assets/main.css"
export class App extends React.Component {
render () {
return (
<div>
<WorldMap />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById("root")) |
@joseluisq It should work with just |
@mbrowne yeah, because I've copied and pasted. It just works for TS and Babel. So comment above updated. |
@chiefGui Doesn't your solution add an additional overhead of processing ts files with Babel? I guess by default ts files are only processed with TypeScript compiler. |
I just moved to monorepos with typescript and after spending countless hours with Parcel2 (also downgraded to 1) I just can't get moduel resolution working with all the guides there is. Only thing that works is using relative imports. Worked relatively fine before monorepo. In submodules I need tsconfig options alot. If someone has working example of monorepo with working path resolution I would like to see it. (Feel free to suggest better place for this comment) Tried fuse-box bundler now that has build in typescript support and based on small testing works like a charm with small monorepo I put up. Also the bundle times were much faster and no caching issues (with parcel I need to destroy my cache many times a day) |
I see a fair amount of quick solutions here, but I spent a lot of time on my tsconfig and just do not want to switch away. It kind of irked me that there wasn't a good path forward and seems like there won't be, so I created |
Is their any way to have endpoints point to folders without the / in ~/. E.G ~types over ~/types. This is my current setup so babelrc {
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"~": "./src"
}
}
]
]
}
tsconfig {
"compilerOptions": {
...
"rootDir": ".",
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
}
}
import { type } from '~/types' I have tried removing the / (ie |
@zachbryant are you planning to release 1.0.0 of |
This is now released in nightly if anyone wants to try it and report back. |
Should this work in parcel 2.12? I can't seem to get any absolute imports working using any baseUrl-isms so curious if its actually "turned on"? |
It should work automatically: https://parceljs.org/features/dependency-resolution/#tsconfig Otherwise, please open a new issue with a reproduction and I can take a look |
It doesn't seeeem to. I am just redacting an strace to post, you can clearly see it going through the right directory but it doesn't actually look for source files rather than directories. |
Here trying to resolve `import 'ui'"!
|
Is there something to copy in the testsuite? |
🐛 bug report
🎛 Configuration (.babelrc, package.json, cli command)
tsconfig.json
:🤔 Expected Behavior
structure:
component/Custom.ts:
Main.ts:
console should show 'Hello!'
😯 Current Behavior
Running from shell:
💁 Possible Solution
The compiler should read
tsconfig.json
file right, and understands thatbaseUrl
has made thefrom
part ofimport ... from ...
no need to be the actual relative path from the current file.In my case, because of
baseUrl
set to./src
, the originalimport hello from '../componet/Custom' can be replaced as
import hello from 'component/Custom'`.This might because of the way typescript compiles the
import
part just as what is written in the codes:is just compiled to:
And then, parcel-require can not recognize the path and throws error.
🔦 Context
Our actual project are full of relative paths based on
baseUrl
, which we omitted the../
parts when using relative import. Withwebpack
andawesome-typescript-loader
it works as expected. So when we are trying to get our hands on parcel, this problem really blocks us from going further.🌍 Your Environment
Sorry for my poor english.
The text was updated successfully, but these errors were encountered: