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

feat: bypass transformation in browser and vitest #158

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

antfu
Copy link
Member

@antfu antfu commented Aug 16, 2023

Combining with unjs/unbuild#300, it would allow unbuild --stub's output compatible with the browser and Vitest, by opting-out jiti transform on them and forward to Vite's transformation.

Playground: https://stackblitz.com/~/github.com/antfu/testing-jiti-on-vite

@pi0
Copy link
Member

pi0 commented Aug 16, 2023

(drafting until we support jiti.import to avoid breaking changes for current jiti users)

@antfu
Copy link
Member Author

antfu commented Oct 2, 2023

@pi0 Implemented the async jiti.import (eddf1f5) and let unbuild's mjs stub using it (unjs/unbuild@f3d5a56) for vite/vitest env. WDYT?

@pi0
Copy link
Member

pi0 commented Oct 2, 2023

Seems good start only would you mind separating PR for jiti.import?

@pi0 pi0 marked this pull request as ready for review October 2, 2023 10:03
lib/index.mjs Outdated
typeof document !== "undefined" &&
typeof navigator !== "undefined";
const isVitest =
typeof process !== "undefined" && process.env && process.env.VITEST;
Copy link
Member

Choose a reason for hiding this comment

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

We can use std-env also would isTest be enough to make implementation generic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure about other testing env, but generally we do this cause Vitest is based on Vite, let me test a bit with Jest at least. Maybe also for Bun?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. In general, if we can do it runtime-agnostic even if aiming for vitest for now, i would prefer that. If vite/vitest has special behavior, would be happy to also add specific condition (ideally flag from std-env) -- if we have to.

Copy link
Member Author

@antfu antfu Oct 2, 2023

Choose a reason for hiding this comment

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

Jest seems to never pick up the ESM entry, so yeah it should be fine I guess? Updated.

lib/index.mjs Outdated
};

try {
createRequire = await import("module").then((m) => m.createRequire);
Copy link
Member

@pi0 pi0 Oct 2, 2023

Choose a reason for hiding this comment

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

Suggested change
createRequire = await import("module").then((m) => m.createRequire);
createRequire = await import("node:module").then((m) => m.createRequire);

We can also add fallback in .catch

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by .cache?

Copy link
Member

Choose a reason for hiding this comment

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

I meaning chaining promise with catch! Will add code refactor.


// In browsers, or in Vitest,
// we bypass jiti transform and fallback to the native import and let the runtime handles it
if (isBrowser || isTest) {
Copy link
Member

Choose a reason for hiding this comment

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

We cannot always assume this for browser envs. In an environment like jsdom, isBrowser can be true but also jiti could be working.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah true, but I am not sure if there is a good way to distinguish jsdom and real browser. Any ideas?

package.json Show resolved Hide resolved
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.

None yet

2 participants