Skip to content

Commit

Permalink
fix: compile time evaluation of require.ensure (#8242)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Oct 28, 2024
1 parent 8243df6 commit df8ca34
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
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 () { });
});

2 comments on commit df8ca34

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-10-28 0db8b94) Current Change
10000_development-mode + exec 2.13 s ± 34 ms 2.13 s ± 27 ms -0.04 %
10000_development-mode_hmr + exec 674 ms ± 22 ms 666 ms ± 5.9 ms -1.16 %
10000_production-mode + exec 2.7 s ± 47 ms 2.71 s ± 50 ms +0.59 %
arco-pro_development-mode + exec 1.78 s ± 72 ms 1.79 s ± 84 ms +0.53 %
arco-pro_development-mode_hmr + exec 428 ms ± 2.2 ms 429 ms ± 7.5 ms +0.29 %
arco-pro_production-mode + exec 3.21 s ± 53 ms 3.21 s ± 68 ms 0.00 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.22 s ± 58 ms 3.22 s ± 49 ms -0.04 %
threejs_development-mode_10x + exec 1.62 s ± 22 ms 1.62 s ± 16 ms +0.13 %
threejs_development-mode_10x_hmr + exec 759 ms ± 9.9 ms 771 ms ± 12 ms +1.50 %
threejs_production-mode_10x + exec 5.01 s ± 29 ms 5.01 s ± 40 ms +0.05 %

Please sign in to comment.