Skip to content

Commit

Permalink
feat: add require.main (#5209)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jan 4, 2024
1 parent 64939a7 commit 75ad63f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
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.

0 comments on commit 75ad63f

Please sign in to comment.