-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
chore: add nx #11461
chore: add nx #11461
Conversation
} | ||
} | ||
}, | ||
"namedInputs": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See description for how inputs
and namedInputs
can help local dev and CI experience.
"playground": [ | ||
"default", | ||
"{workspaceRoot}/playground/**/*", | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inputs
can include environment variables or the result of a shell script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it also include extra cli arguments? eg pnpm test-serve some-test-file-to-focus.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, extra args should be included already
} | ||
] | ||
}, | ||
"targetDefaults": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section configures tasks and their dependencies + inputs that affect their computation.
@@ -19,17 +19,17 @@ | |||
"lint": "eslint --cache .", | |||
"typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck", | |||
"test": "run-s test-unit test-serve test-build", | |||
"test-serve": "vitest run -c vitest.config.e2e.ts", | |||
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", | |||
"test-serve": "nx exec -- vitest run -c vitest.config.e2e.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nx exec
is used to wrap any package scripts that that should run through Nx. This way non of the local or CI scripts need to be updated to integrate with Nx.
For example, users continue to use pnpm run docs
not nx run docs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the nx cli prefix/modify log output of the original command?
does it allow passing additional args like focussing a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the nx exec
command executes whatever command you pass to it (including any flags). The output is rendered with the exact same colors and formatting. The only modification is that Nx adds a message at the end saying "replayed from cache" or something similar.
acbe9df
to
9b9d746
Compare
Thanks a lot for the PR and such a detailed explanation of the benefits @jaysoo! Two things I would like we evaluate:
Again, thanks so much for taking the time and send us such a careful evaluation of what it would mean to use Nx in Vite Core. |
I think it's great that Nx and Vite are working together and I'm appreciative of the efforts made here. We've setup a tool called Turbo (https://turbo.build/) for SvelteKit. It looks like Nx is very similar and in fact the config files look almost identical. I don't mean to be a downer here especially since I have no direct experience with Nx, but I have to be very honest that Turbo is the single most upsetting piece of technology I've ever dealt with and I can't wait for it to vanish from this earth. Its configuration is extremely brittle. It's a constant struggle that we've accidentally missed configuring some piece of input or output and Turbo returns a cached version of our job instead of executing it. Caching is one of the hard things in computer science and I've spent way more time dealing with getting it right than Turbo has ever saved me. While in some ways it's not fair to judge Nx based on my experience with Turbo, the similarities between the tools are so striking it's hard for me not to imagine that Nx would be prone to the same issues. I'm very very very strongly against this change. Kill it with fire. The extra layer of potential headaches is not worth the time savings in my opinion. I really do hate to leave such a negative comment on your first PR in this repo, but would feel remiss if I didn't share my experience. I do thank you for your efforts here and welcome you to the Vite community and hope we will see more PRs from you in the future |
I share Ben's sentiments, the complexity and risk of adding a caching layer outweigh any benefits it may bring. Even if we get beyond all this and it somehow always works and was low maintenance, it would still be doing extra things to solve problems that are not really problems in this repo and even if they become problems, we have other options to deal with them:
Ultimately it is even possible to not need build for some or all things. Either by using a typescript aware node loader or by having the source in .js files with types in jsdoc (this works great for sveltekit, but would be a huge undertaking for vite, don't expect that to happen anytime soon or at all, but it is possible) so we can get builds to plenty fast without needing a cache. Even the current build speeds havn't raised concerns to my knowledge.
Don't get me wrong, nx is an invaluable tool for larger monorepos and organizations that can make the most of it's featureset in a controlled environment, but for the vite monorepo, i don't think adding it would be beneficial right now. |
"test-docs", | ||
"docs-build" | ||
], | ||
"accessToken": "NjYwZGFlNWYtZGYyNS00MTIzLTkzYTEtYzAxZTZiYzUwYTVifHJlYWQtd3JpdGU=" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as mentioned in my general comment, this key must not provide write access to the cache or it would be far too dangerous to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. The accessToken that is committed to an open source repo should always be a read only token. I'm sure @jaysoo set this token to read only. You can use a read-write token in a .env
file that is not committed to the repo on trusted dev machines or only use the read-write token in the environment variables in CI.
Is there any form of data collection or tracking in the nx cli or nx cloud? |
No, the CLI does not have any telemetry. Nx Cloud collects historical data for scheduling purposes (average task runs are used to divvy out tasks to agents if Distributed Task Execution is enabled). |
Closing this PR since the improvement is not worth the effort at this point. We can revisit in the future, even for another repo. Always open to support. |
Thanks for taking the time to show us how Nx would work in Vite core @jaysoo. We really appreciate it. For others reading, we discussed with the team and we found the benefits compelling but decided to delay adopting another tool until CI and build times start to be an issue for us. We'll re-evaluate in the future. |
Description
This is an example PR of adding Nx to the Vite repo. Myself and Juri discussed with @patak-dev about whether this repo could benefit from Nx so I'm outlining the changes and benefits here.
Additional context
This main monorepo does not have many packages left, and
create-vite
I heard is also moving to a separate repo later. However, I can still see two main benefits for using Nx:Basically, it doesn't matter if this repo is a monorepo of not, Nx is used as a task runner and has sophisticated distributed caching support.
Task execution and caching through Nx unlocks many benefits, such as:
I'll go into the above points into more details next. This PR only provides basic configuration changes, but there are more optimizations that are possible if desired.
Easier method of configuring and debugging task dependencies
There are tasks that implicitly depend on other tasks.
e.g.
test -> vite:build
test-docs -> docs-build -> vite:build
test-build -> vite:build
Using Nx, we can explicitly define these dependencies in
nx.json
. There is a lot more control afforded here than through ad-hoc package scripts.e.g.
The above tells Nx that
test-unit
andtest-build
both depend onbuild
of their dependencies (the^
is a shorthand for dependencies).Nx provides task visualization through the
nx graph
command. Here are some examples.Build task graph:
Docs task graph:
Using the visualizations, you can debug issues where task dependencies are set up incorrectly -- maybe dependencies are missing, or shouldn't exist, the graph will show that.
Contributors to docs will have a better first experience
If a contributor only changes files in
docs/
folder, then running the following command should be sufficient, and should start the server as fast as possible (almost instantly).To achieve this experience, we configure
nx.json
to add inputs for the different tasks.default
andproduction
. Default inputs are all files underpackages/
folder, whereasproduction
inputs are files that affect the published npm package.docs
task to only be affected by files underdocs/
, and depend on thebuild
task.build
task to only be affected by production files (e.g. changes to spec files do not affect build).With the above configuration, contributors can get started with just two commands:
And since
build
is not affected by docs changes, the server should start almost immediately (~5s for my locally). The build cache is pulled from Nx Cloud so user never needs to build locally.See here for more info on inputs: https://nx.dev/more-concepts/customizing-inputs#customizing-inputs-and-named-inputs
Faster CI checks when updating docs
Similar to the previous section, when files in
docs/
are changed (and nothing else), then most of the CI checks do not need to run, or at least they can be cached.This means the time it takes to update docs and getting it to production should be decreased significantly.
Some examples of docs-only PRs:
docs
Faster CI checks for @vitejs/plugin-legacy fixes
For changes in only
@vitejs/plugin-legacy
package, there is no need to run tasks relating tovite
nor docs.test-serve
, andtest-build
are needed to run (both are ~10s)test-build
andtest-serve
, e.g. separate tests for legacy plugin vs main vite packages)Faster CI checks when pushing multiple times to the same PR
When tasks hve the correct inputs configured, then Nx will re-run only what's necessary.
For example, if developer pushes changes to production and spec files, the first run will need to re-build and re-test everything.
However, if a second push only changes a spec file, then builds and docs are skipped.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).