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

fix: compile time evaluation of require.ensure #8242

Merged
merged 1 commit into from
Oct 28, 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
Expand Up @@ -2,23 +2,58 @@ use std::borrow::Cow;

use either::Either;
use rspack_core::{
AsyncDependenciesBlock, BoxDependency, ChunkGroupOptions, DependencyLocation, GroupOptions,
RealDependencyLocation,
AsyncDependenciesBlock, BoxDependency, ChunkGroupOptions, ConstDependency, DependencyLocation,
GroupOptions, RealDependencyLocation, SpanExt,
};
use swc_core::{
common::Spanned,
ecma::ast::{ArrowExpr, BlockStmtOrExpr, CallExpr, Expr, FnExpr},
ecma::ast::{ArrowExpr, BlockStmtOrExpr, CallExpr, Expr, FnExpr, UnaryExpr},
};

use super::JavascriptParserPlugin;
use crate::{
dependency::{RequireEnsureDependency, RequireEnsureItemDependency},
utils::eval::{self, BasicEvaluatedExpression},
visitors::{expr_matcher::is_require_ensure, JavascriptParser, Statement},
};

pub struct RequireEnsureDependenciesBlockParserPlugin;

impl JavascriptParserPlugin for RequireEnsureDependenciesBlockParserPlugin {
fn evaluate_typeof(
&self,
_parser: &mut JavascriptParser,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
(for_name == "require.ensure").then(|| {
eval::evaluate_to_string(
"function".to_string(),
expr.span.real_lo(),
expr.span.real_hi(),
)
})
}

fn r#typeof(
&self,
parser: &mut JavascriptParser,
expr: &swc_core::ecma::ast::UnaryExpr,
for_name: &str,
) -> Option<bool> {
(for_name == "require.ensure").then(|| {
parser
.presentational_dependencies
.push(Box::new(ConstDependency::new(
expr.span().real_lo(),
expr.span.real_hi(),
"'function'".into(),
None,
)));
true
})
}

fn call(&self, parser: &mut JavascriptParser, expr: &CallExpr, _for_name: &str) -> Option<bool> {
if expr
.callee
Expand Down
10 changes: 5 additions & 5 deletions tests/webpack-test/cases/parsing/typeof/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ it("should answer typeof exports correctly", function () {
// it("should answer typeof require.include correctly", function () {
// expect(typeof require.include).toBe("function");
// });
// it("should answer typeof require.ensure correctly", function () {
// expect(typeof require.ensure).toBe("function");
// });
it("should answer typeof require.ensure correctly", function () {
expect(typeof require.ensure).toBe("function");
});
it("should answer typeof require.resolve correctly", function () {
expect(typeof require.resolve).toBe("function");
});
Expand All @@ -43,6 +43,6 @@ it("should not parse filtered stuff", function () {
if (typeof module != "object") module = require("fail");
if (typeof exports == "undefined") exports = require("fail");
// if (typeof require.include !== "function") require.include("fail");
// if (typeof require.ensure !== "function")
// require.ensure(["fail"], function () { });
if (typeof require.ensure !== "function")
require.ensure(["fail"], function () { });
});
Loading