Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into refactory/change_mo…
Browse files Browse the repository at this point in the history
…dule_graph
  • Loading branch information
stormslowly committed Dec 5, 2024
2 parents 7e8c9c3 + ea532ba commit 3fe7af2
Show file tree
Hide file tree
Showing 35 changed files with 477 additions and 117 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.9.7

`2024-11-25`

- fix: devserver response header add cacheControl no-cache by [@Jinbao1001](https://github.com/Jinbao1001) in [#1692](https://github.com/umijs/mako/pull/1692) [#1699](https://github.com/umijs/mako/pull/1699)
- fix(ssu): in case external not available to all entries by [@stormslowly](https://github.com/stormslowly) in [#1698](https://github.com/umijs/mako/pull/1698)

## 0.9.6

`2024-11-14`
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG_zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.9.7

`2024-11-25`

- 修复: 开发服务器未设置缓存头问题 by [@Jinbao1001](https://github.com/Jinbao1001) in [#1692](https://github.com/umijs/mako/pull/1692) [#1699](https://github.com/umijs/mako/pull/1699)
- 修复: SSU 补偿的 external 代码可能出错的情况 [@stormslowly](https://github.com/stormslowly) in [#1698](https://github.com/umijs/mako/pull/1698)

## 0.9.6

`2024-11-14`
Expand Down
5 changes: 4 additions & 1 deletion crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ pub struct BuildParams {
selectorDoubleList?: string[];
mediaQuery?: boolean;
};
stats?: boolean;
stats?: false | {
modules?: boolean;
};
hash?: boolean;
autoCSSModules?: boolean;
ignoreCSSParserErrors?: boolean;
Expand All @@ -146,6 +148,7 @@ pub struct BuildParams {
};
optimization?: false | {
skipModules?: boolean;
concatenateModules?: boolean;
};
react?: {
runtime?: "automatic" | "classic";
Expand Down
21 changes: 21 additions & 0 deletions crates/mako/src/ast/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use swc_core::ecma::ast::{
MetaPropExpr, MetaPropKind, Module, ModuleItem,
};

use crate::module::{ModuleAst, ModuleSystem};

pub fn is_remote_or_data(url: &str) -> bool {
let lower_url = url.to_lowercase();
// ref:
Expand Down Expand Up @@ -148,3 +150,22 @@ pub fn require_ensure(source: String) -> Expr {
}],
)
}

pub fn get_module_system(ast: &ModuleAst) -> ModuleSystem {
match ast {
ModuleAst::Script(module) => {
let is_esm = module
.ast
.body
.iter()
.any(|s| matches!(s, ModuleItem::ModuleDecl(_)));
if is_esm {
ModuleSystem::ESModule
} else {
ModuleSystem::CommonJS
}
}
crate::module::ModuleAst::Css(_) => ModuleSystem::Custom,
crate::module::ModuleAst::None => ModuleSystem::Custom,
}
}
5 changes: 5 additions & 0 deletions crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use colored::Colorize;
use thiserror::Error;

use crate::ast::file::{Content, File, JsContent};
use crate::ast::utils::get_module_system;
use crate::compiler::{Compiler, Context};
use crate::generate::chunk_pot::util::hash_hashmap;
use crate::module::{Module, ModuleAst, ModuleId, ModuleInfo};
Expand Down Expand Up @@ -186,6 +187,7 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
let raw = file.get_content_raw();
let info = ModuleInfo {
file,
module_system: get_module_system(&ast),
ast,
external: Some(external_name),
is_async,
Expand All @@ -210,6 +212,7 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
let raw = file.get_content_raw();
let info = ModuleInfo {
file,
module_system: get_module_system(&ast),
ast,
raw,
..Default::default()
Expand All @@ -235,6 +238,7 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(

ModuleInfo {
file,
module_system: get_module_system(&ast),
ast,
is_ignored: true,
..Default::default()
Expand Down Expand Up @@ -315,6 +319,7 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
let info = ModuleInfo {
file,
deps,
module_system: get_module_system(&ast),
ast,
resolved_resource: parent_resource,
source_map_chain,
Expand Down
6 changes: 5 additions & 1 deletion crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tracing::debug;

use crate::ast::comments::Comments;
use crate::ast::file::win_path;
use crate::config::{Config, ModuleIdStrategy, OutputMode};
use crate::config::{Config, Mode, ModuleIdStrategy, OutputMode};
use crate::generate::chunk_graph::ChunkGraph;
use crate::generate::optimize_chunk::OptimizeChunksInfo;
use crate::module_graph::{ModuleGraph, ModuleRegistry};
Expand Down Expand Up @@ -262,6 +262,10 @@ impl Compiler {

let mut config = config;

if config.mode == Mode::Production && config.experimental.imports_checker {
plugins.push(Arc::new(plugins::imports_checker::ImportsChecker {}));
}

if let Some(progress) = &config.progress {
plugins.push(Arc::new(plugins::progress::ProgressPlugin::new(
plugins::progress::ProgressPluginOptions {
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/config/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct ExperimentalConfig {
#[serde(deserialize_with = "deserialize_detect_loop")]
pub detect_circular_dependence: Option<DetectCircularDependence>,
pub central_ensure: bool,
pub imports_checker: bool,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/config/mako.config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"ignores": ["node_modules"],
"graphviz": false
},
"centralEnsure": true
"centralEnsure": true,
"importsChecker": false
},
"useDefineForClassFields": true,
"emitDecoratorMetadata": false,
Expand Down
14 changes: 11 additions & 3 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::{self, Result};
use colored::Colorize;
use futures::{SinkExt, StreamExt};
use get_if_addrs::get_if_addrs;
use hyper::header::CONTENT_TYPE;
use hyper::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CACHE_CONTROL, CONTENT_TYPE};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Server};
use notify_debouncer_full::new_debouncer;
Expand Down Expand Up @@ -72,8 +72,12 @@ impl DevServer {
Ok::<_, hyper::Error>(service_fn(move |req| {
let context = context.clone();
let txws = txws.clone();
let staticfile =
hyper_staticfile::Static::new(context.config.output.path.clone());
let staticfile = {
let mut sf =
hyper_staticfile::Static::new(context.config.output.path.clone());
sf.cache_headers(Some(0));
sf
};
async move { Self::handle_requests(req, context, staticfile, txws).await }
}))
}
Expand Down Expand Up @@ -179,6 +183,8 @@ impl DevServer {

return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CACHE_CONTROL, "no-cache")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
Expand All @@ -194,6 +200,8 @@ impl DevServer {
Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.header(CACHE_CONTROL, "no-cache")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(hyper::Body::from(bytes))
.unwrap())
});
Expand Down
9 changes: 9 additions & 0 deletions crates/mako/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ pub struct Dependency {
pub span: Option<Span>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModuleSystem {
CommonJS,
ESModule,
Custom,
}

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Default)]
pub struct ResolveTypeFlags: u16 {
Expand Down Expand Up @@ -192,11 +199,13 @@ pub struct ModuleInfo {
pub resolved_resource: Option<ResolverResource>,
/// The transformed source map chain of this module
pub source_map_chain: Vec<Vec<u8>>,
pub module_system: ModuleSystem,
}

impl Default for ModuleInfo {
fn default() -> Self {
Self {
module_system: ModuleSystem::CommonJS,
ast: ModuleAst::None,
file: Default::default(),
deps: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod graphviz;
pub mod hmr_runtime;
pub mod ignore;
pub mod import;
pub mod imports_checker;
pub mod invalid_webpack_syntax;
pub mod manifest;
pub mod minifish;
Expand Down
6 changes: 4 additions & 2 deletions crates/mako/src/plugins/central_ensure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::iter::once;
use std::sync::Arc;

use anyhow::anyhow;
Expand Down Expand Up @@ -32,8 +33,9 @@ pub fn module_ensure_map(context: &Arc<Context>) -> anyhow::Result<BTreeMap<Stri
.iter()
.for_each(|chunk| {
let deps_chunks = cg
.installable_descendants_chunk(&chunk.id)
.sync_dependencies_chunk(&chunk.id)
.iter()
.chain(once(&chunk.id))
.map(|chunk_id| chunk_id.generate(context))
.collect::<Vec<_>>();

Expand Down Expand Up @@ -61,7 +63,7 @@ impl Plugin for CentralChunkEnsure {
let map = {ensure_map};
requireModule.updateEnsure2Map = function(newMapping) {{
map = newMapping;
}}
}};
requireModule.ensure2 = function(chunkId){{
let toEnsure = map[chunkId];
if (toEnsure) {{
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/detect_circular_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Plugin for LoopDetector {

if detect_loop.graphviz {
let dot_content = loop_lines.join("\n");
let dot = format!(r#"digraph Loop {{\n{}\n}}"#, dot_content);
let dot = format!("digraph Loop {{\n{}\n}}\n", dot_content);
std::fs::write(context.root.join("_mako_loop_detector.dot"), dot)?;
}
}
Expand Down
60 changes: 33 additions & 27 deletions crates/mako/src/plugins/duplicate_package_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::{Arc, RwLock};
use semver::Version;

use crate::compiler::Context;
use crate::module::Module;
use crate::module_graph::{ModuleGraph, ModuleRegistry};
use crate::plugin::Plugin;
use crate::resolve::ResolverResource;
Expand Down Expand Up @@ -46,6 +47,30 @@ fn clean_path_relative_to_context(module_path: &Path, context: &Path) -> PathBuf
}
}

fn extract_package_info(module: &Module) -> Option<PackageInfo> {
module
.info
.as_ref()
.and_then(|info| info.resolved_resource.as_ref())
.and_then(|resolver_resource| {
if let ResolverResource::Resolved(resource) = resolver_resource {
let package_json = resource.0.package_json()?;
let name = package_json.name.clone()?;
let raw_json = package_json.raw_json();
let version = raw_json.as_object()?.get("version")?;
let version = semver::Version::parse(version.as_str().unwrap()).ok()?;

Some(PackageInfo {
name,
version,
path: package_json.path.clone(),
})
} else {
None
}
})
}

impl DuplicatePackageCheckerPlugin {
pub fn new() -> Self {
Self::default()
Expand Down Expand Up @@ -93,33 +118,14 @@ impl DuplicatePackageCheckerPlugin {
.read()
.unwrap()
.modules()
.iter()
.for_each(|module_id| {
if let Some(module) = module_registry.module(module_id) {
if let Some(ResolverResource::Resolved(resource)) = module
.info
.as_ref()
.and_then(|info| info.resolved_resource.as_ref())
{
if let Some(package_json) = resource.0.package_json() {
let raw_json = package_json.raw_json();
if let Some(name) = package_json.name.clone() {
if let Some(version) = raw_json.as_object().unwrap().get("version")
{
let version = semver::Version::parse(version.as_str().unwrap());
if let Ok(version) = version {
let package_info = PackageInfo {
name,
version,
path: package_json.path.clone(),
};
packages.push(package_info);
}
}
}
}
}
}
.into_iter()
.filter_map(|module_id| {
module_registry
.get_module(module_id)
.and_then(extract_package_info)
})
.for_each(|package_info| {
packages.push(package_info);
});

Self::find_duplicates(packages)
Expand Down
Loading

0 comments on commit 3fe7af2

Please sign in to comment.