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

Special loader for vite(st) environments #150

Open
pi0 opened this issue Jul 18, 2023 · 6 comments
Open

Special loader for vite(st) environments #150

pi0 opened this issue Jul 18, 2023 · 6 comments

Comments

@pi0
Copy link
Member

pi0 commented Jul 18, 2023

While this is not a direct purpose of jiti, when it is used in testing environments (unbuild dist in stub mode) we might need to detect vite environment and bypass transforms.

@pi0
Copy link
Member Author

pi0 commented Jul 18, 2023

/cc @antfu If you have any ideas.

@antfu
Copy link
Member

antfu commented Jul 18, 2023

I image that we could have a magic comment in the stub file, and then with a Vite plugin to transform it back to a redirect. For example:

// @jiti-stub /path/to/source.ts

module.exports = jiti()('/path/to/source.ts')
export const jitiStubPlugin() {
	return {
      name: 'jiti:stub',
      transform(code, id) {
        if (!code.includes('@jiti-stub')) return
        const match = /REGEX/.match(code)
        return `export * from "${match[1]}"`
      }
    }
}

@pi0
Copy link
Member Author

pi0 commented Jul 18, 2023

Do you think would it be possible to magically making jiti+vite working together out of the box without custom user/framework plugins?

@antfu
Copy link
Member

antfu commented Jul 18, 2023

I can't think of one at this moment (without changes in Vite core, which is unlikely to happen)

@pi0
Copy link
Member Author

pi0 commented Jul 19, 2023

Can we detect vite environment in a reliable way or having an export condition by default in vite? This way i was thinking jiti instead can change it's internal behavior for vite(st) compatibility.

@pi0 pi0 changed the title Special loader for vite and vitest environments Special loader for vite(st) environments Jul 19, 2023
@justin-schroeder
Copy link

FWIW — Im able to get the tests to run fine in vitest using this little unplugin resolver i wrote (main issue was the file:// protocol doesnt seem to work in vite): https://github.com/justin-schroeder/unplugin-file-url

It works fine — the remaining issue is (in the context of a monorepo at least) making changes in stubbed packageA does not trigger vitest to re-run in the context of packageB/teststhat depend on the stubbedpackageA`

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

No branches or pull requests

3 participants