Skip to content

Commit

Permalink
feat(copy): support advanced copy configuration with custom target paths
Browse files Browse the repository at this point in the history
- Add CopyConfig enum to support both basic and advanced copy modes
- Basic mode: maintains backward compatibility with string[] format
- Advanced mode: supports {from: string, to: string} format for custom paths
- Update copy plugin to handle both configuration formats
- Ensure target directories are created automatically

Example config:
{
  'copy': [
    'public',                              // basic mode
    { 'from': 'assets', 'to': 'static' }  // advanced mode
  ]
}
  • Loading branch information
BQXBQX authored and bqx committed Dec 2, 2024
1 parent 89d7c53 commit a3ddec7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
9 changes: 8 additions & 1 deletion crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ pub enum Platform {
Node,
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
pub enum CopyConfig {
Basic(String),
Advanced { from: String, to: String },
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Config {
Expand All @@ -137,7 +144,7 @@ pub struct Config {
pub devtool: Option<DevtoolConfig>,
pub externals: HashMap<String, ExternalConfig>,
pub providers: Providers,
pub copy: Vec<String>,
pub copy: Vec<CopyConfig>,
pub public_path: String,
pub inline_limit: usize,
pub inline_excludes_extensions: Vec<String>,
Expand Down
34 changes: 28 additions & 6 deletions crates/mako/src/plugins/copy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::path::Path;
use std::sync::Arc;

Expand All @@ -11,6 +12,7 @@ use tracing::debug;

use crate::ast::file::win_path;
use crate::compiler::Context;
use crate::config::CopyConfig;
use crate::plugin::Plugin;
use crate::stats::StatsJsonMap;
use crate::utils::tokio_runtime;
Expand All @@ -29,8 +31,12 @@ impl CopyPlugin {
notify::Config::default(),
)
.unwrap();
for src in context.config.copy.iter() {
let src = context.root.join(src);
for config in context.config.copy.iter() {
let src = match config {
CopyConfig::Basic(src) => context.root.join(src),
CopyConfig::Advanced { from, .. } => context.root.join(from),
};

if src.exists() {
debug!("watch {:?}", src);
let mode = if src.is_dir() {
Expand Down Expand Up @@ -62,10 +68,26 @@ impl CopyPlugin {
fn copy(context: &Arc<Context>) -> Result<()> {
debug!("copy");
let dest = context.config.output.path.as_path();
for src in context.config.copy.iter() {
let src = context.root.join(src);
debug!("copy {:?} to {:?}", src, dest);
copy(src.as_path(), dest)?;
for config in context.config.copy.iter() {
match config {
CopyConfig::Basic(src) => {
let src = context.root.join(src);
debug!("copy {:?} to {:?}", src, dest);
copy(&src, dest)?;
}

CopyConfig::Advanced { from, to } => {
let src = context.root.join(from);
let target = dest.join(to.trim_start_matches("/"));

if !target.exists() {
fs::create_dir_all(&target)?;
}

debug!("copy {:?} to {:?}", src, target);
copy(&src, &target)?;
}
}
}
Ok(())
}
Expand Down

0 comments on commit a3ddec7

Please sign in to comment.