Skip to content

Add nodeNext and node16 support#147

Closed
cuppachino wants to merge 10 commits intogvergnaud:mainfrom
cuppachino:node-next
Closed

Add nodeNext and node16 support#147
cuppachino wants to merge 10 commits intogvergnaud:mainfrom
cuppachino:node-next

Conversation

@cuppachino
Copy link
Copy Markdown

@cuppachino cuppachino commented Feb 19, 2023

This branch potentially fixes #110. I used tsup to quickly try out a few common configs, but this change needs more testing.

changes

jest.config.cjs Transform hard-coded .js to .ts during testing.
package.json Expose .d.ts exports & use tsconfig.prod.json in scripts
src/**/*.(ts|mts|cts) regexp workaround. append .js to imports
tsconfig.json enforce import paths end with .js during development
tsconfig.prod.json extends dev config with microbundles' recommended settings

@cuppachino
Copy link
Copy Markdown
Author

there might be working solution now with bundler module resolution.

@gvergnaud
Copy link
Copy Markdown
Owner

@cuppachino I didn't look at the new bundler resolution mode very closely, is there something we should do on the ts-pattern side, or does this new mode fix this issue for us?

@cuppachino
Copy link
Copy Markdown
Author

cuppachino commented Mar 23, 2023

@gvergnaud I just checked the PR for it, and I think if we want to hit both module systems for both browser and node we have to use node16 / nodeNext.

From a bunch of experimentation, this seems to be the easiest way to do it:

Use esm by default

  • set package.json type to "module".
  • use .ts extension for source files.
  • if you need a commonjs source file, use the .cts extension.
  • import complete paths, relative to the file's dist location, specifying .js / .cjs / .mjs extensions.

tsconfig.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "outDir": "./dist/esm"
  }
}

tsconfig.cjs.json

{
    "compilerOptions": {
      "target": "ES2015",
      "module": "CommonJS",
      "moduleResolution": "nodenext",
      "outDir": "./dist/cjs"
    },
    "extends": "./tsconfig.base.json"
}
  • build to dist/esm/ using tsconfig.json
  • build to dist/cjs/ using tsconfig.cjs.json
    • run a script to replace dist/cjs/*.js with .cjs extensions.
    • (maybe not necessary) copy an index.d.ts from one of the output folders to the root of dist/
  • configure package.json exports:
{
  "type": "module",
  "main": "./dist/cjs/index.js",
  "module": "./dist/esm/index.js",
  "exports": {
    "types": "./dist/index.d.ts",
    "import": "./dist/esm/index.js",
    "require": "./dist/cjs/index.cjs",
    "browser": "./dist/esm/index.js",
    "default": "./dist/cjs/index.cjs"
  }
}

@cuppachino
Copy link
Copy Markdown
Author

Just tried this out in another popular project with success. Here's the conversion commit.

I'd suggest using microbundle on JavaScript after compilation with tsc (+ the steps previously mentioned) or not at all.

openapi-typescript-fetch #41

@gvergnaud
Copy link
Copy Markdown
Owner

gvergnaud commented May 8, 2023

Thanks for opening this PR! Closing in favor of #160

@gvergnaud gvergnaud closed this May 8, 2023
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.

There is a compatibility issue with "moduleResolution": "NodeNext" in TS

2 participants