feat: Add es-main compatibility to vite-node#4751
Merged
sheremet-va merged 1 commit intovitest-dev:mainfrom Dec 15, 2023
Merged
feat: Add es-main compatibility to vite-node#4751sheremet-va merged 1 commit intovitest-dev:mainfrom
sheremet-va merged 1 commit intovitest-dev:mainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
In ES modules, we can't use the traditional "require.main" method in node to figure out if the currently executing module is the main entry point for the node process. The es-main module provides a utility that lets us accomplish the same thing using import.main metadata that is available in ES modules. You can read more about this use case in the es-main module readme.
es-mainexpectsprocess.argv[1]to be the path to the script entry point to figure out if the module is the main one, but it expects this to be an absolute path because this is how node always formats the argument.vite-nodeis extremely close to being able to support this properly because when the--scriptargument is provided it does populate the script entry point path inprocess.argv[1], but unfortunately it provides it as a relative path which doesn't work withes-main. This PR makes a very small change to make theprocess.argv[1]value thatvite-nodesets when the--scriptargument to be an absolute path, matching the behavior of normal NodeJS execution.Without This Change
Using test.js:
Without this change the output of
node test.jsismain, but the output ofvite-node --script test.jsisnot main.With This Change
Using test.js:
With this change the output of
node test.jsismain, and the output ofvite-node --script test.jsismain.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.