-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial work for compile-time evaluation (#3495)
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
Showing
127 changed files
with
104,729 additions
and
65,655 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
crates/next-dev-tests/tests/integration/turbopack/basic/comptime/input/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.