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

POC: Limited AST for preTransforms in TypeScript #121

Closed
wants to merge 2 commits into from

Conversation

Zn4rK
Copy link

@Zn4rK Zn4rK commented Feb 17, 2023

Okay!

DO NOT MERGE.

With regards to my comment here, I've created this POC.

It took a bit longer time than I expected, since I wasn't very familiar with Rust or how to write SWC plugins before this.

A few things to note:

  1. It's only the swc/transform plugin that has been updated - I haven't touched babel yet.
  2. I've left a bunch of comments all over the place - these would of course be cleaned up.
  3. I haven't put any thought into where I've placed the TypeScript definitions. I just wanted to get it to work.
  4. I've only tested it with playground/next.
  5. It feels like the "forBuild"-idea is solid, but objects can have references as well, so we can probably simplify it if this idea is viable.
  6. The default options (packages/swc-plugin/src/transform.ts#L43) could be specified in the swc/babel plugin as defaults.

It's very rough, and messy at the moment. But due to the time difference from Sweden to Japan, I wanted to get it out there before you go to sleep.

The idea is that library authors might want to extend the functionality of the engine. This would allow for that.

I imagine an api like this:

function myPlugin() {
  return {
    transforms: [{
      from: {
        specifier: '__preCreateTheme',
        source: 'my-theming-library'
      },
      to: {
        specifier: '__createTheme',
        source: 'my-theming-library'
      }
    },
  };
}

const withKazeStyle = createKazeStylePlugin({
  swc: true, cssLayer: true, plugins: [
    myPlugin(),
  ]
});

Then my __preCreateTheme would create a serialisation that matches what needs to be done.

Again, super early draft of my idea. I haven't had much time to work on it in code. But I think it's pretty flushed out conceptually.

Please take a look @taishinaritomi :)

Edit:
I also think that with this concept, we could remove the preTransform -plugins from babel and swc and adapt the new one I've written, to do all the work for us. Maybe. Just a thought!

@taishinaritomi
Copy link
Owner

I feel very good about it.

Will review tomorrow.😎

@taishinaritomi
Copy link
Owner

taishinaritomi commented Feb 18, 2023

Hi @Zn4rK !
This feature is so great that I want to incorporate it into kaze-style. 🎉
The simple abstraction of AST is very nice.

There are a few areas of concern.

  • I would like to write the transform option like this.
const withKazeStyle = createKazeStylePlugin({
  swc: true,
  cssLayer: true,
  transforms: [{
      from: {
        specifier: '__preCreateTheme',
        source: 'my-theming-library'
      },
      to: {
        specifier: '__createTheme',
        source: 'my-theming-library'
      }
    }],
});

Thank you for suggesting the best POC.

How do you plan to work on this PR?

@Zn4rK
Copy link
Author

Zn4rK commented Feb 18, 2023

Nice!

Both of the changes you are suggesting make sense. I don't really know when I'll have proper time to work on this. Feel free if you have time before me. Next week will be very busy for me with work.

But I'm glad you like the idea! I'll play more with it today.

@taishinaritomi
Copy link
Owner

taishinaritomi commented Feb 19, 2023

Okay!

I will implement it, probably within the next week.
I would appreciate your feedback once it is completed.

@taishinaritomi taishinaritomi mentioned this pull request Feb 21, 2023
6 tasks
@taishinaritomi
Copy link
Owner

taishinaritomi commented Feb 22, 2023

Hi @Zn4rK !!

We have been working on the implementation of the new Compiler in #123 and have now completed the implementation of LimitedAST and the transforms option to some extent.
Please try it out.

We are now in the process of implementing #120.
However, I have little knowledge of Webpack childCompiler and am having difficulty implementing it.
If possible, I would like your help.

@taishinaritomi taishinaritomi marked this pull request as draft February 27, 2023 09:47
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

Successfully merging this pull request may close these issues.

2 participants