-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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 absolute imports with @
#5118
Comments
Place |
I’d say let’s just add |
@
We'll need to make sure it works with webpack (obviously), but also Jest/Flow/ESLint(?). |
Can we just create |
@gaearon Why does monorepos affect |
If we had monorepo support we’d encourage people to put different top level folders into separate packages instead of having a special way to refer to the root. |
By the way, our individual packages in monorepo, if we don't want to publish it to NPM, how should it be named? |
Doesn't matter. |
I think even with monorepo, we still need a special way to refer to the root. For example, if we needs two axios instances, it would be unreasonable for us to put these strongly business-relevant codes in separate packages. |
@gaearon any update on this? |
The simplest way to achieve this is to create {
// ...
"scripts": {
"preinstall": "rm node_modules/@",
"postinstall": "ln -s `pwd`/src node_modules/@",
// ...
},
// ...
} |
If we would proceed with this would |
@rovansteen I personally prefer an explicit symbol so that I can tell whether the imports come from the application code or a node module. If |
@tanduong it's not about that the |
You could then argue if the absolute path should be the root directory of the application or the |
Is there any IDE support on this for proper auto-complete etc? Didn't see such practice on corporate projects before. But relative paths are very annoying indeed. |
@Z-AX definitely IDEs auto-complete and indexing will become an issue. Even in Vue, I didn't use this. It sounds good, it looks good, but in practice becomes an issue. |
@Z-AX |
VS Code at least supports jsconfig.json which uses the exact same "paths" options as tsconfig to tell the editor how to resolve paths. https://code.visualstudio.com/docs/languages/jsconfig So far as I can tell, TypeScript does not support NODE_PATH. I was adopting TypeScript in an existing CRA app relying on NODE_PATH, and tsconfig's path options saved me from running into a big blocker while CRA's typescript support was in beta. @tanduong's proposal for a filesystem link isn't OS independent, so now I'm stalled trying to figure out what to do with a project I over optimistically started porting over to typescript and is just completely broken ATM edit: Using a collage of packages (rimraf, lnk-cli, globstar), I think I have a cross environment happy pre/post install setup that seems to be mostly working
|
There is also another plausible alternative — we could embrace |
|
Probably — let's discuss in #5645? I don't fully understand what this entails. |
what about... '@/': paths.appSrc + '/', |
This script is so DANGEROUS! |
Hi! I tried this solution but is not working for me. This is my tsconfig.json: { |
My solution:
Update "scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
"eject": "react-app-rewired eject"
}, Add const path = require('path');
module.exports = function override(config) {
config.resolve = {
...config.resolve,
alias: { '@': path.resolve(__dirname, 'src') },
};
return config;
}; Add {
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
} Add the following to "extends": "./tsconfig.paths.json", // or jsconfig.paths.json CRA warns about |
@jjenzz your solution works smooth! Hope the core team will provide a solution without other packages to achieve it ... |
Thanks for your solution. One silly question, how I can use to solution to avoid using '@'? I mean, instead of doing `@/components/DummyComponent' I would like to do 'components/DummyComponent'. |
@jjvainstein rly? :) second comment in this thread |
@Kepro that simple solution doesn't work for me and many other people. I tried creating that .env file with it doesn't work. So for that reason I was thinking that with the solution using eact-app-rewired plus some modification I can achieve that. |
Is there any news? |
just wanted to add to @jjenzz solution that you also need to map // package.json
"jest": {
"moduleNameMapper": {
"@/(.*)": "<rootDir>/src/$1"
}
}, |
This really works super awesome .. |
|
This enables me to use absolute paths, but not aliases, right? // edit |
webpack.base.conf.js
wrote as below:https://github.com/vuejs-templates/webpack/blob/master/template/build/webpack.base.conf.js#L40
Can we use
import Hello from '@/components/Hello'
like vue?The text was updated successfully, but these errors were encountered: