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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit file extensions imports and make TypeScript checks for them #4217

Conversation

aloisklink
Copy link
Member

馃搼 Summary

When using import ... from "file.js";, many ESM software requires having file-extensions in the imports (for example, Node.JS has this behaviour, see https://nodejs.org/api/esm.html#mandatory-file-extensions).

Currently, we don't have any tests for this, which means we've accidentally added this bug a few times before:

TypeScript will automatically check this for us if we use the moduleResolution: "nodenext" setting, see https://www.typescriptlang.org/docs/handbook/esm-node.html

馃搹 Design Decisions

Issues

Only works with TypeScript 5.0 RC

This only works when using TypeScript v5 allowImportingTsExtensions: true setting. This is because Vite only supports loading TypeScript files using import * from "file.ts"; (i.e. with .ts extension), but TypeScript v4 only allows loading TypeScript files with the .js extension.

However, TypeScript v5 is not out yet, (only a release candidate is out). Because of that, VS Code and other editors throw TypeScript errors (unless you enable experimental settings).

We may want to avoid merging this PR until TypeScript v5 is officially out (should be out on March 14th 2023).

TypeScript doesn't check .js files

TypeScript currently doesn't check .js files, so it won't check for the correct import extensions in those files.

We could add it by setting checkJs: true in the tsconfig.json file, but that will probably throw up a lot of other TypeScript errors.

// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */

Alternatively, we could also use the import/extensions rule from eslint-plugin-import only on JS files.

Alternative PR

@remcohaszing has made a similar PR (see #4213) before I did, but has also tried to get it working with Cypress types. Their PR also uses TypeScript v4.9.

Since they made their PR first, I'll rather give priority to merge that PR first, if they get E2E tests working.

馃搵 Tasks

Make sure you

  • 馃摉 have read the contribution guidelines
  • 馃捇 have added unit/e2e tests (if appropriate)
  • 馃摀 have added documentation (if appropriate)
  • 馃敄 targeted develop branch

ESM and Node.JS requires that all relative imports have a file
extension. For example, `import x from "abc";` is invalid, as
you instead need to do `import x from "abc.js";`.

Vite allows it, but it causes issues with other bundlers/software
for people that use Mermaid.
Using `moduleResolution: "nodenext"` is the new recommended way of
using TypeScript,
see https://www.typescriptlang.org/tsconfig#moduleResolution
Move the `--emitDeclarationOnly` away from the `tsc` CLI,
and into the tsconfig.json file.
TypeScript version 5.0 added supported for importing files with a `.ts`
extension, which is what Vite requires in order to import TypeScript
files.
Vite doesn't allow importing TypeScript files with
`import xx from "xx.js";`. Instead, you must import the files
with a `.ts` extension, e.g. `import xx from "xx.ts";`.

Because this relies on the new `--allowImportingTsExtensions` option
in TypeScript v5.0, most IDEs currently struggle to handle this.
Hopefully, this will be fixed once TypeScript v5.0 is officially
released!
@aloisklink aloisklink mentioned this pull request Mar 12, 2023
4 tasks
@aloisklink
Copy link
Member Author

It looks like the allowImportingTsExtensions: true setting doesn't rewrite the .ts imports, see #4213 (comment).

This is fine in the generated JavaScript files, because Mermaid uses Vite to transpile TypeScript files.

However, the generated .d.ts files still have the .ts extension. This seems to be a pretty major problem, since it breaks the type definitions for downstream users, unless they switch to TypeScript v5.0 too, and that would be a breaking change.

Because of that, I'll close this PR. We can consider re-opening it if we ever decide to make a Mermaid v11 release. Or if a future TypeScript release converts these extensions in the .d.ts file.

@aloisklink aloisklink closed this Mar 15, 2023
@aloisklink aloisklink deleted the refactor/add-explicit-file-extensions branch May 7, 2023 17:10
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.

None yet

1 participant