unjs / jiti

Runtime Typescript and ESM support for Node.js
MIT License
1.62k stars 57 forks source link

Special loader for vite(st) environments #150

Open pi0 opened 1 year ago

pi0 commented 1 year ago

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 commented 1 year ago

/cc @antfu If you have any ideas.

antfu commented 1 year ago

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 commented 1 year ago

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

antfu commented 1 year ago

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

pi0 commented 1 year ago

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.

justin-schroeder commented 5 months ago

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 ofpackageB/teststhat depend on the stubbedpackageA`

giiska commented 3 months ago

Any news on this?

ZaneL1u commented 3 months ago

Any news on this?

+1

lizyChy0329 commented 2 months ago

not work in vite yet