Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use opts.onBuildError to catch build errors #868

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ impl Context {

pub fn get_static_content<T: AsRef<str>>(&self, path: T) -> Option<Vec<u8>> {
let map = self.static_cache.read().unwrap();

map.read(path)
}
}
Expand Down Expand Up @@ -220,7 +219,9 @@ impl Compiler {
args: Args,
extra_plugins: Option<Vec<Arc<dyn Plugin>>>,
) -> Result<Self> {
assert!(root.is_absolute(), "root path must be absolute");
if !root.is_absolute() {
return Err(anyhow!("root path must be absolute"));
}

// why add plugins before builtin plugins?
// because plugins like less-loader need to be added before assets plugin
Expand Down Expand Up @@ -357,7 +358,7 @@ impl Compiler {
pub fn compile(&self) -> Result<()> {
// 先清空 dist 目录
if self.context.config.clean {
self.clean_dist();
self.clean_dist()?;
}

let t_compiler = Instant::now();
Expand Down Expand Up @@ -396,14 +397,13 @@ impl Compiler {
is_first_compile: true,
time: t_compiler.elapsed().as_millis() as u64,
stats: PluginGenerateStats {
start_time: start_time.duration_since(UNIX_EPOCH).unwrap().as_millis() as u64,
end_time: end_time.duration_since(UNIX_EPOCH).unwrap().as_millis() as u64,
start_time: start_time.duration_since(UNIX_EPOCH)?.as_millis() as u64,
end_time: end_time.duration_since(UNIX_EPOCH)?.as_millis() as u64,
},
};
self.context
.plugin_driver
.generate_end(&params, &self.context)
.unwrap();
.generate_end(&params, &self.context)?;
Ok(())
} else {
result
Expand All @@ -414,15 +414,15 @@ impl Compiler {
mako_core::mako_profile_function!();
let cg = self.context.chunk_graph.read().unwrap();
let mg = self.context.module_graph.read().unwrap();

cg.full_hash(&mg)
}

pub fn clean_dist(&self) {
pub fn clean_dist(&self) -> Result<()> {
// compiler 前清除 dist,如果后续 dev 环境不在 output_path 里,需要再补上 dev 的逻辑
let output_path = &self.context.config.output.path;
if fs::metadata(output_path).is_ok() {
fs::remove_dir_all(output_path).unwrap();
fs::remove_dir_all(output_path)?;
}
Ok(())
}
}
8 changes: 7 additions & 1 deletion packages/bundler-okam/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ exports.build = async function (opts) {
});
} catch (e) {
console.error(e.message);
opts.onBuildError?.(e);
const err = new Error('Build with mako failed.');
err.stack = null;
throw err;
Expand Down Expand Up @@ -208,6 +209,7 @@ exports.dev = async function (opts) {
watch: true,
});
} catch (e) {
opts.onBuildError?.(e);
console.error(e.message);
const err = new Error('Build with mako failed.');
err.stack = null;
Expand Down Expand Up @@ -424,7 +426,11 @@ async function getOkamConfig(opts) {
let makoConfig = {};
const makoConfigPath = path.join(opts.cwd, 'mako.config.json');
if (fs.existsSync(makoConfigPath)) {
makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8'));
try {
makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8'));
} catch (e) {
throw new Error(`Parse mako.config.json failed: ${e.message}`);
}
}

const {
Expand Down