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

ADR: Configuration of import aliases in Javascript applications #6

Merged
merged 6 commits into from
Nov 22, 2020

Conversation

yordis
Copy link
Member

@yordis yordis commented Nov 18, 2020

No description provided.

@yordis yordis added State: Draft The ADR is being discussed. Type: ADR It is something related to ADRs. labels Nov 18, 2020
Copy link
Contributor

@manuphatak manuphatak left a comment

Choose a reason for hiding this comment

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

I like to throw my best counterargument at these ADRs. I can't think of one for this ADR. This ADR is rock-solid advice.

adrs/2068541249/README.md Outdated Show resolved Hide resolved
@yordis yordis changed the title ADR: Configuration of baseUrl and paths of jsconfig/tsconfig files in applications ADR: Configuration of import aliases in Javascript applications Nov 20, 2020
@yordis
Copy link
Member Author

yordis commented Nov 20, 2020

@yordis yordis added State: Reviewing The authors have reached a general consensus on the proposal and the editors are now involved. and removed State: Draft The ADR is being discussed. labels Nov 21, 2020
@yordis
Copy link
Member Author

yordis commented Nov 21, 2020

Moved the ADR to Reviewing

@tleef
Copy link
Contributor

tleef commented Nov 21, 2020

I found a decent workaround for CRA which I think satisfies the spirit of this ADR.

The problem is CRA doesn't allow you to define path aliases. However, we are able to define a baseUrl in our tsconfig.json See here

We can use the baseUrl to achieve the goal of importing our application files like the following example

import { Header } from "@/components/Header";

CRA sets up the directory structure for you and requires that you have a directory named src with an index.tsx file and a react-app-env.d.ts file (assuming you are using the TypeScript template) It also comes with a predefined tsconfig.json

We can satisfy these requirements with minimal changes to the generated directory structure and tsconfig.json while still being able to import our app modules using the above style by setting our "baseUrl": "./src" in our tsconfig.json and creating a @ directory inside of src

The resulting directory structure would be as follows

tree . -L 2
├── tsconfig.json
├── src
│   ├── index.tsx
│   ├── react-app-env.d.ts
│   ├── @
│   │   ├── components/...

and our tsconfig.json will look like this (or similar)

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "baseUrl": "./src"
  },
  "include": [
    "src"
  ]
}

@yordis yordis added State: Approved The ADR has been agreed upon. and removed State: Reviewing The authors have reached a general consensus on the proposal and the editors are now involved. labels Nov 22, 2020
@yordis yordis merged commit 8e6b16e into master Nov 22, 2020
@yordis yordis deleted the yordis/jsconfig-paths branch November 22, 2020 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
State: Approved The ADR has been agreed upon. Type: ADR It is something related to ADRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants