Skip to content

Conversation

@akbarkz
Copy link
Contributor

@akbarkz akbarkz commented Jan 31, 2025

Done

In tsconfig.json:

  • Extend TS configuration provided by @canonical/typescript-config-react package as instructed here.
  • Only leave configs that are needed by our project.

QA

QA steps

  • Check that all CI actions succeed on this PR, including lint that utilizes TS compiler.

@webteam-app
Copy link

@akbarkz akbarkz force-pushed the chore/tsconfig_extend branch from d575a1a to 55495d5 Compare January 31, 2025 09:28
Copy link

@advl advl Jan 31, 2025

Choose a reason for hiding this comment

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

Curious which prop made this change necessary
cc @jmuzina

If this is standard can we capture it in our docs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@advl Yes, this change was caused by this error from TSC:

error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './main.js'?

There was an option of fixing by adding an extension to the file ./main.tsx and adding additional configuration to tsconfig.json allowImportingTsExtensions. Another option was the one I applied.

Copy link

@advl advl Jan 31, 2025

Choose a reason for hiding this comment

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

Can you try "import("./main.js")) and tell me if it works ?

This is due to two things :

  • Using NodeNext, ts expects paths to be fully specified (Also, NodeNext also can be used in the browser, we choose it because it's stricter than EsNext)
  • Without allowImportingTsExtensions the extension should be .js even if the file is .ts or .tsx (typescript is able to resolve that)

The mixture of the two implies that a best practice is to import "my/local/file.js" :)

Reference here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@advl The lint passes if I do it like that as well, although in the IDE it's highlighted with the error Module './main.js' was resolved to '/home/akbar/projects/canonical/websites-content-system/static/client/main.tsx', but '--jsx' is not set.. Shall we go with that then?

Copy link
Member

Choose a reason for hiding this comment

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

There may be something wrong with your IDE here. I cloned this PR and edited this file to use an import with main.js and was able to run yarn build, yarn test, and yarn lint with no errors. Maybe you need to clean-install dependencies?

/* eslint-disable testing-library/no-node-access */
import { act } from "react";

import { waitFor } from "@testing-library/react";

describe("main", () => {
  beforeAll(() => {
    const rootElement = document.createElement("div");
    rootElement.setAttribute("id", "root");
    document.body.appendChild(rootElement);
  });

  it("renders the app in the root element", async () => {
    await act(() => import("./main.js"));
    const container = document.getElementById("root") as HTMLElement;
    await waitFor(() => expect(container.querySelector(".l-application")).toBeInTheDocument());
  });
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@advl @jmuzina indeed, removing all deps and installing again did the trick! removed the dynamic import.

@akbarkz akbarkz force-pushed the chore/tsconfig_extend branch from 55495d5 to 2ac85ba Compare February 3, 2025 08:29
Copy link

@advl advl left a comment

Choose a reason for hiding this comment

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

lgtm

@akbarkz akbarkz merged commit 6e2ae7e into main Feb 4, 2025
7 checks passed
@akbarkz akbarkz deleted the chore/tsconfig_extend branch February 4, 2025 05:46
@github-actions
Copy link

github-actions bot commented Sep 5, 2025

🎉 This PR is included in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants