Skip to content

Commit 5ce14bf

Browse files
committed
chmod
1 parent 6bf6543 commit 5ce14bf

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

Cargo.lock

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

crates/rspack_binding_api/src/fs_node/node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Write = ThreadsafeFunction<FnArgs<(i32, Buffer, u32)>, Promise<Either<u32,
1111
type Read = ThreadsafeFunction<FnArgs<(i32, u32, u32)>, Promise<Either<Buffer, ()>>>;
1212
type ReadUtil = ThreadsafeFunction<FnArgs<(i32, u8, u32)>, Promise<Either<Buffer, ()>>>;
1313
type ReadToEnd = ThreadsafeFunction<FnArgs<(i32, u32)>, Promise<Either<Buffer, ()>>>;
14+
type Chmod = ThreadsafeFunction<FnArgs<(String, u32)>, Promise<()>>;
1415

1516
#[derive(Debug)]
1617
#[napi(object, object_to_js = false, js_name = "ThreadsafeNodeFS")]
@@ -53,7 +54,7 @@ pub struct ThreadsafeNodeFS {
5354
pub read_to_end: ReadToEnd,
5455
// The following functions are not supported by webpack, so they are optional
5556
#[napi(ts_type = "(name: string, mode: number) => Promise<void>")]
56-
pub chmod: Option<ThreadsafeFunction<(String, u32), Promise<()>>>,
57+
pub chmod: Option<Chmod>,
5758
}
5859

5960
#[napi(object, object_to_js = false)]

crates/rspack_binding_api/src/fs_node/write.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ impl WritableFileSystem for NodeFileSystem {
134134
&& let Some(chmod) = &self.0.chmod
135135
{
136136
let file = path.as_str().to_string();
137-
return chmod.call_with_promise((file, mode)).await.to_fs_result();
137+
return chmod
138+
.call_with_promise((file, mode).into())
139+
.await
140+
.to_fs_result();
138141
}
139142
Ok(())
140143
}

crates/rspack_plugin_esm_library/src/render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl EsmLibraryPlugin {
101101
chunk_init_fragments.insert(
102102
0,
103103
Box::new(rspack_core::NormalInitFragment::new(
104-
format!("{}\n", hashbang),
104+
format!("{hashbang}\n"),
105105
rspack_core::InitFragmentStage::StageConstants,
106106
i32::MIN,
107107
rspack_core::InitFragmentKey::unique(),
@@ -117,7 +117,7 @@ impl EsmLibraryPlugin {
117117
chunk_init_fragments.insert(
118118
insert_pos,
119119
Box::new(rspack_core::NormalInitFragment::new(
120-
format!("{}\n", directive),
120+
format!("{directive}\n"),
121121
rspack_core::InitFragmentStage::StageConstants,
122122
i32::MIN + 1 + idx as i32,
123123
rspack_core::InitFragmentKey::unique(),

crates/rspack_plugin_rslib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version.workspace = true
1111
rspack_cacheable = { workspace = true }
1212
rspack_core = { workspace = true }
1313
rspack_error = { workspace = true }
14+
rspack_fs = { workspace = true }
1415
rspack_hash = { workspace = true }
1516
rspack_hook = { workspace = true }
1617
rspack_plugin_asset = { workspace = true }

crates/rspack_plugin_rslib/src/plugin.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::{
44
};
55

66
use rspack_core::{
7-
ChunkUkey, Compilation, CompilationParams, CompilerCompilation, CompilerFinishMake, ModuleType,
8-
NormalModuleFactoryParser, ParserAndGenerator, ParserOptions, Plugin, get_module_directives,
9-
get_module_hashbang,
7+
AssetEmittedInfo, ChunkUkey, Compilation, CompilationParams, CompilerAssetEmitted,
8+
CompilerCompilation, CompilerFinishMake, ModuleType, NormalModuleFactoryParser,
9+
ParserAndGenerator, ParserOptions, Plugin, get_module_directives, get_module_hashbang,
1010
rspack_sources::{ConcatSource, RawStringSource, SourceExt},
1111
};
1212
use rspack_error::Result;
@@ -179,6 +179,30 @@ async fn finish_make(&self, compilation: &mut Compilation) -> Result<()> {
179179
Ok(())
180180
}
181181

182+
#[plugin_hook(CompilerAssetEmitted for RslibPlugin)]
183+
async fn asset_emitted(
184+
&self,
185+
compilation: &Compilation,
186+
_filename: &str,
187+
info: &AssetEmittedInfo,
188+
) -> Result<()> {
189+
use rspack_core::rspack_sources::Source;
190+
use rspack_fs::FilePermissions;
191+
192+
// Check if the file content starts with a hashbang
193+
let content = info.source.source().into_string_lossy();
194+
if content.starts_with("#!") {
195+
// Set file permissions to 0o755 (rwxr-xr-x) using the file system interface
196+
// This will work on Unix/macOS and be a no-op on Windows/WASM
197+
let output_fs = &compilation.output_filesystem;
198+
let permissions = FilePermissions::from_mode(0o755);
199+
output_fs
200+
.set_permissions(&info.target_path, permissions)
201+
.await?;
202+
}
203+
Ok(())
204+
}
205+
182206
impl Plugin for RslibPlugin {
183207
fn name(&self) -> &'static str {
184208
"rslib"
@@ -192,6 +216,10 @@ impl Plugin for RslibPlugin {
192216
.tap(nmf_parser::new(self));
193217

194218
ctx.compiler_hooks.finish_make.tap(finish_make::new(self));
219+
ctx
220+
.compiler_hooks
221+
.asset_emitted
222+
.tap(asset_emitted::new(self));
195223

196224
Ok(())
197225
}

0 commit comments

Comments
 (0)