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

vite: Add plugin for Vite #33

Closed
zoontek opened this issue Feb 16, 2022 · 13 comments · Fixed by #41
Closed

vite: Add plugin for Vite #33

zoontek opened this issue Feb 16, 2022 · 13 comments · Fixed by #41
Labels
💅 enhancement New feature or request

Comments

@zoontek
Copy link

zoontek commented Feb 16, 2022

Hello 👋

This project seems very promising! Do you have plan to support Vite with a custom plugin?

Documentation: https://vitejs.dev/guide/api-plugin.html
vanilla-extract plugin (for reference): https://github.com/seek-oss/vanilla-extract/tree/master/packages/vite-plugin

@layershifter layershifter added the 💅 enhancement New feature or request label Feb 16, 2022
@layershifter layershifter changed the title Vite support Add plugin for Vite Feb 16, 2022
@layershifter
Copy link
Member

Hey, yes we plan to have more integrations with bundlers and frameworks. Having Vite plugin is something that we want.

The implementation should be similar to the existing Webpack loader: call Babel and reuse bundler's module resolution. I haven't checked Vite docs yet and don't know what APIs are exposed, but it should it be doable.

@zoontek
Copy link
Author

zoontek commented Feb 16, 2022

Yes, it's doable! @vitejs/plugin-vue-jsx does exactly that: https://github.com/vitejs/vite/tree/main/packages/plugin-vue-jsx

@layershifter
Copy link
Member

The closest example/reference for us is @linaria/rollup, but it has the problem that I mentioned (callstack/linaria#888). In fact, we need something similar in Rollup/Vite:

// ⚠ "this._compilation" limits loaders compatibility, however there seems to be no other way to access Webpack's
// resolver.
// There is this.resolve(), but it's asynchronous. Another option is to read the webpack.config.js, but it won't work
// for programmatic usage. This API is used by many loaders/plugins, so hope we're safe for a while
const resolveOptionsFromWebpackConfig: webpack.ResolveOptions = this._compilation?.options.resolve || {};
const resolveSync = enhancedResolve.create.sync({
...resolveOptionsDefaults,
alias: resolveOptionsFromWebpackConfig.alias,
modules: resolveOptionsFromWebpackConfig.modules,
plugins: resolveOptionsFromWebpackConfig.plugins,
});

@layershifter
Copy link
Member

Okay, it seems that this.resolve() in Rollup is close to what we need 😸

@layershifter
Copy link
Member

Okay, it seems that this.resolve() in Rollup is close to what we need 😸

FYI: I've made some progress on it and it almost works.

The problem is that in Module._resolveFilename we should have only sync code (it's used in require() overrides) while this.resolve() in Rollup is async 💣

Module._resolveFilename = (id, { filename }) => {
const resolvedPath = resolveSync(path.dirname(filename), id);

I will think how we can workaround this 🤔

@zoontek
Copy link
Author

zoontek commented Feb 20, 2022

I will check when I have some time 🙂

@layershifter
Copy link
Member

layershifter commented Feb 20, 2022

I will check when I have some time 🙂

@zoontek thanks. FYI: I will cleanup my branch and will post as a draft PR on the next week.

EDIT: PR is posted #41, packages/vite-plugin/src/vitePlugin.ts contains a comment about problematic place:

image

@layershifter
Copy link
Member

There is some work in Linaria that could help with that, callstack/linaria#1001. Looking forward to it 🐱

@layershifter layershifter changed the title Add plugin for Vite vite: Add plugin for Vite Jul 22, 2022
@SahilTara
Copy link

Just wanted to bump on this issue since the linaria pr linked seems to have been merged.

@layershifter
Copy link
Member

@SahilTara indeed, but we will need to move to Linaria v4 first (#184).

@vamsiampolu
Copy link

Any update on the vite plugin?

@layershifter
Copy link
Member

Any update on the vite plugin?

As wyw-in-js is out, I can rollout the plugin finally 🎉

I reworked original PR (#41) and it can be shipped once the issue will be fixed (Anber/wyw-in-js#27). As @Anber will be offline for some time, I expect that we will be able to ship it after NY.

@layershifter
Copy link
Member

FYI #41 was merged and plugin will be available on NPM today. It does not support CSS extraction, yet (#494).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💅 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants