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

Switch module config in tsconfig.json #346

Merged
merged 2 commits into from
Mar 15, 2024

Conversation

karlhorky
Copy link
Member

@karlhorky karlhorky commented Mar 8, 2024

Switch "module" config in tsconfig.json:

  1. Switch "module": "ESNext" to "module": "Preserve" for bundler environments
  2. Switch "module": "ESNext" to "module": "NodeNext" for Node.js
  3. Switch "moduleResolution": "Bundler" to "moduleResolution": "NodeNext" for Node.js

Reasoning

  1. According to Matt Pocock, "module": "Preserve" more closely matches what bundlers do than "module": "ESNext":
  2. Also according to Matt Pocock, "module": "NodeNext" and "moduleResolution": "NodeNext" should be used if transpiling with tsc (which would be the case for Node.js projects)

@karlhorky karlhorky requested a review from ProchaLu March 8, 2024 14:53
@karlhorky
Copy link
Member Author

karlhorky commented Mar 8, 2024

@ProchaLu if you could test this on all of our supported project environments, then we can go ahead with this one:

  • Node.js
  • create-react-app
  • Next.js
  • Expo

@karlhorky karlhorky changed the title Switch module from ESNext to Preserve Switch module config in tsconfig.json Mar 8, 2024
@ProchaLu
Copy link
Member

ProchaLu commented Mar 15, 2024

It is important to update the TypeScript version to v.5.4.0 because setting "module": "Preserve" will fail if the TypeScript version is below v5.4.0.

Node.js

Updated the tsconfig.json files for the "Random Color Generator" and "Meme Scraper" projects. The application runs smoothly locally and when deployed, without encountering any issues.

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "eslint-config-upleveled/tsconfig.base.json",
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  },
  "include": [
    "**/*.ts",
    "**/*.tsx",
    "**/*.js",
    "**/*.jsx",
    "**/*.cjs",
    "**/*.mjs"
  ]
}

create-react-app

Updated the tsconfig.json file and all create-react-app student projects run without encountering issues.

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "eslint-config-upleveled/tsconfig.base.json",
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "target": "ES2015",
    "module": "Preserve",
    "moduleResolution": "Bundler",
    "moduleDetection": "force",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "downlevelIteration": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react-jsx",
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "skipLibCheck": true,
    "strict": true,
    "incremental": true,
    "noUncheckedIndexedAccess": true
  },
  "exclude": ["../../node_modules", "../../build"],
  "include": [
    "**/*.ts",
    "**/*.tsx",
    "**/*.js",
    "**/*.jsx",
    "**/*.cjs",
    "**/*.mjs"
  ]
}

Next.js

After updating the tsconfig.json file, and running the dev server, Next.js reconfigures the tsconfig.json file, and updates the module to esnext

   We detected TypeScript in your project and reconfigured your tsconfig.json file for you. Strict-mode is set to false by default.
   The following mandatory changes were made to your tsconfig.json:

        - module was set to esnext (for dynamic import() support)

Expo

Updated the tsconfig.json file and Expo projects ran without encountering issues.

@karlhorky karlhorky merged commit 4356179 into main Mar 15, 2024
3 checks passed
@karlhorky karlhorky deleted the switch-tsconfig-to-module-preserve branch March 15, 2024 12:33
@karlhorky
Copy link
Member Author

Looks like Next.js (next@14.2.0-canary.63) now supports "module": "Preserve" without changing unnecessary tsconfig.json options:

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