Skip to content

Commit

Permalink
Merge branch 'main' into inc-chunk-render
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Nov 18, 2024
2 parents dae8c8c + e379fe9 commit 4f8cbff
Show file tree
Hide file tree
Showing 65 changed files with 416 additions and 174 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ jobs:
echo 'debug = false' >> Cargo.toml
- name: Run test
env:
MIRIFLAGS: -Zmiri-tree-borrows -Zmiri-disable-isolation
# reason for excluding https://github.com/napi-rs/napi-rs/issues/2200
run: cargo miri test --workspace --exclude rspack_binding_options --exclude rspack_node -- --nocapture

Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,7 @@ export interface RawLightningCssMinimizerOptions {
include?: number
exclude?: number
draft?: RawDraft
drafts?: RawDraft
nonStandard?: RawNonStandard
pseudoClasses?: RawLightningCssPseudoClasses
unusedSymbols: Array<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub struct RawLightningCssMinimizerOptions {
pub targets: Option<Vec<String>>,
pub include: Option<u32>,
pub exclude: Option<u32>,
// TODO: deprecate `draft` in favor of `drafts`
pub draft: Option<RawDraft>,
pub drafts: Option<RawDraft>,
pub non_standard: Option<RawNonStandard>,
pub pseudo_classes: Option<RawLightningCssPseudoClasses>,
pub unused_symbols: Vec<String>,
Expand Down Expand Up @@ -90,9 +92,14 @@ impl TryFrom<RawLightningCssMinimizerRspackPluginOptions> for PluginOptions {
.flatten(),
include: value.minimizer_options.include,
exclude: value.minimizer_options.exclude,
draft: value.minimizer_options.draft.map(|d| Draft {
custom_media: d.custom_media,
}),
// We should use `drafts` if it is present, otherwise use `draft`
draft: value
.minimizer_options
.drafts
.or(value.minimizer_options.draft)
.map(|d| Draft {
custom_media: d.custom_media,
}),
non_standard: value.minimizer_options.non_standard.map(|n| NonStandard {
deep_selector_combinator: n.deep_selector_combinator,
}),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_values/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl From<JsHtmlPluginTag> for HtmlPluginTag {
value.as_ref().and_then(|v| match v {
Either::A(x) => Some(HtmlPluginAttribute {
attr_name: key.cow_to_ascii_lowercase().into_owned(),
attr_value: Some(x.cow_to_ascii_lowercase().into_owned()),
attr_value: Some(x.to_owned()),
}),
Either::B(x) => {
if *x {
Expand Down
8 changes: 7 additions & 1 deletion crates/rspack_cacheable/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rkyv::{
bytecheck::CheckBytes,
de::Pool,
rancor::{BoxedError, Source, Strategy, Trace},
util::AlignedVec,
Archive, Deserialize,
};

Expand Down Expand Up @@ -79,8 +80,13 @@ where
let guard = ContextGuard::new(context);
let mut deserializer = Pool::default();
guard.add_to_pooling(&mut deserializer)?;
// The `bytes` ptr address in miri will throw UnalignedPointer error in rkyv.
// AlignedVec will force aligned the ptr address.
// Refer code: https://github.com/rkyv/rkyv/blob/dabbc1fcf5052f141403b84493bddb74c44f9ba9/rkyv/src/validation/archive/validator.rs#L135
let mut aligned_vec = AlignedVec::<16>::new();
aligned_vec.extend_from_slice(bytes);
deserialize_using(
access::<T::Archived, DeserializeError>(bytes)?,
access::<T::Archived, DeserializeError>(&aligned_vec)?,
&mut deserializer,
)
}
2 changes: 2 additions & 0 deletions crates/rspack_cacheable_test/tests/macro/cacheable_dyn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_cacheable::{cacheable, cacheable_dyn, from_bytes, to_bytes};

#[test]
#[cfg_attr(miri, ignore)]
fn test_cacheable_dyn_macro() {
struct Context;

Expand Down Expand Up @@ -71,6 +72,7 @@ fn test_cacheable_dyn_macro() {
}

#[test]
#[cfg_attr(miri, ignore)]
fn test_cacheable_dyn_macro_with_generics() {
struct Context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_cacheable::{cacheable, from_bytes, to_bytes};

#[test]
#[cfg_attr(miri, ignore)]
fn test_manual_cacheable_dyn_macro() {
struct Context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_cacheable::{cacheable, from_bytes, to_bytes};

#[test]
#[cfg_attr(miri, ignore)]
fn test_manual_cacheable_dyn_macro_with_generics() {
struct Context;

Expand Down
46 changes: 35 additions & 11 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
to_identifier, AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, BuildMeta,
BuildMetaExportsType, BuildResult, ChunkInitFragments, ChunkUkey, CodeGenerationDataUrl,
CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock, DependencyId,
ExternalType, FactoryMeta, InitFragmentExt, InitFragmentKey, InitFragmentStage, LibIdentOptions,
Module, ModuleType, NormalInitFragment, RuntimeGlobals, RuntimeSpec, SourceType,
ExternalType, FactoryMeta, ImportAttributes, InitFragmentExt, InitFragmentKey, InitFragmentStage,
LibIdentOptions, Module, ModuleType, NormalInitFragment, RuntimeGlobals, RuntimeSpec, SourceType,
StaticExportsDependency, StaticExportsSpec, NAMESPACE_OBJECT_EXPORT,
};
use crate::{ChunkGraph, ModuleGraph};
Expand Down Expand Up @@ -112,12 +112,24 @@ fn get_source_for_commonjs(module_and_specifiers: &ExternalRequestValue) -> Stri
fn get_source_for_import(
module_and_specifiers: &ExternalRequestValue,
compilation: &Compilation,
attributes: &Option<ImportAttributes>,
) -> String {
format!(
"{}({})",
compilation.options.output.import_function_name,
serde_json::to_string(module_and_specifiers.primary()).expect("invalid json to_string")
)
format!("{}({})", compilation.options.output.import_function_name, {
let attributes_str = if let Some(attributes) = attributes {
format!(
", {{ with: {} }}",
serde_json::to_string(attributes).expect("invalid json to_string")
)
} else {
String::new()
};

format!(
"{}{}",
serde_json::to_string(module_and_specifiers.primary()).expect("invalid json to_string"),
attributes_str
)
})
}

/**
Expand Down Expand Up @@ -178,6 +190,7 @@ pub type MetaExternalType = Option<ExternalTypeEnum>;
#[derive(Debug)]
pub struct DependencyMeta {
pub external_type: MetaExternalType,
pub attributes: Option<ImportAttributes>,
}

impl ExternalModule {
Expand Down Expand Up @@ -300,7 +313,7 @@ impl ExternalModule {
"import" if let Some(request) = request => format!(
"{} = {};",
get_namespace_object_export(concatenation_scope, supports_const),
get_source_for_import(request, compilation)
get_source_for_import(request, compilation, &self.dependency_meta.attributes)
),
"var" | "promise" | "const" | "let" | "assign" if let Some(request) = request => format!(
"{} = {};",
Expand All @@ -313,9 +326,20 @@ impl ExternalModule {
chunk_init_fragments.push(
NormalInitFragment::new(
format!(
"import * as __WEBPACK_EXTERNAL_MODULE_{}__ from {};\n",
"import * as __WEBPACK_EXTERNAL_MODULE_{}__ from {}{};\n",
id.clone(),
json_stringify(request.primary())
json_stringify(request.primary()),
{
let meta = &self.dependency_meta.attributes;
if let Some(meta) = meta {
format!(
" with {}",
serde_json::to_string(meta).expect("json stringify failed"),
)
} else {
String::new()
}
},
),
InitFragmentStage::StageESMImports,
0,
Expand Down Expand Up @@ -344,7 +368,7 @@ impl ExternalModule {
format!(
"{} = {};",
get_namespace_object_export(concatenation_scope, supports_const),
get_source_for_import(request, compilation)
get_source_for_import(request, compilation, &self.dependency_meta.attributes)
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_loader_lightningcss/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ pub struct RawConfig {
pub targets: Option<Vec<String>>,
pub include: Option<u32>,
pub exclude: Option<u32>,
// TODO: deprecate `draft` in favor of `drafts`
pub draft: Option<Draft>,
pub drafts: Option<Draft>,
pub non_standard: Option<NonStandard>,
pub pseudo_classes: Option<PseudoClasses>,
pub unused_symbols: Option<Vec<String>>,
Expand All @@ -64,7 +66,8 @@ impl TryFrom<RawConfig> for Config {
.flatten(),
include: value.include,
exclude: value.exclude,
draft: value.draft,
// We should use `drafts` if it is present, otherwise use `draft`
draft: value.drafts.or(value.draft),
non_standard: value.non_standard,
pseudo_classes: value.pseudo_classes,
unused_symbols: value.unused_symbols,
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_macros_test/tests/compiletest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[test]
#[cfg_attr(miri, ignore)]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/hook/*.rs");
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_externals/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl ExternalsPlugin {
}

let dependency_meta: DependencyMeta = DependencyMeta {
attributes: dependency.get_attributes().cloned(),
external_type: {
if dependency
.as_any()
Expand Down
24 changes: 10 additions & 14 deletions crates/rspack_plugin_html/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,17 @@ impl HtmlPluginAssets {
favicon_relative_path.to_string_lossy().to_string().as_str(),
));

let fake_html_file_name = compilation
.get_path(
html_file_name,
PathData::default().filename(output_path.as_str()),
)
.always_ok();

if favicon_path.to_str().unwrap_or_default().is_empty() {
favicon_path = compilation
.options
.output
.path
.as_std_path()
.join(favicon_relative_path)
.relative(PathBuf::from(fake_html_file_name).join(".."));
let fake_html_file_name = compilation
.get_path(
html_file_name,
PathData::default().filename(output_path.as_str()),
)
.always_ok();
let output_path = compilation.options.output.path.as_std_path();
favicon_path = output_path
.relative(output_path.join(fake_html_file_name).join(".."))
.join(favicon_relative_path);
} else {
favicon_path.push(favicon_relative_path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {
define_record = define_record
.with_on_evaluate_identifier(Box::new(move |record, parser, _ident, start, end| {
let evaluated = parser
.evaluate(to_code(&record.code, None).into_owned(), "DefinePlugin")
.evaluate(
to_code(&record.code, None, None).into_owned(),
"DefinePlugin",
)
.map(|mut evaluated| {
evaluated.set_range(start, end);
evaluated
Expand All @@ -216,7 +219,7 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {
}))
.with_on_expression(Box::new(
move |record, parser, span, start, end, for_name| {
let code = to_code(&record.code, Some(!parser.is_asi_position(span.lo)));
let code = to_code(&record.code, Some(!parser.is_asi_position(span.lo)), None);
parser
.presentational_dependencies
.push(Box::new(dep(parser, code, for_name, start, end)));
Expand All @@ -227,7 +230,7 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {

define_record = define_record
.with_on_evaluate_typeof(Box::new(move |record, parser, start, end| {
let code = to_code(&record.code, None);
let code = to_code(&record.code, None, None);
let typeof_code = if is_typeof {
code
} else {
Expand All @@ -241,7 +244,7 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {
})
}))
.with_on_typeof(Box::new(move |record, parser, start, end| {
let code = to_code(&record.code, None);
let code = to_code(&record.code, None, None);
let typeof_code = if is_typeof {
code
} else {
Expand Down Expand Up @@ -285,7 +288,7 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {
}))
.with_on_expression(Box::new(
move |record, parser, span, start, end, for_name| {
let code = to_code(&record.object, Some(!parser.is_asi_position(span.lo)));
let code = to_code(&record.object, Some(!parser.is_asi_position(span.lo)), None);
parser
.presentational_dependencies
.push(Box::new(dep(parser, code, for_name, start, end)));
Expand All @@ -304,7 +307,11 @@ pub(super) fn walk_definitions(definitions: &DefineValue) -> WalkData {
}))
.with_on_expression(Box::new(
move |record, parser, span, start, end, for_name| {
let code = to_code(&record.object, Some(!parser.is_asi_position(span.lo)));
let code = to_code(
&record.object,
Some(!parser.is_asi_position(span.lo)),
parser.destructuring_assignment_properties_for(&span),
);
parser
.presentational_dependencies
.push(Box::new(dep(parser, code, for_name, start, end)));
Expand Down Expand Up @@ -551,7 +558,7 @@ fn dep(
}
}

fn to_code(code: &Value, asi_safe: Option<bool>) -> Cow<str> {
fn to_code(code: &Value, asi_safe: Option<bool>, obj_keys: Option<FxHashSet<String>>) -> Cow<str> {
fn wrap_ansi(code: Cow<str>, is_arr: bool, asi_safe: Option<bool>) -> Cow<str> {
match asi_safe {
Some(true) if is_arr => code,
Expand All @@ -568,13 +575,19 @@ fn to_code(code: &Value, asi_safe: Option<bool>) -> Cow<str> {
Value::Bool(b) => Cow::Borrowed(if *b { "true" } else { "false" }),
Value::Number(n) => Cow::Owned(n.to_string()),
Value::Array(arr) => {
let elements = arr.iter().map(|code| to_code(code, None)).join(",");
let elements = arr.iter().map(|code| to_code(code, None, None)).join(",");
wrap_ansi(Cow::Owned(format!("[{elements}]")), true, asi_safe)
}
Value::Object(obj) => {
let elements = obj
.iter()
.map(|(key, value)| format!("{}:{}", json!(key), to_code(value, None)))
.filter_map(|(key, value)| {
if obj_keys.as_ref().map_or(true, |keys| keys.contains(key)) {
Some(format!("{}:{}", json!(key), to_code(value, None, None)))
} else {
None
}
})
.join(",");
wrap_ansi(Cow::Owned(format!("{{ {elements} }}")), false, asi_safe)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ impl DependencyTemplate for ModernModuleImportDependency {
};

if let Some(request_and_external_type) = request_and_external_type.0 {
let attributes_str = if let Some(attributes) = &self.attributes {
format!(
", {{ with: {} }}",
serde_json::to_string(attributes).expect("invalid json to_string")
)
} else {
String::new()
};

source.replace(
self.range.start,
self.range.end,
format!(
"import({})",
"import({}{})",
serde_json::to_string(request_and_external_type.primary())
.expect("invalid json to_string")
.expect("invalid json to_string"),
attributes_str
)
.as_str(),
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> {
external_module.request.clone(),
external_module.external_type.clone(),
import_dependency.range.clone(),
None,
import_dependency.get_attributes().cloned(),
);

deps_to_replace.push((
Expand Down
Loading

0 comments on commit 4f8cbff

Please sign in to comment.