Skip to content
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

Create aliases for the imports to omit long import statements (dev issue not end user's) #487

Closed
i-amansharma07 opened this issue Jul 7, 2024 · 9 comments · Fixed by #500
Labels
enhancement New feature or request

Comments

@i-amansharma07
Copy link

Problem:
In the Screen Shot below we have a very long import statement which disrupts the code appearance and also make importing components harder which can be done relatively easy manner.

code

Can be seen in these files as well :

Screenshot from 2024-07-07 23-36-36

Solution
We can use path aliasing like @components/AppbarClient there are some alias already present inside next.config.js and we can describe some more and use it as a standard to bring uniformity in the code base.

correct imports examples :

code

@i-amansharma07 i-amansharma07 added the enhancement New feature or request label Jul 7, 2024
@vishwas-babar
Copy link

Hi, I'd like to work on this issue. Could you please assign it to me?

@i-amansharma07
Copy link
Author

Hi, I'd like to work on this issue. Could you please assign it to me?

Hey Vishwas i don't have permissions to assign tasks, you can complete your work and raise a PR, thanks for picking this up.

@vishwas-babar
Copy link

Ok I will work on that then raise a pr

@S007K
Copy link
Contributor

S007K commented Jul 27, 2024

To create aliases in a React codebase, particularly in a Next.js project, you can use path aliasing for import statements. This can be achieved by making modifications to the next.config.js file and then using the defined aliases in the codebase.

Here are the steps to create aliases in a React codebase, specifically in a Next.js project:

Modify next.config.js: In the root of your Next.js project, create or open the next.config.js file. If it doesn't exist, create this file at the root of your project.

Inside next.config.js, you can use the resolve.alias property to define your path aliases. Here's an example of how to define aliases for commonly used paths:

// next.config.js

const path = require('path');
module.exports = {
  webpack: (config, options) => {
    config.resolve.alias['@components'] = path.join(__dirname, 'components');
    config.resolve.alias['@styles'] = path.join(__dirname, 'styles');
    // Add more aliases as needed
    return config;
  },
};

In this example, we're defining aliases for the '@components' and '@Styles' paths.

@S007K
Copy link
Contributor

S007K commented Jul 27, 2024

Using the Aliases in Code: After defining the aliases in next.config.js, you can now use these aliases in your import statements throughout your codebase. For example, if you defined an alias for '@components', you can import components like this:

import AppbarClient from '@components/AppbarClient';

@S007K
Copy link
Contributor

S007K commented Jul 27, 2024

i hope it has answered your issue

@S007K S007K mentioned this issue Jul 27, 2024
2 tasks
@S007K
Copy link
Contributor

S007K commented Jul 27, 2024

i have also raised the PR #500 , on merging it resolved this issue.
Happy coding 👍

@i-amansharma07
Copy link
Author

Yes but can you also use these aliases and update the PR?

@S007K
Copy link
Contributor

S007K commented Jul 27, 2024

yes! thanks for reminding me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants