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

Add alias for absolute imports in coral. #150

Merged
merged 5 commits into from
Oct 26, 2022
Merged

Conversation

programmiri
Copy link
Contributor

About this change - What it does
Configures TS and vite to use absolut imports where "@/" maps to coral/src.
Adds a eslint rule to restrict imports using relative paths except from the same directory.

Why this way
Avoiding relative imports is making potential refactoring more smooth. It also adds readability for the imports.

@programmiri programmiri requested review from a team as code owners October 25, 2022 13:13
Copy link
Contributor

@SmuliS SmuliS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you feel about changing @ into ´src´. That way we would not have a magic alias, but something that follows the directory structure.

coral/.eslintrc.cjs Outdated Show resolved Hide resolved
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about changing "@/*" -> "src/*". So basically we would not introduce any real aliases, but we would always start the absolute imports from src.

@SmuliS
Copy link
Contributor

SmuliS commented Oct 26, 2022

We could also approach the problem differently without the use of aliases. We could do the following change to tsconfig.json:

"baseUrl": ".",
"paths": {
  "@/*": ["src/*"]
},

->

"baseUrl": "src",

and in vite.config.ts we could replace the alias:

resolve: {
  alias: {
    "@": resolve(rootPath, "./src"),
  },
},

with a root definition: root: resolve(__dirname, "src").

That way the src would be the true root directory and we could import eveything as import App from "App";. If we would implement the followin approach, we would also need to move index.html to src directory, but I think that OK as that file can be considered as source code and not a static asset.

coral/vite.config.ts Outdated Show resolved Hide resolved
@programmiri
Copy link
Contributor Author

What do you feel about changing @ into ´src´. That way we would not have a magic alias, but something that follows the directory structure.

We can do that! I like the magic @ because than it's e.g. @/domain/... and @/components which adds readability, but src instead of @ is not really a big difference, so pure personal preference 😬 Will change that!

@programmiri
Copy link
Contributor Author

We could also approach the problem differently without the use of aliases. We could do the following change to tsconfig.json:

"baseUrl": ".",
"paths": {
  "@/*": ["src/*"]
},

->

"baseUrl": "src",

and in vite.config.ts we could replace the alias:

resolve: {
  alias: {
    "@": resolve(rootPath, "./src"),
  },
},

with a root definition: root: resolve(__dirname, "src").

That way the src would be the true root directory and we could import eveything as import App from "App";. If we would implement the followin approach, we would also need to move index.html to src directory, but I think that OK as that file can be considered as source code and not a static asset.

I'll try if that works (without too much config in vite)!

@programmiri programmiri force-pushed the add-non-relative-imports branch from dd0a9eb to e70b823 Compare October 26, 2022 12:12
@programmiri programmiri requested a review from SmuliS October 26, 2022 12:13
Copy link
Contributor

@SmuliS SmuliS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two very minor comments. Looks like it works without setting the root in vite.config.ts.

coral/src/pages/hello/index.test.tsx Outdated Show resolved Hide resolved
coral/src/pages/index.test.tsx Outdated Show resolved Hide resolved
coral/src/router.tsx Outdated Show resolved Hide resolved
@SmuliS SmuliS merged commit ef8e08a into main Oct 26, 2022
@SmuliS SmuliS deleted the add-non-relative-imports branch October 26, 2022 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants