Skip to content

Commit d5b543d

Browse files
branchseerclaude
andauthored
feat: allow comments (JSONC) in vite-task.json (#139)
# Add support for JSONC comments in vite-task.json Use jsonc-parser to parse vite-task.json, enabling `//` line comments, `/* */` block comments, and trailing commas. Add comments to several test fixture files to exercise this. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5ea9649 commit d5b543d

File tree

10 files changed

+48
-28
lines changed

10 files changed

+48
-28
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fspy_test_utils = { path = "crates/fspy_test_utils" }
7575
futures = "0.3.31"
7676
futures-util = "0.3.31"
7777
insta = "1.44.3"
78+
jsonc-parser = { version = "0.29.0", features = ["serde"] }
7879
libc = "0.2.172"
7980
memmap2 = "0.9.7"
8081
monostate = "1.0.2"

crates/vite_task_bin/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ path = "src/main.rs"
1414
anyhow = { workspace = true }
1515
async-trait = { workspace = true }
1616
clap = { workspace = true, features = ["derive"] }
17+
jsonc-parser = { workspace = true }
18+
serde_json = { workspace = true }
1719
tokio = { workspace = true, features = ["full"] }
1820
vite_path = { workspace = true }
1921
vite_str = { workspace = true }

crates/vite_task_bin/src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,37 @@ impl vite_task::TaskSynthesizer<CustomTaskSubcommand> for TaskSynthesizer {
135135
}
136136
}
137137

138+
/// A `UserConfigLoader` implementation that only loads `vite-task.json`.
139+
///
140+
/// This is mainly for examples and testing as it does not require Node.js environment.
141+
#[derive(Default, Debug)]
142+
pub struct JsonUserConfigLoader(());
143+
144+
#[async_trait::async_trait(?Send)]
145+
impl vite_task::loader::UserConfigLoader for JsonUserConfigLoader {
146+
async fn load_user_config_file(
147+
&self,
148+
package_path: &AbsolutePath,
149+
) -> anyhow::Result<Option<vite_task::config::UserRunConfig>> {
150+
let config_path = package_path.join("vite-task.json");
151+
let config_content = match tokio::fs::read_to_string(&config_path).await {
152+
Ok(content) => content,
153+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
154+
return Ok(None);
155+
}
156+
Err(err) => return Err(err.into()),
157+
};
158+
let json_value = jsonc_parser::parse_to_serde_value(&config_content, &Default::default())?
159+
.unwrap_or_default();
160+
let user_config: vite_task::config::UserRunConfig = serde_json::from_value(json_value)?;
161+
Ok(Some(user_config))
162+
}
163+
}
164+
138165
#[derive(Default)]
139166
pub struct OwnedSessionCallbacks {
140167
task_synthesizer: TaskSynthesizer,
141-
user_config_loader: vite_task::loader::JsonUserConfigLoader,
168+
user_config_loader: JsonUserConfigLoader,
142169
}
143170

144171
impl OwnedSessionCallbacks {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
// Smoke test: enables caching for all package.json scripts.
23
"cacheScripts": true
34
}

crates/vite_task_graph/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ petgraph = { workspace = true }
1515
serde = { workspace = true, features = ["derive"] }
1616
serde_json = { workspace = true }
1717
thiserror = { workspace = true }
18-
tokio = { workspace = true, features = ["fs"] }
1918
vec1 = { workspace = true, features = ["smallvec-v1"] }
2019
vite_graph_ser = { workspace = true }
2120
vite_path = { workspace = true }
@@ -24,7 +23,6 @@ vite_workspace = { workspace = true }
2423

2524
[dev-dependencies]
2625
pretty_assertions = { workspace = true }
27-
tokio = { workspace = true, features = ["fs", "rt-multi-thread"] }
2826
ts-rs = { workspace = true }
2927
vite_path = { workspace = true, features = ["ts-rs"] }
3028
vite_str = { workspace = true, features = ["ts-rs"] }

crates/vite_task_graph/src/loader.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,3 @@ pub trait UserConfigLoader: Debug + Send + Sync {
1212
package_path: &AbsolutePath,
1313
) -> anyhow::Result<Option<UserRunConfig>>;
1414
}
15-
16-
/// A `UserConfigLoader` implementation that only loads `vite-task.json`.
17-
///
18-
/// This is mainly for examples and testing as it does not require Node.js environment.
19-
#[derive(Default, Debug)]
20-
pub struct JsonUserConfigLoader(());
21-
22-
#[async_trait::async_trait(?Send)]
23-
impl UserConfigLoader for JsonUserConfigLoader {
24-
async fn load_user_config_file(
25-
&self,
26-
package_path: &AbsolutePath,
27-
) -> anyhow::Result<Option<UserRunConfig>> {
28-
let config_path = package_path.join("vite-task.json");
29-
let config_content = match tokio::fs::read_to_string(&config_path).await {
30-
Ok(content) => content,
31-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
32-
return Ok(None);
33-
}
34-
Err(err) => return Err(err.into()),
35-
};
36-
let user_config: UserRunConfig = serde_json::from_str(&config_content)?;
37-
Ok(Some(user_config))
38-
}
39-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
// Enables caching for all package.json scripts in the workspace.
23
"cacheScripts": true
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
// Enables caching for all package.json scripts in the workspace.
23
"cacheScripts": true
34
}

crates/vite_task_plan/tests/plan_snapshots/fixtures/workspace-root-no-package-json/vite-task.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
// This workspace root has no package.json — only pnpm-workspace.yaml.
3+
// vite-task.json should still be loaded and applied.
24
"cacheScripts": true,
35
"tasks": {
46
"deploy": {

0 commit comments

Comments
 (0)