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

Fix/typescript-config #248

Merged
merged 14 commits into from
Oct 30, 2023
Merged

Conversation

vejja
Copy link
Collaborator

@vejja vejja commented Oct 17, 2023

This PR aims at improving the TypeScript configuration of the repo

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Currently, the TypeScript linter emits several warnings throughout the codebase.

  • We get warnings that the module "#imports" was not found, forcing us to use @ts-ignore overrides
  • We need to reimport types from far-fetched places (such as 'h3' or 'nitropack'), where they should be available
  • We need to import standard Nuxt utility functions (such as defineEventHandler from 'h3') from modules dependencies, i.e. that are not directly defined in our package.json
  • We sometimes need to cast types to 'unknown' before re-casting them to the right type, to suppress type warnings

Solution: The Nuxt core team has formulated recommendations on how to setup the TS configuration of Nuxt modules (https://nuxt.com/docs/guide/going-further/modules).

This involves
1- setting the root tsconfig file to extend to ./.nuxt instead of ./playground/.nuxt
2- setting autoImports to false in .nuxtrc
3- setting the playground tsconfig file to extend ./.nuxt
The configuration is detailed in the reference template for Nuxt3 modules at https://github.com/nuxt/starter/tree/module

In addition, we also need to provide complementary tsconfig files for the runtime/server and runtime/nitro folders, that extends tsconfig.server.json instead of tsconfig.json.
This is directly copied from the reference template for Nuxt3 apps at https://github.com/nuxt/starter/tree/v3

We further add a specific .nuxtrc file in the playground that allows autoImports in this folder only. This allows us to use classic Nuxt syntax (auto-imports) in the playground. This setup is replicated in fixture files for tests to run without modification.

Finally, the content of package.json is also upgraded to follow the Nuxt recommendations, specifically with regards to the npm run dev:prepare script, and to npm package versions.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes (if not applicable, please state why)

No changes are required to the documentation and the tests pass.

In order for the types to be correctly re-generated, it is required to run

npm upgrade
npm run dev:prepare

@vejja vejja marked this pull request as ready for review October 17, 2023 21:38
@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 1: upgrade TS config

Now that the TS config is compliant, we can
- remove @ts-ignore overrides
- import from "#imports" instead of importing from dependencies
@vercel
Copy link

vercel bot commented Oct 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuxt-security ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 28, 2023 8:48am

@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 2 : clean-up ts-ignore overrides and 'h3' imports in src/runtime

@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 3 : remove 'h3' imports from playground and fixtures

- no need to import NitroAppPlugin from 'nitropack'
- rename 'nitro' variable to 'nitroApp' for clarity
@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 4 : use standard approach in nitro plugins

@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 5 : type runtimeConfig via nuxt/schema interface
Allows proper type inference for security options, avoids unecessary type re-casts

@vejja
Copy link
Collaborator Author

vejja commented Oct 17, 2023

step 6 : this one improves how the lru-cache storage is mounted
Currently when build we have the following warning:

Build is done with some warnings:
- Inlined implicit external unstorage
- Inlined implicit external destr

Instead of inlining the config and mounting later in middleware, we explicitly mount it upfront via config in the nitro:config hook
This allows to later invoke the standard useStorage utility in the rateLimiter middleware

The warning is now suppressed

@Baroshem
Copy link
Owner

Hey,

I will make sure to merge it for 1.0.0-rc.2 :)

@Baroshem
Copy link
Owner

@vejja

I have included your latest fix for inlined implicit as it was blocking me from release. Thanks for that!

I think after the release of 1.0.0-rc.1 you could just revert the last commit and we will be able to merge the rest of your stuff :)

@Baroshem Baroshem added this to the 1.0.0-rc.2 milestone Oct 18, 2023
@Baroshem
Copy link
Owner

Update.

Let's make it ready for the 1.0.0-rc.3. I needed to push a fix to 1.0.0-rc.1 because there was a bug that I didn't catch during testing. It was triggered once released to NPM :(

@vejja
Copy link
Collaborator Author

vejja commented Oct 19, 2023

Update.

Let's make it ready for the 1.0.0-rc.3. I needed to push a fix to 1.0.0-rc.1 because there was a bug that I didn't catch during testing. It was triggered once released to NPM :(

Was the bug related to this PR? What is NPM complaining about?

@Baroshem
Copy link
Owner

No no! the bug was releated to Nuxt internal.

Basically, when I used the await import to import the vite plugin it was working perfectly until publishing a package to npm.

After publishing and trying to install the package, it was breaking the app and showing error that the plugin is not a function.

I changed the import from await to normal and it started working correctly. Pretty strange

@vejja vejja changed the base branch from chore/1.0.0-rc.1 to main October 19, 2023 07:37
- conform eslint to standard recommendation
- fix "template root requires one element" in secret.vue
- also conforms 'npm run dev' script setup
Copy link
Owner

@Baroshem Baroshem left a comment

Choose a reason for hiding this comment

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

Few suggestions from my side

package-lock.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated
"@nuxt/eslint-config": "^0.2.0",
"@types/node": "^18.18.1",
"eslint": "^8.50.0",
"nuxt": "^3.7.4",
Copy link
Owner

Choose a reason for hiding this comment

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

suggestion: Nuxt recently released 3.8 version. Maybe we could migrate to that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok

Copy link
Collaborator Author

@vejja vejja Oct 26, 2023

Choose a reason for hiding this comment

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

3.8 requires modification to type imports

See https://github.com/nuxt/nuxt/releases/tag/v3.8.0

Only minor modifications are required in our codebase, I will include these as well

@Baroshem Baroshem changed the base branch from main to chore/1.0.0-rc.3 October 25, 2023 08:53
@Baroshem
Copy link
Owner

@vejja could you please also resolve the conflict in 99-cspNonce file? It is probably caused by the other PR you created recently.

@vejja
Copy link
Collaborator Author

vejja commented Oct 26, 2023

@vejja could you please also resolve the conflict in 99-cspNonce file? It is probably caused by the other PR you created recently.

will do

@Baroshem
Copy link
Owner

Hey @vejja

Could you please adjust this PR? I have merged your previous one about fixing complaine of CSP and it resulted in conflicts here.

Also, could you try to fix the CI?

Here is the output of Vercel:

image

Thanks! 💚

@vejja
Copy link
Collaborator Author

vejja commented Oct 27, 2023

Sure - Would you mind sharing your Vercel config file so that I can reproduce on my end ?

Update: pls ignore, managed to reproduce on Vercel with standard setup

@vejja
Copy link
Collaborator Author

vejja commented Oct 27, 2023

Hi @Baroshem
Do you know when the Vercel regression happened in the first time ? (the Load plugin failed: pinceau/volar Error [ERR_REQUIRE_ESM]: require() of ES Module)
I have it on my end on the main branch, and I'm trying to revert to a point where it did not exist

Update: pls ignore, the build was not failing because of this error, but because defineAppConfig was not auto-imported by Nuxt

- add .nuxtrc autoImport directive in docs/
- enables building outside of root directory
@vejja
Copy link
Collaborator Author

vejja commented Oct 28, 2023

Quick update:

  • Merge conflicts resolved
  • Vercel build now passing

Build-time warnings in the vercel logs are not on our side, but on third-party packages (nuxt-themes/docus).
Unfortunately there is nothing we can do. However, aside from polluting the logs, they do not prevent building successfully so it's only a minor annoyance.

@Baroshem
Copy link
Owner

Thanks @vejja ! I will merge it to the 1.0.0-rc.3 branch and test it there.

@Baroshem Baroshem merged commit a667406 into Baroshem:chore/1.0.0-rc.3 Oct 30, 2023
3 checks passed
@Baroshem Baroshem mentioned this pull request Oct 30, 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.

2 participants