Skip to content

Commit

Permalink
initial work for compile-time evaluation (#3495)
Browse files Browse the repository at this point in the history
Allows to skip unreachable code based on compile-time constant
conditions

e. g.

```
if (!process.turbopack) {
  require("will-not-be-processed");
}
```

* add if() and process.turbopack fixes WEB-491
* Removes unreachable code fixes WEB-498
* evaluate some logical operations `&&`, `||`, `??`, `!`
* fix arguments handling fixes WEB-529
* nested effects for function called with closures
* handle closures when array methods are used fixes WEB-538
* evaluates `process.turbopack` fixes WEB-496
  • Loading branch information
sokra authored Feb 3, 2023
1 parent 5b7c53f commit 4f66bf6
Show file tree
Hide file tree
Showing 127 changed files with 104,729 additions and 65,655 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ crates/next-core/js/src/compiled
crates/turbopack-node/js/src/compiled
crates/turbopack/bench.json
crates/turbopack/tests
crates/turbopack-ecmascript/tests/analyzer/graph
crates/next-transform-strip-page-exports/tests
crates/next-transform-dynamic/tests
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
it("importing a not existing file should throw", () => {
// This is a check to make sure that the following tests would fail if they require("fail")
expect(() => {
require("./not-existing-file");
}).toThrow();
});

function maybeReturn(x) {
if (x) {
return true;
}
}

function func() {
if (false) {
require("fail");
import("fail");
}
if (true) {
require("./ok");
}
if (true) {
require("./ok");
} else {
require("fail");
import("fail");
}
if (false) {
require("fail");
import("fail");
} else {
require("./ok");
}
}

it("should not follow conditional references", () => {
func();

expect(func.toString()).not.toContain("import(");
});

it("should allow replacements in IIFEs", () => {
(function func() {
if (false) {
require("fail");
import("fail");
}
})();
});

it("should support functions that only sometimes return", () => {
let ok = false;
if (maybeReturn(true)) {
ok = true;
}
expect(ok).toBe(true);
});

it("should evaluate process.turbopack", () => {
let ok = false;
if (process.turbopack) {
ok = true;
} else {
require("fail");
import("fail");
}
expect(ok).toBe(true);
});

it("should evaluate !process.turbopack", () => {
if (!process.turbopack) {
require("fail");
import("fail");
}
});

// it("should evaluate NODE_ENV", () => {
// if (process.env.NODE_ENV !== "development") {
// require("fail");
// import("fail");
// }
// });
Empty file.
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/reference_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

// These enums list well known types, which we use internally. Plugins might add
// These enums list well-known types, which we use internally. Plugins might add
// custom types too.

// TODO when plugins are supported, replace u8 with a trait that defines the
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-create-test-app/src/test_app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl TestAppBuilder {
};

#[cfg(windows)]
let relative_effect = relative_effect.replace("\\", "/");
let relative_effect = relative_effect.replace('\\', "/");

formatdoc! {r#"
{SETUP_IMPORTS}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-dev-server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn process_request_with_content_source(

let header_map = response.headers_mut().expect("headers must be defined");

for (header_name, header_value) in &*headers {
for (header_name, header_value) in headers {
header_map.append(
HeaderName::try_from(header_name.clone())?,
hyper::header::HeaderValue::try_from(header_value.as_str())?,
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-dev-server/src/source/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub async fn resolve_source_request(
ContentSourceResult::NeedData(needed) => {
current_source = needed.source.resolve().await?;
current_asset_path = needed.path.clone();
data = request_to_data(&mut request_overwrites, &needed.vary).await?;
data = request_to_data(&request_overwrites, &needed.vary).await?;
}
ContentSourceResult::Result { get_content, .. } => {
let content_vary = get_content.vary().await?;
let content_data = request_to_data(&mut request_overwrites, &content_vary).await?;
let content_data = request_to_data(&request_overwrites, &content_vary).await?;
let content = get_content.get(Value::new(content_data));
match &*content.await? {
ContentSourceContent::Rewrite(rewrite) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/turbopack-ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ lazy_static = "1.4.0"
next-font = { path = "../next-font" }
next-transform-dynamic = { path = "../next-transform-dynamic" }
next-transform-strip-page-exports = { path = "../next-transform-strip-page-exports" }
num-bigint = "0.4"
num-traits = "0.2.15"
once_cell = "1.13.0"
parking_lot = "0.12.1"
pin-project-lite = "0.2.9"
regex = "1.5.4"
serde = "1.0.136"
Expand Down Expand Up @@ -58,9 +61,6 @@ swc_core = { workspace = true, features = [
"base",
] }

[dependencies.num-bigint]
version = "0.4"

[dev-dependencies]
criterion = { version = "0.3.5", features = ["async_tokio"] }
rstest = "0.12.0"
Expand Down
41 changes: 16 additions & 25 deletions crates/turbopack-ecmascript/benches/analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
fs,
path::PathBuf,
sync::{Arc, Mutex},
time::Duration,
};
use std::{fs, path::PathBuf, sync::Arc, time::Duration};

use criterion::{Bencher, BenchmarkId, Criterion};
use swc_core::{
Expand All @@ -23,8 +18,8 @@ use turbopack_core::{
};
use turbopack_ecmascript::analyzer::{
graph::{create_graph, EvalContext, VarGraph},
linker::{link, LinkCache},
test_utils::visitor,
linker::link,
test_utils::{early_visitor, visitor},
};

pub fn benchmark(c: &mut Criterion) {
Expand Down Expand Up @@ -95,28 +90,24 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
.unwrap();

b.to_async(rt).iter(|| async {
let cache = Mutex::new(LinkCache::new());
for val in input.var_graph.values.values() {
VcStorage::with(async {
let env = EnvironmentVc::new(
Value::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
compile_target: CompileTargetVc::unknown(),
..Default::default()
}
.into(),
)),
Value::new(EnvironmentIntention::ServerRendering),
);
link(
&input.var_graph,
val.clone(),
&(|val| {
Box::pin(visitor(
val,
EnvironmentVc::new(
Value::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
compile_target: CompileTargetVc::unknown(),
..Default::default()
}
.into(),
)),
Value::new(EnvironmentIntention::ServerRendering),
),
))
}),
&cache,
&early_visitor,
&(|val| visitor(val, env)),
Default::default(),
)
.await
})
Expand Down
Loading

0 comments on commit 4f66bf6

Please sign in to comment.