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

Guidelines on implementing plugins? #470

Closed
cyjake opened this issue Nov 28, 2019 · 12 comments
Closed

Guidelines on implementing plugins? #470

cyjake opened this issue Nov 28, 2019 · 12 comments

Comments

@cyjake
Copy link
Contributor

cyjake commented Nov 28, 2019

Need to port https://www.npmjs.com/package/babel-plugin-import

A simple regex replacer might suffice but I'd like to learn the recommended way to implement plugins for swc.

@cyjake cyjake changed the title Guidelines on implemnting plugins? Guidelines on implementing plugins? Nov 28, 2019
@kdy1
Copy link
Member

kdy1 commented Nov 28, 2019

Currently plugin system is not ready yet.

I'm considering

1. Embed plugins in swc binary

This make deploying plugins easier, and allow me maintaining plugins on swc update.
(codes will live in a module like swc_ecma_transforms::plugin::xxx)

Also, this option allows me to update plugins when types of ast node is changed.

Note: I can write plugins. (It's an easy task to me)

2. dll / so based

Seems ideal option, but dependency hell might occur.

3. Custom binary built by user with additional transform

It means building a rust library / command line app which depends on swc. If you pick binary, deploying would not be that hard.

See: https://github.com/izelnakri/mber-rust

4. IPC-based plugin

protobuf work in this way, and it allows writing plugin in any language.

5. js plugin interface

I thought this is the best, but context switching and serializing native object is really slow.

6. DSL

There's some languages written in rust.
See: https://github.com/gluon-lang/gluon

7. njs form nginx

This seems attractive, but some investigation is required.


Which option do you prefer?


Edit: typo
Edit: njs

@kdy1 kdy1 mentioned this issue Nov 28, 2019
@kdy1
Copy link
Member

kdy1 commented Nov 28, 2019

Closing in favor of #471.

@kdy1 kdy1 closed this as completed Nov 28, 2019
@kdy1
Copy link
Member

kdy1 commented Nov 30, 2019

@cyjake
Javascript based plugin is almost done, and there's a basic example of using plugin.
See https://github.com/swc-project/node-swc/blob/d51369e43e590fa66ddc2db6ca39976cdd185ac9/__tests__/transform/plugin.js

kdy1 added a commit to swc-project/website that referenced this issue Nov 30, 2019
@kdy1
Copy link
Member

kdy1 commented Nov 30, 2019

@cyjake
Copy link
Contributor Author

cyjake commented Dec 2, 2019

Awesome! I'll try implementing babel-plugin-import this week

@matzeeable
Copy link

Hey @cyjake !

Are there any news about the plugin? :-)

@cyjake
Copy link
Contributor Author

cyjake commented Oct 6, 2021

@matzeeable @kdy1 Probably a bit too late for this experiment since the js api will be deprecated in swc v2, anyway here it goes https://github.com/cyjake/swc-plugin-import/blob/master/src/import.ts#L32

How do I replace one ImportDeclaration with multiple ones? For example,

import { debounce, throttle } from 'lodash';

should be replaced with,

import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';

I think I'll give it a try no matter the stable API is in JavaScript or in Rust. Trying to replace babel with swc in my own bundler called Porter in the foreseeable month.

@kdy1
Copy link
Member

kdy1 commented Oct 6, 2021

js api may not be deprecated.
It depends on #2175

If you are using rust, you can handle it in fold_module_items or visit_mut_module_items.

@cyjake
Copy link
Contributor Author

cyjake commented Oct 8, 2021

@kdy1 any example code of writing plugin in Rust? Need some template code to get started 😂

@kdy1
Copy link
Member

kdy1 commented Oct 8, 2021

@cyjake I documented about it at https://docs.rs/swc/0.65.0/swc/#cutomizing
I wrote documents for someone building a custom node binding, but plugin API will be almost identical.

@ankitchouhan1020
Copy link

If anyone is still looking for one, here is my implementation. swc-plugin-transform-import.

bestlucky0825 added a commit to bestlucky0825/swc-website that referenced this issue Jun 1, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 18, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants