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

feat: add require.main #5209

Merged
merged 1 commit into from
Jan 4, 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rspack_core::{
DependencyLocation, DependencyTemplate, RuntimeGlobals, RuntimeRequirementsDependency,
ConstDependency, DependencyLocation, DependencyTemplate, RuntimeGlobals,
RuntimeRequirementsDependency, SpanExt,
};
use swc_core::common::SyntaxContext;
use swc_core::common::{Spanned, SyntaxContext};
use swc_core::ecma::ast::{Expr, Ident};
use swc_core::ecma::visit::{noop_visit_type, Visit, VisitWith};

Expand Down Expand Up @@ -63,6 +64,24 @@ impl Visit for CommonJsScanner<'_> {
RuntimeGlobals::MODULE_LOADED,
)));
}
if expr_matcher::is_require_main(expr) {
let mut runtime_requirements = RuntimeGlobals::default();
runtime_requirements.insert(RuntimeGlobals::MODULE_CACHE);
runtime_requirements.insert(RuntimeGlobals::ENTRY_MODULE_ID);
self
.presentational_dependencies
.push(Box::new(ConstDependency::new(
expr.span().real_lo(),
expr.span().real_hi(),
format!(
"{}[{}]",
RuntimeGlobals::MODULE_CACHE,
RuntimeGlobals::ENTRY_MODULE_ID
)
.into(),
Some(runtime_requirements),
)));
}
expr.visit_children_with(self);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ pub(crate) mod expr_matcher {
// - Matching would ignore Span and SyntaxContext
define_expr_matchers!({
is_require: "require",
is_require_main: "require.main",
is_require_context: "require.context",
is_require_resolve: "require.resolve",
is_require_resolve_weak: "require.resolveWeak",
is_require_cache: "require.cache",
is_module_hot_accept: "module.hot.accept",
is_module_hot_decline: "module.hot.decline",
is_module_hot: "module.hot",
is_module_id: "module.id",
is_module_loaded: "module.loaded",
is_module_exports: "module.exports",
is_module_require: "module.require",
is_require_cache: "require.cache",
is_webpack_module_id: "__webpack_module__.id",
is_import_meta_webpack_hot: "import.meta.webpackHot",
is_import_meta_webpack_hot_accept: "import.meta.webpackHot.accept",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading