Skip to content

Commit

Permalink
refact: some confusing types and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Jul 10, 2024
1 parent 9027639 commit 0159d86
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 188 deletions.
12 changes: 4 additions & 8 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Plugin for JsPlugin {
"js_plugin"
}

fn build_start(&self, _context: &Arc<Context>) -> Result<Option<()>> {
fn build_start(&self, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.build_start {
let (tx, rx) = mpsc::channel::<napi::Result<()>>();
hook.call(
Expand All @@ -26,7 +26,7 @@ impl Plugin for JsPlugin {
rx.recv()
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?;
}
Ok(None)
Ok(())
}

fn load(&self, param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> {
Expand Down Expand Up @@ -64,11 +64,7 @@ impl Plugin for JsPlugin {
Ok(None)
}

fn generate_end(
&self,
param: &PluginGenerateEndParams,
_context: &Arc<Context>,
) -> Result<Option<()>> {
fn generate_end(&self, param: &PluginGenerateEndParams, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.generate_end {
let (tx, rx) = mpsc::channel::<napi::Result<()>>();
hook.call(
Expand All @@ -81,7 +77,7 @@ impl Plugin for JsPlugin {
rx.recv()
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?;
}
Ok(None)
Ok(())
}

fn before_write_fs(&self, path: &std::path::Path, content: &[u8]) -> Result<()> {
Expand Down
5 changes: 4 additions & 1 deletion crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ impl Compiler {
}

if config.output.mode == OutputMode::Bundless {
plugins.insert(0, Arc::new(plugins::bundless_compiler::BundlessCompiler {}));
plugins.insert(
0,
Arc::new(plugins::bundless_compiler::BundlessCompilerPlugin {}),
);
}

if std::env::var("DEBUG_GRAPH").is_ok_and(|v| v == "true") {
Expand Down
7 changes: 3 additions & 4 deletions crates/mako/src/generate/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::fs;
use std::sync::Arc;
use std::path::Path;

use anyhow::Result;

use crate::compiler::Context;
use crate::stats::StatsJsonMap;

pub struct Analyze {}

impl Analyze {
pub fn write_analyze(stats: &StatsJsonMap, context: Arc<Context>) -> Result<()> {
pub fn write_analyze(stats: &StatsJsonMap, path: &Path) -> Result<()> {
let stats_json = serde_json::to_string_pretty(&stats).unwrap();
let html_str = format!(
r#"<!DOCTYPE html>
Expand All @@ -31,7 +30,7 @@ impl Analyze {
stats_json,
include_str!("../../../../client/dist/index.js").replace("</script>", "<\\/script>")
);
let report_path = context.config.output.path.join("analyze-report.html");
let report_path = path.join("analyze-report.html");
fs::write(&report_path, html_str).unwrap();
println!(
"Analyze report generated at: {}",
Expand Down
15 changes: 8 additions & 7 deletions crates/mako/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::config::{DevtoolConfig, OutputMode, TreeShakingStrategy};
use crate::dev::update::UpdateResult;
use crate::generate::generate_chunks::{ChunkFile, ChunkFileType};
use crate::module::{Dependency, ModuleId};
use crate::plugins::bundless_compiler::BundlessCompiler;
use crate::stats::{create_stats_info, print_stats, write_stats};
use crate::utils::base64_encode;
use crate::visitors::async_module::mark_async;
Expand All @@ -48,8 +49,9 @@ struct ChunksUrlMap {
}

impl Compiler {
fn generate_with_plugin_driver(&self) -> Result<()> {
self.context.plugin_driver.generate(&self.context)?;
fn generate_bundless(&self) -> Result<()> {
let bundless_compiler = BundlessCompiler::new(self.context.clone());
bundless_compiler.generate()?;

let stats = create_stats_info(0, self);

Expand Down Expand Up @@ -114,9 +116,8 @@ impl Compiler {
}
let t_tree_shaking = t_tree_shaking.elapsed();

// TODO: improve this hardcode
if self.context.config.output.mode == OutputMode::Bundless {
return self.generate_with_plugin_driver();
return self.generate_bundless();
}

let t_group_chunks = Instant::now();
Expand Down Expand Up @@ -183,7 +184,7 @@ impl Compiler {
let stats = create_stats_info(0, self);

if self.context.config.stats.is_some() {
write_stats(&stats, self);
write_stats(&stats, &self.context.config.output.path);
}

// build_success hook
Expand All @@ -197,7 +198,7 @@ impl Compiler {
}

if self.context.config.analyze.is_some() {
Analyze::write_analyze(&stats, self.context.clone())?;
Analyze::write_analyze(&stats, &self.context.config.output.path)?;
}

debug!("generate done in {}ms", t_generate.elapsed().as_millis());
Expand Down Expand Up @@ -351,7 +352,7 @@ impl Compiler {
// ref: https://github.com/umijs/mako/issues/1107
if self.context.config.stats.is_some() {
let stats = create_stats_info(0, self);
write_stats(&stats, self);
write_stats(&stats, &self.context.config.output.path);
}

let t_generate = t_generate.elapsed();
Expand Down
54 changes: 18 additions & 36 deletions crates/mako/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::path::Path;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use anyhow::Result;
use swc_core::common::errors::Handler;
use swc_core::common::Mark;
use swc_core::ecma::ast::Module;
Expand Down Expand Up @@ -95,10 +95,6 @@ pub trait Plugin: Any + Send + Sync {
Ok(())
}

fn generate(&self, _context: &Arc<Context>) -> Result<Option<()>> {
Ok(None)
}

fn after_generate_chunk_files(
&self,
_chunk_files: &[ChunkFile],
Expand All @@ -107,24 +103,24 @@ pub trait Plugin: Any + Send + Sync {
Ok(())
}

fn build_success(&self, _stats: &StatsJsonMap, _context: &Arc<Context>) -> Result<Option<()>> {
Ok(None)
fn build_success(&self, _stats: &StatsJsonMap, _context: &Arc<Context>) -> Result<()> {
Ok(())
}

fn build_start(&self, _context: &Arc<Context>) -> Result<Option<()>> {
Ok(None)
fn build_start(&self, _context: &Arc<Context>) -> Result<()> {
Ok(())
}

fn generate_beg(&self, _context: &Arc<Context>) -> Result<()> {
fn generate_begin(&self, _context: &Arc<Context>) -> Result<()> {
Ok(())
}

fn generate_end(
&self,
_params: &PluginGenerateEndParams,
_context: &Arc<Context>,
) -> Result<Option<()>> {
Ok(None)
) -> Result<()> {
Ok(())
}

fn runtime_plugins(&self, _context: &Arc<Context>) -> Result<Vec<String>> {
Expand Down Expand Up @@ -255,21 +251,11 @@ impl PluginDriver {

pub fn before_generate(&self, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.generate_beg(context)?;
plugin.generate_begin(context)?;
}
Ok(())
}

pub fn generate(&self, context: &Arc<Context>) -> Result<Option<()>> {
for plugin in &self.plugins {
let ret = plugin.generate(context)?;
if ret.is_some() {
return Ok(Some(()));
}
}
Err(anyhow!("None of the plugins generate content"))
}

pub(crate) fn after_generate_chunk_files(
&self,
chunk_files: &[ChunkFile],
Expand All @@ -282,40 +268,36 @@ impl PluginDriver {
Ok(())
}

pub fn build_start(&self, context: &Arc<Context>) -> Result<Option<()>> {
pub fn build_start(&self, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.build_start(context)?;
}
Ok(None)
Ok(())
}

pub fn generate_end(
&self,
param: &PluginGenerateEndParams,
context: &Arc<Context>,
) -> Result<Option<()>> {
) -> Result<()> {
for plugin in &self.plugins {
plugin.generate_end(param, context)?;
}
Ok(None)
Ok(())
}

pub fn generate_beg(&self, context: &Arc<Context>) -> Result<Option<()>> {
pub fn generate_begin(&self, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.generate_beg(context)?;
plugin.generate_begin(context)?;
}
Ok(None)
Ok(())
}

pub fn build_success(
&self,
stats: &StatsJsonMap,
context: &Arc<Context>,
) -> Result<Option<()>> {
pub fn build_success(&self, stats: &StatsJsonMap, context: &Arc<Context>) -> Result<()> {
for plugin in &self.plugins {
plugin.build_success(stats, context)?;
}
Ok(None)
Ok(())
}

pub fn runtime_plugins_code(&self, context: &Arc<Context>) -> Result<String> {
Expand Down
Loading

0 comments on commit 0159d86

Please sign in to comment.