Skip to content

Commit

Permalink
Merge branch 'main' into action_scope_0906
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 9, 2024
2 parents 9762b86 + 500c045 commit 6905655
Show file tree
Hide file tree
Showing 22 changed files with 260 additions and 268 deletions.
4 changes: 1 addition & 3 deletions crates/node_binding/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use std::{
},
};

use rspack_fs_node::AsyncNodeWritableFileSystem;

type CompilerInner = rspack_core::Compiler<AsyncNodeWritableFileSystem>;
type CompilerInner = rspack_core::Compiler;

/// `Compiler` struct that is `!Unpin`.
pub(crate) struct Compiler(CompilerInner, PhantomPinned);
Expand Down
6 changes: 4 additions & 2 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ impl Rspack {
let rspack = rspack_core::Compiler::new(
compiler_options,
plugins,
AsyncNodeWritableFileSystem::new(output_filesystem)
.map_err(|e| Error::from_reason(format!("Failed to create writable filesystem: {e}",)))?,
Box::new(
AsyncNodeWritableFileSystem::new(output_filesystem)
.map_err(|e| Error::from_reason(format!("Failed to create writable filesystem: {e}",)))?,
),
Some(resolver_factory),
Some(loader_resolver_factory),
);
Expand Down
52 changes: 35 additions & 17 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use rspack_core::{
AssetParserDataUrlOptions, AssetParserOptions, AssetResourceGeneratorOptions,
CssAutoGeneratorOptions, CssAutoParserOptions, CssGeneratorOptions, CssModuleGeneratorOptions,
CssModuleParserOptions, CssParserOptions, DescriptionData, DynamicImportFetchPriority,
DynamicImportMode, ExportPresenceMode, FuncUseCtx, GeneratorOptions,
GeneratorOptionsByModuleType, JavascriptParserOptions, JavascriptParserOrder,
JavascriptParserUrl, ModuleNoParseRule, ModuleNoParseRules, ModuleNoParseTestFn, ModuleOptions,
ModuleRule, ModuleRuleEffect, ModuleRuleEnforce, ModuleRuleUse, ModuleRuleUseLoader, ModuleType,
OverrideStrict, ParserOptions, ParserOptionsByModuleType,
DynamicImportMode, ExportPresenceMode, FuncUseCtx, GeneratorOptions, GeneratorOptionsMap,
JavascriptParserOptions, JavascriptParserOrder, JavascriptParserUrl, ModuleNoParseRule,
ModuleNoParseRules, ModuleNoParseTestFn, ModuleOptions, ModuleRule, ModuleRuleEffect,
ModuleRuleEnforce, ModuleRuleUse, ModuleRuleUseLoader, OverrideStrict, ParserOptions,
ParserOptionsMap,
};
use rspack_error::error;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
Expand Down Expand Up @@ -203,14 +203,32 @@ impl From<RawParserOptions> for ParserOptions {
.expect("should have an \"asset\" when RawParserOptions.type is \"asset\"")
.into(),
),
"javascript" | "javascript/auto" | "javascript/dynamic" | "javascript/esm" => {
Self::Javascript(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript\"")
.into(),
)
}
"javascript" => Self::Javascript(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript\"")
.into(),
),
"javascript/auto" => Self::JavascriptAuto(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript/auto\"")
.into(),
),
"javascript/dynamic" => Self::JavascriptDynamic(
value
.javascript
.expect(
"should have an \"javascript\" when RawParserOptions.type is \"javascript/dynamic\"",
)
.into(),
),
"javascript/esm" => Self::JavascriptEsm(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript/esm\"")
.into(),
),
"css" => Self::Css(
value
.css
Expand Down Expand Up @@ -805,16 +823,16 @@ impl TryFrom<RawModuleOptions> for ModuleOptions {
.parser
.map(|x| {
x.into_iter()
.map(|(k, v)| Ok((ModuleType::from(k.as_str()), v.into())))
.collect::<std::result::Result<ParserOptionsByModuleType, rspack_error::Error>>()
.map(|(k, v)| Ok((k, v.into())))
.collect::<std::result::Result<ParserOptionsMap, rspack_error::Error>>()
})
.transpose()?,
generator: value
.generator
.map(|x| {
x.into_iter()
.map(|(k, v)| Ok((ModuleType::from(k.as_str()), v.into())))
.collect::<std::result::Result<GeneratorOptionsByModuleType, rspack_error::Error>>()
.map(|(k, v)| Ok((k, v.into())))
.collect::<std::result::Result<GeneratorOptionsMap, rspack_error::Error>>()
})
.transpose()?,
no_parse: value
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::path::PathBuf;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rspack_collections::{Identifier, IdentifierMap};
use rspack_error::Result;
use rspack_fs::AsyncWritableFileSystem;
use rspack_hash::RspackHashDigest;
use rspack_sources::Source;
use rustc_hash::FxHashSet as HashSet;
Expand All @@ -12,10 +11,7 @@ use crate::{
fast_set, get_chunk_from_ukey, ChunkKind, Compilation, Compiler, ModuleExecutor, RuntimeSpec,
};

impl<T> Compiler<T>
where
T: AsyncWritableFileSystem + Send + Sync,
{
impl Compiler {
pub async fn rebuild(
&mut self,
changed_files: std::collections::HashSet<String>,
Expand Down
20 changes: 8 additions & 12 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ mod compilation;
mod hmr;
mod make;
mod module_executor;

use std::sync::Arc;

use derivative::Derivative;
use rspack_error::Result;
use rspack_fs::AsyncWritableFileSystem;
use rspack_futures::FuturesResults;
Expand Down Expand Up @@ -49,13 +49,12 @@ pub struct CompilerHooks {
pub asset_emitted: CompilerAssetEmittedHook,
}

#[derive(Debug)]
pub struct Compiler<T>
where
T: AsyncWritableFileSystem + Send + Sync,
{
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Compiler {
pub options: Arc<CompilerOptions>,
pub output_filesystem: T,
#[derivative(Debug = "ignore")]
pub output_filesystem: Box<dyn AsyncWritableFileSystem + Send + Sync>,
pub compilation: Compilation,
pub plugin_driver: SharedPluginDriver,
pub resolver_factory: Arc<ResolverFactory>,
Expand All @@ -67,15 +66,12 @@ where
unaffected_modules_cache: Arc<UnaffectedModulesCache>,
}

impl<T> Compiler<T>
where
T: AsyncWritableFileSystem + Send + Sync,
{
impl Compiler {
#[instrument(skip_all)]
pub fn new(
options: CompilerOptions,
plugins: Vec<BoxPlugin>,
output_filesystem: T,
output_filesystem: Box<dyn AsyncWritableFileSystem + Send + Sync>,
// no need to pass resolve_factory in rust api
resolver_factory: Option<Arc<ResolverFactory>>,
loader_resolver_factory: Option<Arc<ResolverFactory>>,
Expand Down
49 changes: 38 additions & 11 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,25 +691,52 @@ impl NormalModuleFactory {
.module
.parser
.as_ref()
.and_then(|p| p.get(module_type))
.cloned();
.and_then(|p| 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,
options,
|javascript_options, options| match (javascript_options, options) {
(
ParserOptions::Javascript(a),
ParserOptions::JavascriptAuto(b)
| ParserOptions::JavascriptDynamic(b)
| ParserOptions::JavascriptEsm(b),
) => ParserOptions::Javascript(a.merge_from(b)),
_ => unreachable!(),
},
)
}
_ => p.get(module_type.as_str()).cloned(),
});
let global_generator = self
.options
.module
.generator
.as_ref()
.and_then(|g| g.get(module_type))
.cloned();
.and_then(|g| g.get(module_type.as_str()).cloned());
let parser = rspack_util::merge_from_optional_with(
global_parser,
parser.as_ref(),
|global, local| match (&global, local) {
(ParserOptions::Asset(_), ParserOptions::Asset(_))
| (ParserOptions::Css(_), ParserOptions::Css(_))
| (ParserOptions::CssAuto(_), ParserOptions::CssAuto(_))
| (ParserOptions::CssModule(_), ParserOptions::CssModule(_))
| (ParserOptions::Javascript(_), ParserOptions::Javascript(_)) => global.merge_from(local),
_ => global,
|global, local| match (global, local) {
(ParserOptions::Asset(a), ParserOptions::Asset(b)) => ParserOptions::Asset(a.merge_from(b)),
(ParserOptions::Css(a), ParserOptions::Css(b)) => ParserOptions::Css(a.merge_from(b)),
(ParserOptions::CssAuto(a), ParserOptions::CssAuto(b)) => {
ParserOptions::CssAuto(a.merge_from(b))
}
(ParserOptions::CssModule(a), ParserOptions::CssModule(b)) => {
ParserOptions::CssModule(a.merge_from(b))
}
(
ParserOptions::Javascript(a),
ParserOptions::JavascriptAuto(b)
| ParserOptions::JavascriptDynamic(b)
| ParserOptions::JavascriptEsm(b),
) => ParserOptions::Javascript(a.merge_from(b)),
(global, _) => global,
},
);
let generator = rspack_util::merge_from_optional_with(
Expand Down
38 changes: 24 additions & 14 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use rustc_hash::FxHashMap as HashMap;
use crate::{Filename, Module, ModuleType, PublicPath, Resolve};

#[derive(Debug)]
pub struct ParserOptionsByModuleType(HashMap<ModuleType, ParserOptions>);
pub struct ParserOptionsMap(HashMap<String, ParserOptions>);

impl FromIterator<(ModuleType, ParserOptions)> for ParserOptionsByModuleType {
fn from_iter<I: IntoIterator<Item = (ModuleType, ParserOptions)>>(i: I) -> Self {
impl FromIterator<(String, ParserOptions)> for ParserOptionsMap {
fn from_iter<I: IntoIterator<Item = (String, ParserOptions)>>(i: I) -> Self {
Self(HashMap::from_iter(i))
}
}

impl ParserOptionsByModuleType {
pub fn get<'a>(&'a self, module_type: &'a ModuleType) -> Option<&'a ParserOptions> {
self.0.get(module_type)
impl ParserOptionsMap {
pub fn get<'a>(&'a self, key: &'a str) -> Option<&'a ParserOptions> {
self.0.get(key)
}
}

Expand All @@ -37,6 +37,9 @@ pub enum ParserOptions {
CssAuto(CssAutoParserOptions),
CssModule(CssModuleParserOptions),
Javascript(JavascriptParserOptions),
JavascriptAuto(JavascriptParserOptions),
JavascriptEsm(JavascriptParserOptions),
JavascriptDynamic(JavascriptParserOptions),
Unknown,
}

Expand All @@ -57,6 +60,13 @@ impl ParserOptions {
get_variant!(get_css_auto, CssAuto, CssAutoParserOptions);
get_variant!(get_css_module, CssModule, CssModuleParserOptions);
get_variant!(get_javascript, Javascript, JavascriptParserOptions);
get_variant!(get_javascript_auto, JavascriptAuto, JavascriptParserOptions);
get_variant!(get_javascript_esm, JavascriptEsm, JavascriptParserOptions);
get_variant!(
get_javascript_dynamic,
JavascriptDynamic,
JavascriptParserOptions
);
}

#[derive(Debug, Clone, Copy, MergeFrom)]
Expand Down Expand Up @@ -259,17 +269,17 @@ pub struct CssModuleParserOptions {
}

#[derive(Debug)]
pub struct GeneratorOptionsByModuleType(HashMap<ModuleType, GeneratorOptions>);
pub struct GeneratorOptionsMap(HashMap<String, GeneratorOptions>);

impl FromIterator<(ModuleType, GeneratorOptions)> for GeneratorOptionsByModuleType {
fn from_iter<I: IntoIterator<Item = (ModuleType, GeneratorOptions)>>(i: I) -> Self {
impl FromIterator<(String, GeneratorOptions)> for GeneratorOptionsMap {
fn from_iter<I: IntoIterator<Item = (String, GeneratorOptions)>>(i: I) -> Self {
Self(HashMap::from_iter(i))
}
}

impl GeneratorOptionsByModuleType {
pub fn get(&self, module_type: &ModuleType) -> Option<&GeneratorOptions> {
self.0.get(module_type)
impl GeneratorOptionsMap {
pub fn get(&self, key: &str) -> Option<&GeneratorOptions> {
self.0.get(key)
}
}

Expand Down Expand Up @@ -758,7 +768,7 @@ pub enum ModuleRuleEnforce {
#[derive(Debug, Default)]
pub struct ModuleOptions {
pub rules: Vec<ModuleRule>,
pub parser: Option<ParserOptionsByModuleType>,
pub generator: Option<GeneratorOptionsByModuleType>,
pub parser: Option<ParserOptionsMap>,
pub generator: Option<GeneratorOptionsMap>,
pub no_parse: Option<ModuleNoParseRules>,
}
30 changes: 9 additions & 21 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,9 @@ impl SourceMapDevToolPlugin {

mapped_sources
.into_par_iter()
.map(|(filename, asset, source_map)| {
let code_buffer = {
let mut buffer = Vec::with_capacity(asset.size());
asset.to_writer(&mut buffer).into_diagnostic()?;
buffer
};
let source_map_buffer = match source_map {
Some(map) => {
let mut buffer = Vec::new();
map.to_writer(&mut buffer).into_diagnostic()?;
Some(buffer)
}
.map(|(filename, source, source_map)| {
let source_map_json = match source_map {
Some(map) => Some(map.to_json().into_diagnostic()?),
None => None,
};

Expand All @@ -367,10 +358,7 @@ impl SourceMapDevToolPlugin {
)
})
.clone();
// convert to RawSource to reduce one time source map calculation when convert to JsCompatSource
let raw_source = RawSource::from(code_buffer).boxed();
let Some(source_map_buffer) = source_map_buffer else {
asset.source = Some(raw_source);
let Some(source_map_json) = source_map_json else {
return Ok(MappedAsset {
asset: (filename, asset),
source_map: None,
Expand Down Expand Up @@ -440,7 +428,7 @@ impl SourceMapDevToolPlugin {
};
asset.source = Some(
ConcatSource::new([
raw_source,
source.clone(),
RawSource::from(
current_source_mapping_url_comment.replace("[url]", &source_map_url),
)
Expand All @@ -450,15 +438,15 @@ impl SourceMapDevToolPlugin {
);
asset.info.related.source_map = Some(source_map_filename.clone());
} else {
asset.source = Some(raw_source);
asset.source = Some(source.clone());
}
let mut source_map_asset_info = AssetInfo::default().with_development(true);
if let Some(asset) = compilation.assets().get(filename.as_ref()) {
// set source map asset version to be the same as the target asset
source_map_asset_info.version = asset.info.version.clone();
}
let source_map_asset = CompilationAsset::new(
Some(RawSource::from(source_map_buffer).boxed()),
Some(RawSource::from(source_map_json).boxed()),
source_map_asset_info,
);
Ok(MappedAsset {
Expand All @@ -476,10 +464,10 @@ impl SourceMapDevToolPlugin {
))
}
};
let base64 = rspack_base64::encode_to_string(&source_map_buffer);
let base64 = rspack_base64::encode_to_string(source_map_json.as_bytes());
asset.source = Some(
ConcatSource::new([
raw_source,
source.clone(),
RawSource::from(current_source_mapping_url_comment.replace(
"[url]",
&format!("data:application/json;charset=utf-8;base64,{base64}"),
Expand Down
Loading

0 comments on commit 6905655

Please sign in to comment.