denoland / deno_emit

Transpile and bundle JavaScript and TypeScript under Deno and Deno Deploy
https://jsr.io/@deno/emit
MIT License
222 stars 23 forks source link

Support plugins #89

Closed NyanHelsing closed 1 year ago

NyanHelsing commented 1 year ago

swc supports plugins, so that the way the input is transformed can be customized.

Id like to be able to do this transform, for example this plugin can be compiled to wasm32-unknown-unknown and be passed as a plugin to swc-cli in node, and I'd prefer to call it from deno.:

use string_cache::Atom;
use swc_core::ecma::{
    ast::{ImportDecl, NamedExport, Program},
    transforms::testing::test,
    visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
};
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};

pub struct TransformVisitor;

impl VisitMut for TransformVisitor {
    // Implement necessary visit_mut_* methods for actual custom transform.
    // A comprehensive list of possible visitor methods can be found here:
    // https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html

    fn visit_mut_import_decl(&mut self, decl: &mut ImportDecl) {
        decl.visit_mut_children_with(self);
        decl.src = Box::new(
            Atom::from(decl.src.value.replace(".ts", ".js")).into()
        );
    }

    fn visit_mut_named_export(&mut self, decl: &mut NamedExport) {
        decl.visit_mut_children_with(self);
        match &decl.src {
            Some(src) => {
                decl.src = Some(Box::new(
                    Atom::from(src.value.replace(".ts", ".js")).into()
                ));
            },
            None => ()
        }
    }

}

It would also be very cool if deno looked for a .swcrc file in the directory the path points at and any parent directories up to and including the directory of the first deno.json found.

NyanHelsing commented 1 year ago

i think maybe it needs to be supported in deno_ast here: https://github.com/denoland/deno_ast/blob/570007eb9cb62b527c43a355dc41db3ccfb72af8/src/transpiling/mod.rs#L309-L341 I'm not seeing where i can add in a transform via config.

dsherret commented 1 year ago

This is out of scope for deno_emit. I'd recommend using swc directly.