Skip to content

Commit 6f2cc87

Browse files
authored
Merge branch 'main' into chore/diff-v2
2 parents bbde367 + 4746bb1 commit 6f2cc87

File tree

140 files changed

+730
-555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+730
-555
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/raw_options/raw_builtins/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use rspack_plugin_runtime::{
9292
use rspack_plugin_runtime_chunk::RuntimeChunkPlugin;
9393
use rspack_plugin_schemes::{DataUriPlugin, FileUriPlugin};
9494
use rspack_plugin_size_limits::SizeLimitsPlugin;
95-
use rspack_plugin_sri::SubresourceIntegrityPlugin;
95+
use rspack_plugin_sri::{SubresourceIntegrityPlugin, SubresourceIntegrityPluginOptions};
9696
use rspack_plugin_swc_js_minimizer::SwcJsMinimizerRspackPlugin;
9797
use rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin;
9898
use rspack_plugin_wasm::{
@@ -819,9 +819,15 @@ impl<'a> BuiltinPlugin<'a> {
819819
}
820820
BuiltinPluginName::SubresourceIntegrityPlugin => {
821821
let raw_options = downcast_into::<RawSubresourceIntegrityPluginOptions>(self.options)
822-
.map_err(|report| napi::Error::from_reason(report.to_string()))?;
823-
let options = raw_options.into();
824-
plugins.push(SubresourceIntegrityPlugin::new(options).boxed());
822+
.and_then(SubresourceIntegrityPluginOptions::try_from);
823+
match raw_options {
824+
Ok(options) => {
825+
plugins.push(SubresourceIntegrityPlugin::new(options, None).boxed());
826+
}
827+
Err(error) => {
828+
plugins.push(SubresourceIntegrityPlugin::new(Default::default(), Some(error)).boxed());
829+
}
830+
}
825831
}
826832
BuiltinPluginName::ModuleInfoHeaderPlugin => {
827833
let verbose = downcast_into::<bool>(self.options)

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_sri.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,22 @@ pub struct RawSubresourceIntegrityPluginOptions {
2222
pub html_plugin: String,
2323
}
2424

25-
impl From<RawSubresourceIntegrityPluginOptions> for SubresourceIntegrityPluginOptions {
26-
fn from(options: RawSubresourceIntegrityPluginOptions) -> Self {
27-
Self {
25+
impl TryFrom<RawSubresourceIntegrityPluginOptions> for SubresourceIntegrityPluginOptions {
26+
type Error = rspack_error::Error;
27+
28+
fn try_from(options: RawSubresourceIntegrityPluginOptions) -> Result<Self, rspack_error::Error> {
29+
let html_plugin = options.html_plugin.try_into()?;
30+
if options.hash_func_names.is_empty() {
31+
return Err(rspack_error::Error::error(
32+
"Expect at least one SRI hash function name.".to_string(),
33+
));
34+
}
35+
let hash_func_names = options
36+
.hash_func_names
37+
.into_iter()
38+
.map(SubresourceIntegrityHashFunction::try_from)
39+
.collect::<Result<Vec<_>, rspack_error::Error>>()?;
40+
Ok(Self {
2841
integrity_callback: if let Some(func) = options.integrity_callback {
2942
Some(Arc::new(move |data| {
3043
let func = func.clone();
@@ -33,13 +46,9 @@ impl From<RawSubresourceIntegrityPluginOptions> for SubresourceIntegrityPluginOp
3346
} else {
3447
None
3548
},
36-
hash_func_names: options
37-
.hash_func_names
38-
.into_iter()
39-
.map(SubresourceIntegrityHashFunction::from)
40-
.collect::<Vec<_>>(),
41-
html_plugin: options.html_plugin.into(),
42-
}
49+
hash_func_names,
50+
html_plugin,
51+
})
4352
}
4453
}
4554

crates/rspack_core/src/cache/persistent/build_dependencies/helper/mod.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ impl Helper {
3535
condition_names: Some(vec!["import".into(), "require".into(), "node".into()]),
3636
exports_fields: Some(vec![vec!["exports".into()]]),
3737
builtin_modules: true,
38+
extensions: Some(vec![
39+
".js".into(),
40+
".ts".into(),
41+
".mjs".into(),
42+
".cjs".into(),
43+
".json".into(),
44+
".node".into(),
45+
]),
3846
..Default::default()
3947
},
4048
fs,
@@ -182,10 +190,10 @@ mod test {
182190
async fn helper_file_test() {
183191
let fs = Arc::new(MemoryFileSystem::default());
184192
fs.create_dir_all("/".into()).await.unwrap();
185-
fs.write("/a.js".into(), r#"require("./a1")"#.as_bytes())
193+
fs.write("/a.js".into(), r#"console.log("a")"#.as_bytes())
186194
.await
187195
.unwrap();
188-
fs.write("/a1.js".into(), r#"console.log('a')"#.as_bytes())
196+
fs.write("/a1.jsx".into(), r#"console.log('a1')"#.as_bytes())
189197
.await
190198
.unwrap();
191199
fs.write("/b.js".into(), r#"console.log('b')"#.as_bytes())
@@ -194,14 +202,38 @@ mod test {
194202
fs.write("/c.txt".into(), r#"123"#.as_bytes())
195203
.await
196204
.unwrap();
205+
fs.write("/e.ts".into(), r#"console.log("e")"#.as_bytes())
206+
.await
207+
.unwrap();
208+
fs.write("/e1.tsx".into(), r#"console.log("e1")"#.as_bytes())
209+
.await
210+
.unwrap();
211+
fs.write("/f.json".into(), r#"{"name":"f"}"#.as_bytes())
212+
.await
213+
.unwrap();
214+
fs.write("/g.cjs".into(), r#"console.log("g")"#.as_bytes())
215+
.await
216+
.unwrap();
217+
fs.write("/h.mjs".into(), r#"console.log("h")"#.as_bytes())
218+
.await
219+
.unwrap();
220+
fs.write("/i.node".into(), r#""#.as_bytes()).await.unwrap();
197221
fs.write(
198222
"/index.js".into(),
199223
r#"
200224
import "./a";
225+
import "./a1";
201226
import "./b";
202227
203228
require("./c.txt");
204229
require("./d.md");
230+
231+
require("./e");
232+
require("./e1");
233+
require("./f");
234+
require("./g");
235+
require("./h");
236+
require("./i");
205237
"#
206238
.as_bytes(),
207239
)
@@ -213,9 +245,9 @@ require("./d.md");
213245
.resolve("/index.js".into())
214246
.await
215247
.expect("should have deps");
216-
assert_eq!(deps.len(), 3);
248+
assert_eq!(deps.len(), 8);
217249
let warnings = helper.into_warnings();
218-
assert_eq!(warnings.len(), 1);
250+
assert_eq!(warnings.len(), 3);
219251
}
220252

221253
#[tokio::test]

crates/rspack_core/src/reserved_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub const RESERVED_NAMES: [&str; 188] = [
2-
"__WEBPACK_DEFAULT_EXPORT__",
2+
"__rspack_default_export",
33
"__WEBPACK_NAMESPACE_OBJECT__",
44
"abstract",
55
"arguments",

crates/rspack_core/src/utils/concatenation_scope.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use crate::{
1414
concatenated_module::{ConcatenatedModuleInfo, ModuleInfo},
1515
};
1616

17-
pub static DEFAULT_EXPORT_ATOM: LazyLock<Atom> =
18-
LazyLock::new(|| "__WEBPACK_DEFAULT_EXPORT__".into());
17+
pub static DEFAULT_EXPORT_ATOM: LazyLock<Atom> = LazyLock::new(|| "__rspack_default_export".into());
1918
pub const NAMESPACE_OBJECT_EXPORT: &str = "__WEBPACK_NAMESPACE_OBJECT__";
20-
pub const DEFAULT_EXPORT: &str = "__WEBPACK_DEFAULT_EXPORT__";
19+
pub const DEFAULT_EXPORT: &str = "__rspack_default_export";
2120

2221
static MODULE_REFERENCE_REGEXP: LazyLock<Regex> = LazyLock::new(|| {
2322
Regex::new(

crates/rspack_plugin_sri/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ serde_json = { workspace = true }
3333
sha2 = { workspace = true }
3434
tokio = { workspace = true }
3535
tracing = { workspace = true }
36+
url = { workspace = true }
3637
urlencoding = { workspace = true }
3738

3839
[package.metadata.cargo-shear]
3940
ignored = ["tracing", "async-trait", "rspack_hash", "tokio"]
4041

4142
[lints.rust.unexpected_cfgs]
42-
level = "warn"
4343
check-cfg = ['cfg(allocative)']
44+
level = "warn"

crates/rspack_plugin_sri/src/config.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,30 @@ use crate::integrity::SubresourceIntegrityHashFunction;
1515
pub type IntegrityCallbackFn =
1616
Arc<dyn Fn(IntegrityCallbackData) -> BoxFuture<'static, Result<()>> + Send + Sync>;
1717

18-
#[derive(Debug)]
18+
#[derive(Debug, Default)]
1919
pub enum IntegrityHtmlPlugin {
2020
NativePlugin,
2121
JavaScriptPlugin,
22+
#[default]
2223
Disabled,
2324
}
2425

25-
impl From<String> for IntegrityHtmlPlugin {
26-
fn from(value: String) -> Self {
26+
impl TryFrom<String> for IntegrityHtmlPlugin {
27+
type Error = rspack_error::Error;
28+
29+
fn try_from(value: String) -> Result<Self, rspack_error::Error> {
2730
match value.as_str() {
28-
"JavaScript" => Self::JavaScriptPlugin,
29-
"Native" => Self::NativePlugin,
30-
"Disabled" => Self::Disabled,
31-
_ => panic!("Invalid integrity html plugin: {value}"),
31+
"JavaScript" => Ok(Self::JavaScriptPlugin),
32+
"Native" => Ok(Self::NativePlugin),
33+
"Disabled" => Ok(Self::Disabled),
34+
_ => Err(rspack_error::Error::error(format!(
35+
"Invalid integrity html plugin: {value}"
36+
))),
3237
}
3338
}
3439
}
3540

36-
#[derive(Debug)]
41+
#[derive(Debug, Default)]
3742
pub struct SubresourceIntegrityPluginOptions {
3843
pub hash_func_names: Vec<SubresourceIntegrityHashFunction>,
3944
pub html_plugin: IntegrityHtmlPlugin,

crates/rspack_plugin_sri/src/html.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rspack_plugin_html::{
1111
};
1212
use rustc_hash::FxHashMap as HashMap;
1313
use tokio::sync::RwLock;
14+
use url::Url;
1415

1516
use crate::{
1617
SRICompilationContext, SubresourceIntegrityHashFunction, SubresourceIntegrityPlugin,
@@ -170,6 +171,14 @@ async fn process_tag(
170171
return Ok(None);
171172
};
172173

174+
// A tag which is not generated by chunks should be skipped
175+
if let Ok(url) = Url::parse(&tag_src)
176+
&& (url.scheme() == "http" || url.scheme() == "https")
177+
&& (public_path.is_empty() || !tag_src.starts_with(public_path))
178+
{
179+
return Ok(None);
180+
}
181+
173182
let src = get_asset_path(&tag_src, public_path);
174183
if let Some(integrity) =
175184
get_integrity_checksum_for_asset(&src, integrities, normalized_integrities).await

crates/rspack_plugin_sri/src/integrity.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ pub enum SubresourceIntegrityHashFunction {
1111
Sha512,
1212
}
1313

14-
impl From<String> for SubresourceIntegrityHashFunction {
15-
fn from(s: String) -> Self {
16-
match s.as_str() {
17-
"sha256" => Self::Sha256,
18-
"sha384" => Self::Sha384,
19-
"sha512" => Self::Sha512,
20-
_ => panic!(
21-
"sri hash function only support 'sha256', 'sha384' or 'sha512', but got '{}'.",
22-
s
23-
),
14+
impl TryFrom<String> for SubresourceIntegrityHashFunction {
15+
type Error = rspack_error::Error;
16+
17+
fn try_from(value: String) -> Result<Self, rspack_error::Error> {
18+
match value.as_str() {
19+
"sha256" => Ok(Self::Sha256),
20+
"sha384" => Ok(Self::Sha384),
21+
"sha512" => Ok(Self::Sha512),
22+
_ => Err(rspack_error::Error::error(format!(
23+
"Expect SRI hash function to be 'sha256', 'sha384' or 'sha512', but got '{value}'."
24+
))),
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)