-
Notifications
You must be signed in to change notification settings - Fork 10.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
Support for TypeScripts 'paths' option #4357
Comments
It looks like you'd need to use the Note that Gatsby v1 uses webpack version one. Gatsby v2 will upgrade to a newer release of webpack. |
@m-allanson thanks. Not sure how to make it work. I've tried that:
then
|
Not a complete solution, but I got rid of the TS error (in VS Code) with this in
Though gatsby/webpack still throws an error that the dependency is not found when I import a module using path defined in |
@m-allanson any idea how to make it work? |
I resolved the problem after having a look here #484. Basically, what I do is add an exports.modifyWebpackConfig = function({ config, env }) {
config.merge({
resolve: {
alias: {
`[ALIAS]`: [PATH_TO_ALIAS]
}
}
});
return config;
}; |
Thanks @t49tran. It'd be nice for |
I think it makes complete sense @m-allanson, I will have a look into gatsby-plugin-typescript and see if I can do something. |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
I was struggling to make this work, not realizing the example by @t49tran was using the v1 gatsby config API. For the record, if anyone arrives here looking for a solution using v2, here's the summary: You don't need the In tsconfig I have this: "baseUrl": ".",
"paths": {
"@src/*": ["src/*"]
} So in code you can write things like Then to make the build succeed, add to exports.onCreateWebpackConfig = function({ actions }) {
actions.setWebpackConfig({
resolve: {
alias: {
'@src': path.resolve(__dirname, 'src')
}
}
})
} I personally like the |
This could also indicate a Thanks for the |
@jbmusso You're right, a different character would probably be better. I realized that shortly after :) But I haven't ran into any problems yet. The prefixes I'm using are so generic they are not very likely to clash with a library namespace (src, components, modules, utils, config, ...) |
@0x80 Forgetting this line at the top of the
Here's a quick starter project for anyone interested: |
Has anybody been successful at getting this working while also using TSLint? I have followed what @0x08's recommendedation but I keep running into a "not found" error:
I'll also run in to other lint errors (like |
If anyone's still struggling with this issue, I was able to get it working pretty easily using tsconfig-paths-webpack-plugin. tsconfig.json
gatsby-node.js
And then you can import like this
|
This certainly works for Gatsby v2. |
How to remove the warning from VSCode? Did anyone found a solution to this? |
I'm also struggling with this in Gatsby v3. i've tried variations of with/without |
Deleted my prior comment. This Gatsby plugin is working for me: https://www.gatsbyjs.com/plugins/gatsby-plugin-tsconfig-paths/ Its key limitation is that it doesn't work for |
Description
I'm trying to implement alias for importing Typescript modules from root of my project. According to typescripts docs it can be done by adding following configuration to 'tsconfig.json' file
Then from anywhere ts/tsx file:
import { MyComponent } from '@components/MyComponent'
.Environment
Gatsby version: 1.1.40
Node.js version: v8.9.4
Operating System: macOS 10.13.3
Actual result
Module
@components/MyComponent
not found. The Same behavior has been fixed withcreate-react-app
: issue 203.Expected behavior
Typescript should find my module.
Steps to reproduce
1.
gatsby new website-gatsby-clean https://github.com/haysclark/gatsby-starter-typescript
2. add Typescript paths config to
tsconfig.json
3. create
src/components/MyComponent.tsx
4. import
MyComponent
intosrc/pages/index.tsx
The text was updated successfully, but these errors were encountered: