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

Add support for @site/... imports in docusaurus.config.ts / builder #2

Open
SilasBerger opened this issue Jan 3, 2024 · 1 comment
Labels
tech Technical improvement

Comments

@SilasBerger
Copy link
Owner

@site/... imports work fine in React components and in .mdx files (thanks to intellij.config.js workaround for IntelliJ MDX plugin). However, they do not work in the docusaurus.config.ts and the whole builder part.

The @docusaurus/tsconfig file defines the @site path alias as follows:

"compilerOptions": {
  "paths": {
      "@site/*": ["./*"]
  },
}

It seems like that file doesn't apply to docusaurus.config.ts: Imports such as @site/src/builder/logger in docusaurus.config.ts are accepted (and even suggested) by the IDE, but during yarn run build, they yield the following error:

Cause: Cannot find module '@site/src/builder/logger'
Require stack:
- /Users/silas/repos/teaching-website/docusaurus.config.ts
    at loadFreshModule (/Users/silas/repos/teaching-website/node_modules/@docusaurus/utils/lib/moduleUtils.js:36:15)
    at loadSiteConfig (/Users/silas/repos/teaching-website/node_modules/@docusaurus/core/lib/server/config.js:35:62)
    ... 2 lines matching cause stack trace ...
    at async Command.start (/Users/silas/repos/teaching-website/node_modules/@docusaurus/core/lib/commands/start.js:44:19) {
  [cause]: Error: Cannot find module '@site/src/builder/logger'

Also note that the custom tsconfig.json file in the project root only applies for the IDE and is not used during compilation.

@SilasBerger SilasBerger changed the title Add support for @site/... imports in docusaurus.config.ts / builder Add support for @site/... imports in docusaurus.config.ts / builder Jan 3, 2024
@SilasBerger SilasBerger added the tech Technical improvement label Jan 3, 2024
@SilasBerger
Copy link
Owner Author

What we know so far

  • When auto-completing an import, IntelliJ always takes into account the tsconfig.json nearest to the current file. If that config defines an @site path alias, IntelliJ will create an @site import.
  • Docusaurus does not use the custom tsconfig.json file in compilation (https://docusaurus.io/docs/typescript-support#setup).
  • As far as I can tell, the @site alias works fine in all components, MDX pages, and generally in all the "web app" code. However, it doesn't work within the "CLI part": In the case of this project, this includes docusaurus.config.ts and its import tree.
  • It appears that Docusaurus uses Babel to transpile TypeScript (rather than tsc).
  • Points of interest for further research include Docusaurus' Babel and Webpack source.
  • The current "root" directory where @site imports don't work is src/framework, plus docusaurus.config.ts.
  • Here is an interesting discussion about what seems to be a similar issue: Support TypeScript in Docusaurus config facebook/docusaurus#7911 (comment).
  • Most mentions of the @site alias in the Docusaurus documentation suggests that it is only intended for use within the "MDX world":
  • Most likely, the best solution would be to fix the editor behavior, rather than the compiler settings. That is, prevent IntelliJ from using the @site alias when generating imports in files at the project rootor within the src/framework directory.
  • One possible solution is to disable the @site alias at root level by adding a compiler option "paths": {} to the root-level tsconfig.json, then adding a lower-level tsconfig.json with a compiler option "paths": {"@site/*": ["./*"]} to every directory where @site imports are allowed, such as src/app. The caveat is that overriding the paths option means having to explicitl re-specify every path alias that we still want to be able to use, since we only want to disable @site (where applicable).
  • With the workaround introduced in Rework directives and clean up src directory #28, this issue is now limited to docusaurus.config.ts. Within the src/framework directory, IntelliJ no longer auto-generates @site imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tech Technical improvement
Projects
None yet
Development

No branches or pull requests

1 participant