Skip to content

Commit 477bf82

Browse files
xusd320gemini-code-assist[bot]fireairforce
authored
feat(pack): add swc plugin example (#2709)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: zoomdong <1344492820@qq.com>
1 parent 2a81100 commit 477bf82

10 files changed

Lines changed: 11502 additions & 10998 deletions

File tree

crates/pack-core/src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub struct Config {
107107
react: Option<ReactConfig>,
108108
optimization: Option<OptimizationConfig>,
109109
stats: Option<bool>,
110+
#[bincode(with = "turbo_bincode::serde_self_describing")]
111+
swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>,
110112
#[cfg(any(feature = "process_pool", feature = "worker_pool"))]
111113
plugin_runtime_strategy: Option<PluginRuntimeStrategy>,
112114
persistent_caching: Option<bool>,
@@ -694,8 +696,6 @@ impl TryFrom<RegexComponents> for EsRegex {
694696
#[derive(Clone, Debug, Default, Deserialize, OperationValue)]
695697
#[serde(rename_all = "camelCase")]
696698
pub struct ExperimentalConfig {
697-
#[bincode(with = "turbo_bincode::serde_self_describing")]
698-
swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>,
699699
react_compiler: Option<ReactCompilerOptionsOrBoolean>,
700700
}
701701

@@ -1286,8 +1286,8 @@ impl Config {
12861286
}
12871287

12881288
#[turbo_tasks::function]
1289-
pub fn experimental_swc_plugins(&self) -> Vc<SwcPlugins> {
1290-
Vc::cell(self.experimental.swc_plugins.clone().unwrap_or_default())
1289+
pub fn swc_plugins(&self) -> Vc<SwcPlugins> {
1290+
Vc::cell(self.swc_plugins.clone().unwrap_or_default())
12911291
}
12921292

12931293
#[turbo_tasks::function]

crates/pack-core/src/shared/transforms/swc_ecma_transform_plugins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub async fn get_swc_ecma_transform_plugin_rule(
1313
config: Vc<Config>,
1414
project_path: FileSystemPath,
1515
) -> Result<Option<ModuleRule>> {
16-
let plugin_configs = config.experimental_swc_plugins().await?;
16+
let plugin_configs = config.swc_plugins().await?;
1717
if !plugin_configs.is_empty() {
1818
#[cfg(feature = "plugin")]
1919
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.turbopack
2+
node_modules
3+
dist

examples/with-swc-plugin/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## with-swc-plugin
2+
3+
An example from utoo, using swc plugin to transform the code.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>with-swc-plugin</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script src="./src/index.jsx"></script>
11+
</body>
12+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "with-swc-plugin",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"dev": "up dev",
6+
"build": "up build"
7+
},
8+
"dependencies": {
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0"
11+
},
12+
"devDependencies": {
13+
"autoprefixer": "10.4.10",
14+
"@types/react": "^18.2.0",
15+
"@types/react-dom": "^18.2.0",
16+
"swc-plugin-coverage-instrument": "^0.0.32",
17+
"@utoo/pack-cli": "*"
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
4+
function App() {
5+
return (
6+
<div className="text-3xl font-bold underline">
7+
Swc Plugin Example by Utoopack.
8+
</div>
9+
);
10+
}
11+
12+
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"$schema": "../../packages/pack/config_schema.json",
3+
"entry": [
4+
{
5+
"name": "index",
6+
"import": "./src/index.jsx",
7+
"html": {
8+
"template": "./index.html"
9+
}
10+
}
11+
],
12+
"output": {
13+
"clean": true
14+
},
15+
"optimization": {
16+
"minify": false
17+
},
18+
"swcPlugins": [
19+
[
20+
"swc-plugin-coverage-instrument",
21+
{}
22+
]
23+
]
24+
}

0 commit comments

Comments
 (0)