Skip to content

Commit 2aaf27c

Browse files
authored
Stop if rollup can't find import that should be there (#1118)
1 parent 70f3b22 commit 2aaf27c

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

packages/cargo-pgml-components/Cargo.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cargo-pgml-components/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-pgml-components"
3-
version = "0.1.19"
3+
version = "0.1.20"
44
edition = "2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license = "MIT"
@@ -22,6 +22,7 @@ regex = "1"
2222
toml = "0.7"
2323
serde = { version = "1", features = ["derive"] }
2424
file-lock = "2"
25+
serde_json = "1"
2526

2627
[dev-dependencies]
2728
assert_cmd = "2"

packages/cargo-pgml-components/src/frontend/javascript.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! Javascript bundling.
22
33
use glob::glob;
4-
use std::collections::HashSet;
4+
use std::collections::{HashMap, HashSet};
55
use std::fs::{copy, read_to_string, remove_file, File};
66
use std::io::Write;
7-
use std::path::PathBuf;
7+
use std::path::{Path, PathBuf};
88
use std::process::{exit, Command};
99

1010
use convert_case::{Case, Casing};
11+
use serde::{Deserialize, Serialize};
1112

1213
use crate::config::Config;
1314
use crate::frontend::tools::execute_with_nvm;
@@ -32,6 +33,11 @@ static OLD_BUNLDES_GLOB: &'static str = "static/js/*.*.js";
3233
/// JS compiler
3334
static JS_COMPILER: &'static str = "rollup";
3435

36+
#[derive(Serialize, Deserialize, Debug)]
37+
struct Packages {
38+
dependencies: HashMap<String, String>,
39+
}
40+
3541
/// Delete old bundles we may have created.
3642
fn cleanup_old_bundles() {
3743
// Clean up old bundles
@@ -146,6 +152,16 @@ pub fn bundle(config: Config, minify: bool) {
146152
cleanup_old_bundles();
147153
assemble_modules(config.clone());
148154

155+
let package_json = Path::new("package.json");
156+
157+
let packages: Packages = if package_json.is_file() {
158+
let packages = unwrap_or_exit!(read_to_string(package_json));
159+
unwrap_or_exit!(serde_json::from_str(&packages))
160+
} else {
161+
warn("package.json not found, can't validate rollup output");
162+
serde_json::from_str(r#"{"dependencies": {}}"#).unwrap()
163+
};
164+
149165
let mut command = Command::new(JS_COMPILER);
150166

151167
command
@@ -163,7 +179,17 @@ pub fn bundle(config: Config, minify: bool) {
163179

164180
// Bundle JavaScript.
165181
info("bundling javascript with rollup");
166-
unwrap_or_exit!(execute_with_nvm(&mut command));
182+
let output = unwrap_or_exit!(execute_with_nvm(&mut command));
183+
184+
let lines = output.split("\n");
185+
for line in lines {
186+
for (package, _version) in &packages.dependencies {
187+
if line.contains(package) {
188+
error(&format!("unresolved import: {}", package));
189+
exit(1);
190+
}
191+
}
192+
}
167193

168194
info(&format!("written {}", JS_FILE));
169195

packages/cargo-pgml-components/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn execute_command(command: &mut Command) -> std::io::Result<String> {
6969
info!("{}", stdout);
7070
}
7171

72-
Ok(stdout)
72+
Ok(stdout.clone() + &stderr)
7373
}
7474

7575
pub fn write_to_file(path: &Path, content: &str) -> std::io::Result<()> {

0 commit comments

Comments
 (0)