Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: treats ignore module as normal module #935

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions crates/mako/src/analyze_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub enum AnalyzeDepsError {
pub struct AnalyzeDepsResult {
pub resolved_deps: Vec<ResolvedDep>,
pub missing_deps: Vec<Dependency>,
pub ignored_deps: Vec<Dependency>,
}

#[derive(Debug, Clone)]
Expand All @@ -46,7 +45,6 @@ impl AnalyzeDeps {

let mut resolved_deps = vec![];
let mut missing_deps = vec![];
let mut ignored_deps = vec![];
let path = file.path.to_str().unwrap();
for dep in deps {
let result = resolve(
Expand All @@ -58,14 +56,10 @@ impl AnalyzeDeps {
);
match result {
Ok(resolver_resource) => {
if matches!(resolver_resource, ResolverResource::Ignored) {
ignored_deps.push(dep);
} else {
resolved_deps.push(ResolvedDep {
resolver_resource,
dependency: dep,
});
}
resolved_deps.push(ResolvedDep {
resolver_resource,
dependency: dep,
});
}
Err(_err) => {
missing_deps.push(dep);
Expand Down Expand Up @@ -93,7 +87,6 @@ impl AnalyzeDeps {
Ok(AnalyzeDepsResult {
resolved_deps,
missing_deps,
ignored_deps,
})
}

Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/ast_2/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl File {
// TODO: remove this specific logic
params.iter().any(|(k, _)| k == "asmodule");
let is_under_node_modules = path.to_string_lossy().contains("node_modules");
let extname = PathBuf::from(pathname.clone())
let extname = pathname
.clone()
.extension()
.map(|ext| ext.to_string_lossy().to_string())
.unwrap_or_default();
Expand Down
53 changes: 42 additions & 11 deletions crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,26 @@ impl Compiler {
// handle deps
for dep in resolved_deps {
let path = dep.resolver_resource.get_resolved_path();
let is_external = dep.resolver_resource.get_external().is_some();
let dep_module_id = ModuleId::new(path.clone());
if !module_graph.has_module(&dep_module_id) {
let module = if is_external {
Self::create_external_module(&dep.resolver_resource, self.context.clone())
} else {
Self::create_empty_module(&dep_module_id)
let module = match dep.resolver_resource {
ResolverResource::Resolved(_) => {
count += 1;

let file = File::new(path.clone(), self.context.clone());
build_with_pool(file, Some(dep.resolver_resource.clone()));

Self::create_empty_module(&dep_module_id)
}
ResolverResource::External(_) => Self::create_external_module(
&dep.resolver_resource,
self.context.clone(),
),
ResolverResource::Ignored(_) => {
Self::create_ignored_module(&path, self.context.clone())
}
};
if !is_external {
count += 1;
let file = File::new(path.clone(), self.context.clone());
build_with_pool(file, Some(dep.resolver_resource.clone()));
}

// 拿到依赖之后需要直接添加 module 到 module_graph 里,不能等依赖 build 完再添加
// 是因为由于是异步处理各个模块,后者会导致大量重复任务的 build_module 任务(3 倍左右)
module_ids.insert(module.id.clone());
Expand Down Expand Up @@ -179,6 +186,31 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
Ok(Module::new(module_id, false, Some(info)))
}

fn create_ignored_module(path: &str, context: Arc<Context>) -> Module {
let module_id = ModuleId::new(path.to_owned());

let mut module = Module::new(module_id, false, None);

let info = {
let file = File::with_content(
path.to_owned(),
Content::Js("".to_string()),
context.clone(),
);
let ast = Parse::parse(&file, context.clone()).unwrap();

ModuleInfo {
file,
ast,
..Default::default()
}
};

module.add_info(Some(info));

module
}

pub fn create_empty_module(module_id: &ModuleId) -> Module {
Module::new(module_id.clone(), false, None)
}
Expand Down Expand Up @@ -250,7 +282,6 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
path,
raw,
missing_deps: HashMap::new(),
ignored_deps: vec![],
import_map: vec![],
export_map: vec![],
is_barrel: false,
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct Load {}

impl Load {
pub fn load(file: &File, context: Arc<Context>) -> Result<Content> {
mako_core::mako_profile_function!(file.path.to_str());
mako_core::mako_profile_function!(file.path.to_string_lossy());
debug!("load: {:?}", file);

// plugin first
Expand Down
2 changes: 0 additions & 2 deletions crates/mako/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub struct ModuleInfo {
pub raw: String,
pub raw_hash: u64,
pub missing_deps: HashMap<String, Dependency>,
pub ignored_deps: Vec<String>,
/// Modules with top-level-await
pub top_level_await: bool,
/// The top-level-await module must be an async module, in addition, for example, wasm is also an async module
Expand All @@ -77,7 +76,6 @@ impl Default for ModuleInfo {
raw: "".to_string(),
raw_hash: 0,
missing_deps: HashMap::new(),
ignored_deps: vec![],
top_level_await: false,
is_async: false,
resolved_resource: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Parse {}

impl Parse {
pub fn parse(file: &File, context: Arc<Context>) -> Result<ModuleAst> {
mako_core::mako_profile_function!(file.path);
mako_core::mako_profile_function!(file.path.to_string_lossy());

// plugin first
let ast = context
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Parse {
if is_asmodule {
let mut file = file.clone();
file.set_content(Content::Js(CssAst::generate_css_modules_exports(
&file.pathname.to_string_lossy().to_string(),
&file.pathname.to_string_lossy(),
&mut ast.ast,
context.config.css_modules_export_only_locales,
)));
Expand Down
1 change: 0 additions & 1 deletion crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ pub fn transform_modules(module_ids: Vec<ModuleId>, context: &Arc<Context>) -> R
let deps_to_replace = DependenciesToReplace {
resolved: resolved_deps,
missing: info.missing_deps.clone(),
ignored: vec![],
};

if let ModuleAst::Script(ast) = ast {
Expand Down
3 changes: 1 addition & 2 deletions crates/mako/src/plugins/context_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::path::PathBuf;
use std::sync::Arc;

use mako_core::anyhow::Result;
Expand Down Expand Up @@ -32,7 +31,7 @@ impl Plugin for ContextModulePlugin {
.find_map(|(k, v)| k.eq("glob").then_some(v)),
param.file.path.is_dir(),
) {
let glob_pattern = PathBuf::from(param.file.pathname.clone()).join(glob_pattern);
let glob_pattern = param.file.pathname.clone().join(glob_pattern);
let paths = glob(glob_pattern.to_str().unwrap())?;
let mut key_values = vec![];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ mod tests {
raw: "".to_string(),
raw_hash: 0,
missing_deps: Default::default(),
ignored_deps: vec![],
top_level_await: false,
is_async: false,
resolved_resource: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ fn do_resolve(
}
}
Err(oxc_resolve_err) => match oxc_resolve_err {
OxcResolveError::Ignored(_) => {
OxcResolveError::Ignored(path) => {
debug!("resolve ignored: {:?}", source);
Ok(ResolverResource::Ignored)
Ok(ResolverResource::Ignored(path))
}
_ => {
eprintln!(
Expand Down
10 changes: 6 additions & 4 deletions crates/mako/src/resolve/resource.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use oxc_resolver::Resolution;

#[derive(Debug, Clone)]
Expand All @@ -14,7 +16,7 @@ pub struct ResolvedResource(pub Resolution);
pub enum ResolverResource {
External(ExternalResource),
Resolved(ResolvedResource),
Ignored,
Ignored(PathBuf),
}

impl ResolverResource {
Expand All @@ -24,21 +26,21 @@ impl ResolverResource {
ResolverResource::Resolved(ResolvedResource(resolution)) => {
resolution.full_path().to_string_lossy().to_string()
}
ResolverResource::Ignored => "".to_string(),
ResolverResource::Ignored(path) => path.to_string_lossy().to_string(),
}
}
pub fn get_external(&self) -> Option<String> {
match self {
ResolverResource::External(ExternalResource { external, .. }) => Some(external.clone()),
ResolverResource::Resolved(_) => None,
ResolverResource::Ignored => None,
ResolverResource::Ignored(_) => None,
}
}
pub fn get_script(&self) -> Option<String> {
match self {
ResolverResource::External(ExternalResource { script, .. }) => script.clone(),
ResolverResource::Resolved(_) => None,
ResolverResource::Ignored => None,
ResolverResource::Ignored(_) => None,
}
}
}
1 change: 0 additions & 1 deletion crates/mako/src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub fn create_mock_module(path: PathBuf, code: &str) -> Module {
raw_hash: 0,
resolved_resource: None,
missing_deps: HashMap::new(),
ignored_deps: vec![],
top_level_await: false,
is_async: false,
source_map_chain: vec![],
Expand Down
1 change: 0 additions & 1 deletion crates/mako/src/transform_in_generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ pub fn transform_modules_in_thread(
let deps_to_replace = DependenciesToReplace {
resolved: resolved_deps,
missing: info.missing_deps.clone(),
ignored: info.ignored_deps.clone(),
};
if let ModuleAst::Script(mut ast) = ast {
let ret = transform_js_generate(TransformJsParam {
Expand Down
23 changes: 6 additions & 17 deletions crates/mako/src/transformers/transform_dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub struct DependenciesToReplace {
// e.g. "react" => ("hashed_id", "/abs/to/react/index.js")
pub resolved: HashMap<String, (String, String)>,
pub missing: HashMap<String, Dependency>,
pub ignored: Vec<String>,
}

pub fn miss_throw_stmt<T: AsRef<str>>(source: T) -> Expr {
Expand Down Expand Up @@ -179,18 +178,12 @@ impl VisitMut for DepReplacer<'_> {

impl DepReplacer<'_> {
fn replace_source(&mut self, source: &mut Str) {
let to_replace =
if let Some(replacement) = self.to_replace.resolved.get(&source.value.to_string()) {
replacement.0.clone()
} else if self.to_replace.ignored.contains(&source.value.to_string()) {
"$$IGNORED$$".to_string()
} else {
return;
};

let span = source.span;
*source = Str::from(to_replace);
source.span = span;
if let Some(replacement) = self.to_replace.resolved.get(&source.value.to_string()) {
let module_id = replacement.0.clone();
let span = source.span;
*source = Str::from(module_id);
source.span = span;
}
}
}

Expand Down Expand Up @@ -300,7 +293,6 @@ mod tests {
)
},
missing: HashMap::new(),
ignored: vec![],
};

let cloned = context.clone();
Expand Down Expand Up @@ -349,7 +341,6 @@ mod tests {
span: None,
order: 0,
}},
ignored: vec![],
};

let cloned = context.clone();
Expand Down Expand Up @@ -395,7 +386,6 @@ mod tests {
span: None,
order: 0,
}},
ignored: vec![],
};

let cloned = context.clone();
Expand Down Expand Up @@ -451,7 +441,6 @@ mod tests {
)
},
missing: hashmap! {},
ignored: vec![],
},
context: &context,
unresolved_mark,
Expand Down
1 change: 0 additions & 1 deletion crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ function createRuntime(makoModules, entryModuleId) {
var modulesRegistry = {};

function requireModule(moduleId) {
if (moduleId === '$$IGNORED$$') return {};
var cachedModule = modulesRegistry[moduleId];

if (cachedModule !== undefined) {
Expand Down
9 changes: 7 additions & 2 deletions e2e/fixtures/resolve.ignored/expect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const assert = require("assert");
const { parseBuildResult, trim, moduleReg } = require("../../../scripts/test-utils");
const { parseBuildResult } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

const content = files["index.js"];

assert(content.includes(`__mako_require__("$$IGNORED$$")`), `should contain __mako_require__("$$IGNORED$$")`);
assert(
content.includes(
`/*./node_modules/ignored/index.js*/ "node_modules/ignored/index.js": function(module, exports, __mako_require__) {}`
),
`ignored module should compile to empty module`
);
Loading