Skip to content

Commit

Permalink
fix: merge parser and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Dec 20, 2024
1 parent c4ab885 commit 99c2a62
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 44 deletions.
89 changes: 72 additions & 17 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ impl NormalModuleFactory {

let file_dependency = resource_data.resource_path.clone();

println!("matched module type: {:?}", match_module_type);
println!("resolved module rules: {:?}", resolved_module_rules);

let resolved_module_type =
self.calculate_module_type(match_module_type, &resolved_module_rules);
let resolved_module_layer =
Expand All @@ -524,15 +527,29 @@ impl NormalModuleFactory {
));
}

println!("resolved_module_type: {:?}", resolved_module_type);

let resolved_resolve_options = self.calculate_resolve_options(&resolved_module_rules);
let (resolved_parser_options, resolved_generator_options) =
self.calculate_parser_and_generator_options(&resolved_module_rules);

println!(
"resolved_generator_options: {:?}",
resolved_generator_options
);

let (resolved_parser_options, resolved_generator_options) = self
.merge_global_parser_and_generator_options(
&resolved_module_type,
resolved_parser_options,
resolved_generator_options,
);

println!(
"resolved_generator_options: {:?}",
resolved_generator_options
);

let resolved_side_effects = self.calculate_side_effects(&resolved_module_rules);
let mut resolved_parser_and_generator = self
.plugin_driver
Expand Down Expand Up @@ -688,18 +705,13 @@ impl NormalModuleFactory {
parser: Option<ParserOptions>,
generator: Option<GeneratorOptions>,
) -> (Option<ParserOptions>, Option<GeneratorOptions>) {
let global_parser = self
.options
.module
.parser
.as_ref()
.and_then(|p| match module_type {
let global_parser = self.options.module.parser.as_ref().and_then(|p| {
let options = p.get(module_type.as_str());
match module_type {
ModuleType::JsAuto | ModuleType::JsDynamic | ModuleType::JsEsm => {
let options = p.get(module_type.as_str());
let javascript_options = p.get("javascript").cloned();
// Merge `module.parser.["javascript/xxx"]` with `module.parser.["javascript"]` first
rspack_util::merge_from_optional_with(
javascript_options,
p.get("javascript").cloned(),
options,
|javascript_options, options| match (javascript_options, options) {
(
Expand All @@ -712,14 +724,57 @@ impl NormalModuleFactory {
},
)
}
_ => p.get(module_type.as_str()).cloned(),
});
let global_generator = self
.options
.module
.generator
.as_ref()
.and_then(|g| g.get(module_type.as_str()).cloned());
ModuleType::CssAuto | ModuleType::CssModule => rspack_util::merge_from_optional_with(
p.get("css").cloned(),
options,
|css_options, options| match (css_options, options) {
(ParserOptions::Css(a), ParserOptions::CssAuto(b)) => {
ParserOptions::CssAuto(a.to_css_auto_options().merge_from(b))
}
(ParserOptions::Css(a), ParserOptions::CssModule(b)) => {
ParserOptions::CssModule(a.to_css_module_options().merge_from(b))
}
_ => unreachable!(),
},
),
_ => options.cloned(),
}
});
let global_generator = self.options.module.generator.as_ref().and_then(|g| {
let options = g.get(module_type.as_str());

match module_type {
ModuleType::AssetInline | ModuleType::AssetResource => {
rspack_util::merge_from_optional_with(
g.get("asset").cloned(),
options,
|asset_options, options| match (asset_options, options) {
(GeneratorOptions::Asset(a), GeneratorOptions::AssetInline(b)) => {
GeneratorOptions::AssetInline(a.to_asset_inline_options().merge_from(b))
}
(GeneratorOptions::Asset(a), GeneratorOptions::AssetResource(b)) => {
GeneratorOptions::AssetResource(a.to_asset_resource_options().merge_from(b))
}
_ => unreachable!(),
},
)
}
ModuleType::CssAuto | ModuleType::CssModule => rspack_util::merge_from_optional_with(
g.get("css").cloned(),
options,
|css_options, options| match (css_options, options) {
(GeneratorOptions::Css(a), GeneratorOptions::CssAuto(b)) => {
GeneratorOptions::CssAuto(a.to_css_auto_options().merge_from(b))
}
(GeneratorOptions::Css(a), GeneratorOptions::CssModule(b)) => {
GeneratorOptions::CssModule(a.to_css_module_options().merge_from(b))
}
_ => unreachable!(),
},
),
_ => options.cloned(),
}
});
let parser = rspack_util::merge_from_optional_with(
global_parser,
parser.as_ref(),
Expand Down
53 changes: 51 additions & 2 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ pub struct CssParserOptions {
pub named_exports: Option<bool>,
}

impl CssParserOptions {
pub fn to_css_auto_options(&self) -> CssAutoParserOptions {
CssAutoParserOptions {
named_exports: self.named_exports,
}
}

pub fn to_css_module_options(&self) -> CssModuleParserOptions {
CssModuleParserOptions {
named_exports: self.named_exports,
}
}
}

#[cacheable]
#[derive(Debug, Clone, MergeFrom)]
pub struct CssAutoParserOptions {
Expand Down Expand Up @@ -395,6 +409,23 @@ pub struct AssetGeneratorOptions {
pub data_url: Option<AssetGeneratorDataUrl>,
}

impl AssetGeneratorOptions {
pub fn to_asset_inline_options(&self) -> AssetInlineGeneratorOptions {
AssetInlineGeneratorOptions {
data_url: self.data_url.clone(),
}
}

pub fn to_asset_resource_options(&self) -> AssetResourceGeneratorOptions {
AssetResourceGeneratorOptions {
emit: self.emit,
filename: self.filename.clone(),
output_path: self.output_path.clone(),
public_path: self.public_path.clone(),
}
}
}

pub struct AssetGeneratorDataUrlFnCtx<'a> {
pub filename: String,
pub module: &'a dyn Module,
Expand Down Expand Up @@ -474,8 +505,26 @@ pub struct CssGeneratorOptions {
pub es_module: Option<bool>,
}

impl CssGeneratorOptions {
pub fn to_css_auto_options(&self) -> CssAutoGeneratorOptions {
CssAutoGeneratorOptions {
exports_only: self.exports_only,
es_module: self.es_module,
..Default::default()
}
}

pub fn to_css_module_options(&self) -> CssModuleGeneratorOptions {
CssModuleGeneratorOptions {
exports_only: self.exports_only,
es_module: self.es_module,
..Default::default()
}
}
}

#[cacheable]
#[derive(Debug, Clone, MergeFrom)]
#[derive(Default, Debug, Clone, MergeFrom)]
pub struct CssAutoGeneratorOptions {
pub exports_convention: Option<CssExportsConvention>,
pub exports_only: Option<bool>,
Expand All @@ -484,7 +533,7 @@ pub struct CssAutoGeneratorOptions {
}

#[cacheable]
#[derive(Debug, Clone, MergeFrom)]
#[derive(Default, Debug, Clone, MergeFrom)]
pub struct CssModuleGeneratorOptions {
pub exports_convention: Option<CssExportsConvention>,
pub exports_only: Option<bool>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import png from "../_images/file.png";
import svg from "../_images/file.svg?inline";
// import png from "../_images/file.png";
// import svg from "../_images/file.svg?inline";
import jpg from "../_images/file.jpg";

it("should output various asset types", () => {
expect(png).toMatch(/^[\da-f]{16}\.png$/);
expect(svg).toMatch(/^data:image\/svg\+xml,/);
// expect(png).toMatch(/^[\da-f]{16}\.png$/);
// expect(svg).toMatch(/^data:image\/svg\+xml,/);
expect(jpg).toMatch(/^DATA:image\/jpeg;base64,[0-9a-zA-Z+/]+=*$/);
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = () => { return 'https://github.com/web-infra-dev/rspack/issues/8488' }
module.exports = () => { return true }
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ const mimeTypes = require("mime-types");
module.exports = {
mode: "development",
module: {
parser: {
asset: {
dataUrlCondition: (source, { filename }) => {
return filename.includes("?inline");
}
}
},
// parser: {
// asset: {
// dataUrlCondition: (source, { filename }) => {
// return filename.includes("?inline");
// }
// }
// },
generator: {
asset: {
dataUrl: (source, { module }) => {
const mimeType = mimeTypes.lookup(module.nameForCondition());
if (mimeType === "image/svg+xml") {
if (typeof source !== "string") {
source = source.toString();
}

return svgToMiniDataURI(source);
}

const encodedContent = source.toString("base64");

return `DATA:${mimeType};base64,${encodedContent}`;
dataUrl: {
encoding: "base64",
}
},
"asset/inline": {
dataUrl: {
encoding: false,
}
}
},
Expand Down

0 comments on commit 99c2a62

Please sign in to comment.