Make core transform isomorphic to run in plugin context also
Describe the feature
There are some possible needs for the plugin who want to visit some state of AST after a certain transform phase completes.
In the case of custom transform (via before_custom_pass) it is possible to run any existing core transform visitor as needed prior to run custom pass, then run swc core's transform. It is probably not efficient in some aspects, but it is allowed. However, some visitors may not be allowed to chain certain visitor in the plugin context. Notably some rely on globals, handlers, and other few more plugin sdk provides proxy instead of host.
Ideally, we'd like to provide identical experiences between plugin / custom pass. We may need to update few core transform visitors, mostly under _compat to use proper context to allow this. Probably this is ad-hoc bases as someone need to use it, instead of trying to make everything at once.
In case of babel since context is always same under node.js, plugin author can freely duck-typing like
//plugin
const firstPass = transform(code, { plugins: [someBabelCorePlugins]);
// actual visitor
firstPass.visit()
In our case it's not possible for the plugin.
Babel plugin or link to the feature description
No response
Additional context
No response
I think it's better to allow using swc_common::errors::HANDLER and related friends from plugin, rather than using proxy api from core transforms
Is this intended to support the case where e.g. I have a custom plugin which I want to run after transform of JSX to JS?
If that were possible, it would simplify some custom plugins a lot.
Is this intended to support the case where e.g. I have a custom plugin which I want to run after transform of JSX to JS?
No. This'll allow some core transforms run in plugins instead. The order of transform between core / plugin won't change regardless.
Ah I see. Thanks for clarifying. But is it the intent that a custom plugin can call an SWC transform pass itself (e.g. JSX to JS) before running it's own custom visitor? This is what I'd understood from "it is possible to run any existing core transform visitor as needed prior to run custom pass" - but maybe I'm misunderstanding?
custom plugin can call an SWC transform pass itself (e.g. JSX to JS) before running it's own custom visitor?
Yes, that's correct. But again, this doesn't mean anything aronud actual transform execution order between core / plugin and if core transform omits based on plugin's behavior or not. Certain plugin can try some core transform in its transform pass, but the execution order of plugin / core transform itself won't change.