Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat/watch_node_mod…
Browse files Browse the repository at this point in the history
…ules
  • Loading branch information
Jinbao1001 committed Jul 12, 2024
2 parents 1164fb8 + ce4b647 commit 4d1b64e
Show file tree
Hide file tree
Showing 70 changed files with 1,802 additions and 567 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@

## 0.7.5

`2024-07-11`

- New: Added console warning for HMR if React is external by [@PeachScript](https://github.com/PeachScript) in [#1354](https://github.com/umijs/mako/pull/1354)
- New: CLI now supports custom project names by [@kiner-tang](https://github.com/kiner-tang) in [#1340](https://github.com/umijs/mako/pull/1340)
- New: Upgraded hyper-staticfile to fix JS file charset issues by [@whyer11](https://github.com/whyer11) in [#1363](https://github.com/umijs/mako/pull/1363)
- New: CLI now checks if there are existing files in the current directory by [@liangchaofei](https://github.com/liangchaofei) in [#1368](https://github.com/umijs/mako/pull/1368)
- New: Support for selecting templates from the templates directory by [@kiner-tang](https://github.com/kiner-tang) in [#1370](https://github.com/umijs/mako/pull/1370)
- New: px2rem now supports selectorDoubleRemList by [@xiaohuoni](https://github.com/xiaohuoni) in [#1336](https://github.com/umijs/mako/pull/1336)
- New: Pass umi configuration to mako by [@xiaohuoni](https://github.com/xiaohuoni) in [#1394](https://github.com/umijs/mako/pull/1394)
- Improvement: More idiomatic and concise SWC AST generation by [@stormslowly](https://github.com/stormslowly) in [#1372](https://github.com/umijs/mako/pull/1372)
- Improvement: Clearer code logic and types by [@xusd320](https://github.com/xusd320) in [#1397](https://github.com/umijs/mako/pull/1397)
- Fix: Decoded paths for less plugin by [@stormslowly](https://github.com/stormslowly) in [#1360](https://github.com/umijs/mako/pull/1360)
- Fix: Stringifying object values causing panic by [@xusd320](https://github.com/xusd320) in [#1349](https://github.com/umijs/mako/pull/1349)
- Fix: HMR does not support React.lazy + import() components by [@sorrycc](https://github.com/sorrycc) in [#1369](https://github.com/umijs/mako/pull/1369)
- Fix: Corrected spelling mistakes by [@kiner-tang](https://github.com/kiner-tang) in [#1371](https://github.com/umijs/mako/pull/1371)
- Fix: pnpm installation issues by [@sorrycc](https://github.com/sorrycc) in [#1376](https://github.com/umijs/mako/pull/1376)
- Fix: Unstable entry hash by [@stormslowly](https://github.com/stormslowly) in [#1374](https://github.com/umijs/mako/pull/1374)
- Fix: analyze not working in umi by [@sorrycc](https://github.com/sorrycc) in [#1387](https://github.com/umijs/mako/pull/1387)
- Fix: Loss of CSS order after sorting dependencies alphabetically by [@xusd320](https://github.com/xusd320) in [#1391](https://github.com/umijs/mako/pull/1391)
- Fix: Should check reserved words after preset_env by [@Jinbao1001](https://github.com/Jinbao1001) in [#1367](https://github.com/umijs/mako/pull/1367)
- Fix: commonjs might lack use strict directive by [@Jinbao1001](https://github.com/Jinbao1001) in [#1386](https://github.com/umijs/mako/pull/1386)


## 0.7.4

`2024-07-02`
Expand Down
25 changes: 25 additions & 0 deletions CHANGELOG_zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@

## 0.7.5

`2024-07-11`

- 新增: 为 HMR 增加控制台警告,如果 React 被 external by [@PeachScript](https://github.com/PeachScript) in [#1354](https://github.com/umijs/mako/pull/1354)
- 新增: CLI 支持自定义项目名称 by [@kiner-tang](https://github.com/kiner-tang) in [#1340](https://github.com/umijs/mako/pull/1340)
- 新增: 升级 hyper-staticfile,修复 JS 文件字符集问题 by [@whyer11](https://github.com/whyer11) in [#1363](https://github.com/umijs/mako/pull/1363)
- 新增: CLI 增加判断当前目录是否存在文件 by [@liangchaofei](https://github.com/liangchaofei) in [#1368](https://github.com/umijs/mako/pull/1368)
- 新增: 支持从 templates 目录选择模板 by [@kiner-tang](https://github.com/kiner-tang) in [#1370](https://github.com/umijs/mako/pull/1370)
- 新增: px2rem 支持 selectorDoubleRemList by [@xiaohuoni](https://github.com/xiaohuoni) in [#1336](https://github.com/umijs/mako/pull/1336)
- 新增: 传递 umi 配置到 mako by [@xiaohuoni](https://github.com/xiaohuoni) in [#1394](https://github.com/umijs/mako/pull/1394)
- 优化: 更加惯用和简洁的 SWC AST 生成 by [@stormslowly](https://github.com/stormslowly) in [#1372](https://github.com/umijs/mako/pull/1372)
- 优化: 代码逻辑和类型更清晰 by [@xusd320](https://github.com/xusd320) in [#1397](https://github.com/umijs/mako/pull/1397)
- 修复: less 插件未解码路径 by [@stormslowly](https://github.com/stormslowly) in [#1360](https://github.com/umijs/mako/pull/1360)
- 修复: 字符串化对象值会导致 panic by [@xusd320](https://github.com/xusd320) in [#1349](https://github.com/umijs/mako/pull/1349)
- 修复: HMR 不支持 React.lazy + import() 组件 by [@sorrycc](https://github.com/sorrycc) in [#1369](https://github.com/umijs/mako/pull/1369)
- 修复: 拼写错误 by [@kiner-tang](https://github.com/kiner-tang) in [#1371](https://github.com/umijs/mako/pull/1371)
- 修复: pnpm 安装问题 by [@sorrycc](https://github.com/sorrycc) in [#1376](https://github.com/umijs/mako/pull/1376)
- 修复: entry 哈希不稳定 by [@stormslowly](https://github.com/stormslowly) in [#1374](https://github.com/umijs/mako/pull/1374)
- 修复: analyze 在 umi 中不起作用 by [@sorrycc](https://github.com/sorrycc) in [#1387](https://github.com/umijs/mako/pull/1387)
- 修复: 按字母顺序排序依赖项后丢失 CSS 顺序 by [@xusd320](https://github.com/xusd320) in [#1391](https://github.com/umijs/mako/pull/1391)
- 修复: 在 preset_env 之后应该检查保留字 by [@Jinbao1001](https://github.com/Jinbao1001) in [#1367](https://github.com/umijs/mako/pull/1367)
- 修复: commonjs 可能缺少 use strict 指令 by [@Jinbao1001](https://github.com/Jinbao1001) in [#1386](https://github.com/umijs/mako/pull/1386)

## 0.7.4

`2024-07-02`
Expand Down
12 changes: 4 additions & 8 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Plugin for JsPlugin {
"js_plugin"
}

fn build_start(&self, _context: &Arc<Context>) -> Result<Option<()>> {
fn build_start(&self, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.build_start {
let (tx, rx) = mpsc::channel::<napi::Result<()>>();
hook.call(
Expand All @@ -26,7 +26,7 @@ impl Plugin for JsPlugin {
rx.recv()
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?;
}
Ok(None)
Ok(())
}

fn load(&self, param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> {
Expand Down Expand Up @@ -64,11 +64,7 @@ impl Plugin for JsPlugin {
Ok(None)
}

fn generate_end(
&self,
param: &PluginGenerateEndParams,
_context: &Arc<Context>,
) -> Result<Option<()>> {
fn generate_end(&self, param: &PluginGenerateEndParams, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.generate_end {
let (tx, rx) = mpsc::channel::<napi::Result<()>>();
hook.call(
Expand All @@ -81,7 +77,7 @@ impl Plugin for JsPlugin {
rx.recv()
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?;
}
Ok(None)
Ok(())
}

fn before_write_fs(&self, path: &std::path::Path, content: &[u8]) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub struct BuildParams {
propWhiteList?: string[];
selectorBlackList?: string[];
selectorWhiteList?: string[];
selectorDoubleList?: string[];
};
stats?: boolean;
hash?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions crates/mako/src/build/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum ParseError {
UnsupportedServerAction { path: String },
#[error("The `\"{directive:?}\"` directive must be put at the top of the file.")]
DirectiveNotOnTop { directive: String },
#[error("{path}\n{message}")]
InvalidExpression { message: String, path: String },
}

pub struct Parse {}
Expand Down
2 changes: 2 additions & 0 deletions crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use swc_core::ecma::preset_env::{self as swc_preset_env};
use swc_core::ecma::transforms::base::feature::FeatureFlag;
use swc_core::ecma::transforms::base::fixer::paren_remover;
use swc_core::ecma::transforms::base::{resolver, Assumptions};
use swc_core::ecma::transforms::compat::reserved_words;
use swc_core::ecma::transforms::optimization::simplifier;
use swc_core::ecma::transforms::optimization::simplify::{dce, Config as SimpilifyConfig};
use swc_core::ecma::transforms::proposal::decorators;
Expand Down Expand Up @@ -177,6 +178,7 @@ impl Transform {
assumptions,
&mut FeatureFlag::default(),
)));
folders.push(Box::new(reserved_words::reserved_words()));
folders.push(Box::new(paren_remover(Default::default())));
// simplify, but keep top level dead code
// e.g. import x from 'foo'; but x is not used
Expand Down
9 changes: 8 additions & 1 deletion crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,15 @@ impl Compiler {

let mut config = config;

if config.experimental.require_context {
plugins.push(Arc::new(plugins::require_context::RequireContextPlugin {}))
}

if config.output.mode == OutputMode::Bundless {
plugins.insert(0, Arc::new(plugins::bundless_compiler::BundlessCompiler {}));
plugins.insert(
0,
Arc::new(plugins::bundless_compiler::BundlessCompilerPlugin {}),
);
}

if std::env::var("DEBUG_GRAPH").is_ok_and(|v| v == "true") {
Expand Down
6 changes: 5 additions & 1 deletion crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ pub struct Px2RemConfig {
pub selector_blacklist: Vec<String>,
#[serde(rename = "selectorWhiteList", default)]
pub selector_whitelist: Vec<String>,
#[serde(rename = "selectorDoubleList", default)]
pub selector_doublelist: Vec<String>,
#[serde(rename = "minPixelValue", default)]
pub min_pixel_value: f64,
}
Expand All @@ -257,6 +259,7 @@ impl Default for Px2RemConfig {
prop_whitelist: vec![],
selector_blacklist: vec![],
selector_whitelist: vec![],
selector_doublelist: vec![],
min_pixel_value: 0.0,
}
}
Expand Down Expand Up @@ -426,6 +429,7 @@ pub struct RscClientConfig {
#[serde(rename_all = "camelCase")]
pub struct ExperimentalConfig {
pub webpack_syntax_validate: Vec<String>,
pub require_context: bool,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -697,7 +701,7 @@ const DEFAULT_CONFIG: &str = r#"
"inlineCSS": false,
"rscServer": false,
"rscClient": false,
"experimental": { "webpackSyntaxValidate": [] },
"experimental": { "webpackSyntaxValidate": [], requireContext: true},
"useDefineForClassFields": true,
"watch": { "ignorePaths": [] },
"devServer": { "host": "127.0.0.1", "port": 3000 }
Expand Down
13 changes: 8 additions & 5 deletions crates/mako/src/generate/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::fs;
use std::sync::Arc;
use std::path::Path;

use anyhow::Result;

use crate::compiler::Context;
use crate::stats::StatsJsonMap;

pub struct Analyze {}

impl Analyze {
pub fn write_analyze(stats: &StatsJsonMap, context: Arc<Context>) -> Result<()> {
pub fn write_analyze(stats: &StatsJsonMap, path: &Path) -> Result<()> {
let stats_json = serde_json::to_string_pretty(&stats).unwrap();
let html_str = format!(
r#"<!DOCTYPE html>
Expand All @@ -31,8 +30,12 @@ impl Analyze {
stats_json,
include_str!("../../../../client/dist/index.js").replace("</script>", "<\\/script>")
);
let report_path = context.config.output.path.join("report.html");
fs::write(report_path, html_str).unwrap();
let report_path = path.join("analyze-report.html");
fs::write(&report_path, html_str).unwrap();
println!(
"Analyze report generated at: {}",
report_path.to_string_lossy()
);
Ok(())
}
}
8 changes: 2 additions & 6 deletions crates/mako/src/generate/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,11 @@ impl ChunkGraph {

pub fn sync_dependencies_chunk(&self, chunk_id: &ChunkId) -> Vec<ChunkId> {
let idx = self.id_index_map.get(chunk_id).unwrap();
let mut chunk_ids = self
.graph
self.graph
.neighbors_directed(*idx, Direction::Outgoing)
.filter(|idx| matches!(self.graph[*idx].chunk_type, ChunkType::Sync))
.map(|idx| self.graph[idx].id.clone())
.collect::<Vec<ChunkId>>();

chunk_ids.sort_by(|id1, id2| id1.id.cmp(&id2.id));
chunk_ids
.collect::<Vec<ChunkId>>()
}

pub fn entry_dependencies_chunk(&self, chunk_id: &ChunkId) -> Vec<ChunkId> {
Expand Down
21 changes: 11 additions & 10 deletions crates/mako/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::config::{DevtoolConfig, OutputMode, TreeShakingStrategy};
use crate::dev::update::UpdateResult;
use crate::generate::generate_chunks::{ChunkFile, ChunkFileType};
use crate::module::{Dependency, ModuleId};
use crate::plugins::bundless_compiler::BundlessCompiler;
use crate::stats::{create_stats_info, print_stats, write_stats};
use crate::utils::base64_encode;
use crate::visitors::async_module::mark_async;
Expand All @@ -48,8 +49,9 @@ struct ChunksUrlMap {
}

impl Compiler {
fn generate_with_plugin_driver(&self) -> Result<()> {
self.context.plugin_driver.generate(&self.context)?;
fn generate_bundless(&self) -> Result<()> {
let bundless_compiler = BundlessCompiler::new(self.context.clone());
bundless_compiler.generate()?;

let stats = create_stats_info(0, self);

Expand Down Expand Up @@ -114,9 +116,8 @@ impl Compiler {
}
let t_tree_shaking = t_tree_shaking.elapsed();

// TODO: improve this hardcode
if self.context.config.output.mode == OutputMode::Bundless {
return self.generate_with_plugin_driver();
return self.generate_bundless();
}

let t_group_chunks = Instant::now();
Expand Down Expand Up @@ -182,12 +183,8 @@ impl Compiler {
// generate stats
let stats = create_stats_info(0, self);

if self.context.config.analyze.is_some() {
Analyze::write_analyze(&stats, self.context.clone())?;
}

if self.context.config.stats.is_some() {
write_stats(&stats, self);
write_stats(&stats, &self.context.config.output.path);
}

// build_success hook
Expand All @@ -200,6 +197,10 @@ impl Compiler {
print_stats(self);
}

if self.context.config.analyze.is_some() {
Analyze::write_analyze(&stats, &self.context.config.output.path)?;
}

debug!("generate done in {}ms", t_generate.elapsed().as_millis());
debug!(" - tree shaking: {}ms", t_tree_shaking.as_millis());
debug!(" - group chunks: {}ms", t_group_chunks.as_millis());
Expand Down Expand Up @@ -351,7 +352,7 @@ impl Compiler {
// ref: https://github.com/umijs/mako/issues/1107
if self.context.config.stats.is_some() {
let stats = create_stats_info(0, self);
write_stats(&stats, self);
write_stats(&stats, &self.context.config.output.path);
}

let t_generate = t_generate.elapsed();
Expand Down
27 changes: 4 additions & 23 deletions crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ use swc_core::common::errors::HANDLER;
use swc_core::common::GLOBALS;
use swc_core::css::ast;
use swc_core::css::visit::VisitMutWith as CSSVisitMutWith;
use swc_core::ecma::transforms::base::feature::FeatureFlag;
use swc_core::ecma::transforms::base::fixer::fixer;
use swc_core::ecma::transforms::base::helpers::{inject_helpers, Helpers, HELPERS};
use swc_core::ecma::transforms::base::hygiene;
use swc_core::ecma::transforms::base::hygiene::hygiene_with_config;
use swc_core::ecma::transforms::module::common_js;
use swc_core::ecma::transforms::module::import_analysis::import_analyzer;
use swc_core::ecma::transforms::module::util::{Config, ImportInterop};
use swc_core::ecma::transforms::module::util::ImportInterop;
use swc_core::ecma::visit::VisitMutWith;
use swc_error_reporters::handler::try_with_handler;
use tracing::debug;
Expand All @@ -26,6 +24,7 @@ use crate::compiler::{Compiler, Context};
use crate::module::{Dependency, ModuleAst, ModuleId, ModuleType, ResolveType};
use crate::utils::thread_pool;
use crate::visitors::async_module::{mark_async, AsyncModule};
use crate::visitors::common_js::common_js;
use crate::visitors::css_imports::CSSImports;
use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace};
use crate::visitors::dynamic_import::DynamicImport;
Expand Down Expand Up @@ -197,27 +196,9 @@ pub fn transform_js_generate(transform_js_param: TransformJsParam) -> Result<()>
ast.ast.visit_mut_with(&mut inject_helpers(unresolved_mark));

ast.ast.visit_mut_with(&mut common_js(
context.clone(),
unresolved_mark,
Config {
import_interop: Some(import_interop),
// NOTE: 这里后面要调整为注入自定义require
ignore_dynamic: true,
preserve_import_meta: true,
// TODO: set to false when esm
allow_top_level_this: true,
strict_mode: false,
..Default::default()
},
FeatureFlag::empty(),
Some(
context
.meta
.script
.origin_comments
.read()
.unwrap()
.get_swc_comments(),
),
import_interop,
));

ast.ast.visit_mut_with(&mut OptimizeDefineUtils {
Expand Down
Loading

0 comments on commit 4d1b64e

Please sign in to comment.