-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Support Farm Rust Plugin #28
Comments
This comment was marked as outdated.
This comment was marked as outdated.
What I immediately see looks like a huge limitation for implementing a native plugin: https://www.farmfe.org/docs/plugins/writing-plugins/rust-plugin#using-swc-in-plugin (BTW Amazing docs!) I am using
doesn't seem very promising. What global state do you mean? I know that I also can't really switch to using
https://www.farmfe.org/docs/plugins/writing-plugins/rust-plugin#choosing-rust-toolchain This is also a limitation for native plugins I was expecting tbh. Again, I can pin Farm version and manually synchronise the toolchain. Early thoughts: however tempting it seems to use a native plugin, no-one can really ignore DLL incompatibility. At the moment I am thinking of providing a stable Vite/unplugin version of the project with a potential Farm plugin later down the road. @wre232114 Please tell me if I am wrong and it is actually doable. |
First question:
Dead lock happens only when the plugin reuses the ast parsed by Farm and access There are 2 effective methods to avoid dead lock:
I prefer method 1, do not share ast with Farm, then Farm and Fervid are totally independent. Question 2:
As talked above, if you do not share ast with Farm, you can use any swc versions, it's independent. Question 3:
This limitation only applies to the Rust plugin, you can build Fervid with any rust toolchains. I think the architecture should be: Fervid compiler is toolchain independent, based on Fervid compiler, different toolchains can be used to build isolate products I think Farm Rust plugin is actually doable, and can greatly speedup Vue compilation, and the limitation is reasonable, @phoenix-ru what do you think |
@wre232114 Thank you for your detailed input. I will try the "don't share AST" at first, and then maybe go further with a shared AST, this is a good tip. Didn't know I can have an independent toolchain. Could you please advise if Farm plugin needs to be in its own repository or it can be a part of a monorepo?
Yes, you are absolutely right. Fervid already supports true multithreading in Napi and it's significantly faster than Overall, I think I can start with a plugin very soon |
Farm plugin can be a part of a monorepo, the
Do you mean Fervid can enable multi-threading itself? Farm enables maximum parallel for multiple files too. Looking forward to cooperate on the full new Rust vue plugin, it would be a start of a full new future of vue projects compilation! |
Not Fervid itself, but rather Node.js with libuv: fervid/crates/fervid_napi/src/lib.rs Lines 33 to 44 in 676293e
And the bench result: https://github.com/phoenix-ru/fervid?tab=readme-ov-file#is-it-fast
Me as well, I think native tooling would make Vue and Nuxt way more appealing :) |
@wre232114 Looks very promising from half an hour of experimentation: Thank you for making the setup quite easy! I would still play around more, can you point me at how to emit virtual modules in Farm? I basically want to use something like context.emit_file(EmitFileParams {
resolved_path: "virtual:some-file.css".into(),
name: "virtual:some-file.css".into(),
content: ".bg-red { background: red }".into(),
resource_type: farmfe_core::resource::ResourceType::Css,
}); Is this correct? |
No, For virtual modules, you use use // resolve
fn resolve(self, param) -> .. {
// support resolve virtual module
if param.source == "virtual:some-file.css" {
return "virtual:some-file.css"
}
}
// load
fn load(self, param) -> .. {
if param.resolvedPath == "virtual:some-file.css" {
// return the virtual module code
let code = self.descriptors.get("virtual:some-file").cssCode;
return code;
}
} |
I think the js vite plugin vue is a good example for implement a vue plugin: https://github.com/vitejs/vite-plugin-vue/blob/main/packages/plugin-vue/src/index.ts#L224 |
BTW, is there a repository of the rust vue plugin, we would like to contribute if we have time |
Thank you for clarifying it. It would be of great help if the rust code was documented so that plugin authors don't have to guess. I saw that you have a far bigger API than mentioned in the docs.
This is helpful, however, how do I save one? I tried attaching it to a hash map Another point I found is —
It is currently in the same repo. I can commit my raw findings if you want. |
Thank you for the suggestions and PR welcome! You are right, the writing rust plugins guide is just a small start of writing plugins, more APIS need to be documented.
We uses pub struct FarmPluginCss {
css_modules_paths: Vec<Regex>,
// cache and share css ast
ast_map: Mutex<HashMap<String, (Stylesheet, CommentsMetaData)>>,
content_map: Mutex<HashMap<String, String>>,
sourcemap_map: Mutex<HashMap<String, String>>,
} In this case, we should release the lock as quick as possible to avoid potential thread block.
Yes, all hooks must be synchronous, thanks for the advise, I think we can support async hook in next major version(2.0). reading local files is not bottleneck, but for loading remote modules, that would block the thread and affect performance. I think it's ok for now for sync hooks |
@wre232114 Keeping you updated, I have committed a very basic working Farm example integration here: https://github.com/phoenix-ru/fervid/tree/master/node/examples/farm And plugin code here: |
Awesome! Can not wait to use rust vue plugin in production! I will add official farm fervid template as soon as the plugin is feature complete |
Farm is now 1.0 stable, the Rust plugin(https://www.farmfe.org/docs/plugins/writing-plugins/rust-plugin) are available for community plugin developers too.
We want to develop a Rust vue plugin based on this project, can we have a collaboration. Relative issue: farm-fe/farm#125
The text was updated successfully, but these errors were encountered: