Skip to content

Commit

Permalink
[swc] Support auto import extends
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Nov 30, 2024
1 parent 8a012b0 commit ca292c6
Show file tree
Hide file tree
Showing 23 changed files with 404 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fixture
fixture
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc-plugin-gem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ lto = true
node-resolve = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
swc_core = { workspace = true, features = ["ecma_plugin_transform"] }
swc_core = { workspace = true, features = ["ecma_quote", "ecma_plugin_transform"] }
swc_ecma_visit = { workspace = true }
swc_common = { workspace = true, features = ["concurrent"] }
swc_ecma_ast = { workspace = true }
Expand Down
28 changes: 28 additions & 0 deletions crates/swc-plugin-gem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,31 @@
- support [memo getter](https://github.com/tc39/proposal-decorators/issues/509#issuecomment-2226967170)
- support minify style
- resolve full path (for esm)

# Example

```json
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"target": "es2024",
"parser": { "syntax": "typescript" },
"experimental": {
"plugins": [
[
"swc_plugin_gem",
{
"autoImport": {
"extends": "gem",
"members": {
"test": ["test"]
}
},
"autoImportDts": true
}
]
]
}
}
}
```
2 changes: 1 addition & 1 deletion crates/swc-plugin-gem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swc-plugin-gem",
"version": "0.1.4",
"version": "0.1.5",
"description": "swc plugin for Gem",
"keywords": [
"swc-plugin",
Expand Down
41 changes: 22 additions & 19 deletions crates/swc-plugin-gem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ use swc_core::ecma::visit::VisitMutWith;
use swc_core::plugin::metadata::TransformPluginMetadataContextKind;
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
use swc_ecma_ast::Program;
use visitors::import::gen_once_dts;
pub use visitors::import::import_transform;
pub use visitors::import::{import_transform, AutoImport};
pub use visitors::memo::memo_transform;
pub use visitors::minify::minify_transform;
pub use visitors::path::path_transform;
pub use visitors::preload::preload_transform;

mod visitors;

#[derive(Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default, rename_all = "camelCase")]
struct PluginConfig {
#[serde(default)]
pub style_minify: bool,
#[serde(default)]
pub auto_import: bool,
#[serde(default)]
/// 在安装时会尝试读取 .swcrc 生成,有些项目没有 .swcrc 文件,需要在正式变异时生成
pub auto_import: AutoImport,
/// Write into the src directory
pub auto_import_dts: bool,
#[serde(default)]
/// 配合 import map 直接使用 esm
/// Use esm directly with import map
pub resolve_path: bool,
#[serde(default)]
///depend on URL loader, top await
pub preload: bool,
/// un-implement
pub hmr: bool,
/// un-implement
pub lazy_view: bool,
}

#[plugin_transform]
Expand All @@ -39,20 +39,19 @@ pub fn process_transform(mut program: Program, data: TransformPluginProgramMetad

let filename = data.get_context(&TransformPluginMetadataContextKind::Filename);

// 执行在每个文件
if config.auto_import_dts {
gen_once_dts();
}

program.visit_mut_with(&mut (
Optional {
// 只支持原生装饰器或 `runPluginFirst`,不然被转译了,改写不了
enabled: true,
visitor: memo_transform(),
},
Optional {
enabled: config.auto_import,
visitor: import_transform(),
enabled: match config.auto_import {
AutoImport::Gem(enabeld) => enabeld,
AutoImport::Custom(_) => true,
},
// 执行在每个文件
visitor: import_transform(config.auto_import, config.auto_import_dts),
},
Optional {
enabled: config.style_minify,
Expand All @@ -62,6 +61,10 @@ pub fn process_transform(mut program: Program, data: TransformPluginProgramMetad
enabled: config.resolve_path,
visitor: path_transform(filename.clone()),
},
Optional {
enabled: config.preload,
visitor: preload_transform(),
},
));

program
Expand All @@ -77,7 +80,7 @@ mod tests {
assert_eq!(
config,
PluginConfig {
auto_import: true,
auto_import: AutoImport::Gem(true),
..Default::default()
}
)
Expand Down
Loading

0 comments on commit ca292c6

Please sign in to comment.