Skip to content

Commit

Permalink
Implement typeof window inlining for Turbopack (#66128)
Browse files Browse the repository at this point in the history
Update: Implemented a feature in Turbopack for `typeof window` inlining:
vercel/turborepo#8211. Verified the changes in that
PR + this PR fix the test 💯

Turbopack upgrade:

* vercel/turborepo#8081 <!-- Alexander Lyon - add
support for upload speed / remaining in the cache upload step -->
* vercel/turborepo#7673 <!-- Alexander Lyon -
[turbo-trace] add additional filter modes for allocs over time -->
* vercel/turborepo#8191 <!-- Will Binns-Smith -
Turbopack: Register react refresh exports in module factory -->
* vercel/turborepo#8195 <!-- Donny/강동윤 - feat:
Remove lint for `grid-template-areas`" -->
* vercel/turborepo#8207 <!-- Benjamin Woodruff -
chore: Remove unused `base16` dependency and dead `hex` wrapper function
-->
* vercel/turborepo#8185 <!-- Tim Neutkens - Update
contributing guide to mention snapshot test updating -->
* vercel/turborepo#8211 <!-- Tim Neutkens - Add
option for inlining typeof window -->
* vercel/turborepo#8214 <!-- Will Binns-Smith -
Revert "Turbopack: Register react refresh exports in module factory
(#8191)" -->

---

Previous PR description::

Implements a failing test for issue #66058.
The test only fails when using Turbopack.

While digging into that issue I found that `typeof window` replacement
is either not applied or does not cause `import()` or `require()` to be
removed. In the reproduction that approach is used to load a particular
package only in Node.js and another only in the browser.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

Closes NEXT-
Fixes #

-->
  • Loading branch information
timneutkens authored and ForsakenHarmony committed Aug 15, 2024
1 parent 6ac4115 commit d40efa9
Show file tree
Hide file tree
Showing 15 changed files with 516 additions and 644 deletions.
382 changes: 85 additions & 297 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ members = [
"packages/next-swc/crates/napi",
"packages/next-swc/crates/wasm",
"packages/next-swc/crates/next-api",
"packages/next-swc/crates/next-build-test",
"packages/next-swc/crates/next-build",
"packages/next-swc/crates/next-core",
"packages/next-swc/crates/next-custom-transforms",
Expand All @@ -23,11 +22,6 @@ debug-assertions = false
[profile.dev.build-override]
opt-level = 3

[profile.release-with-debug]
inherits = "release"
debug = true
lto = "thin"

[workspace.dependencies]
# Workspace crates
next-api = { path = "packages/next-swc/crates/next-api" }
Expand All @@ -43,12 +37,11 @@ swc_core = { version = "0.92.5", features = [
testing = { version = "0.35.24" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240521.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240526.2" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240521.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240526.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240521.2" }
turbo-tasks-malloc = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240521.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240526.2" }

# General Deps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use turbopack_binding::{
condition::ContextCondition,
module_options::{
module_options_context::ModuleOptionsContext, JsxTransformOptions, ModuleRule,
TypescriptTransformOptions,
TypeofWindow, TypescriptTransformOptions,
},
resolve_options_context::ResolveOptionsContext,
},
Expand Down Expand Up @@ -270,6 +270,7 @@ pub async fn get_client_module_options_context(
let enable_foreign_postcss_transform = Some(postcss_foreign_transform_options.cell());

let module_options_context = ModuleOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Object),
preset_env_versions: Some(env),
execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
Expand All @@ -280,6 +281,7 @@ pub async fn get_client_module_options_context(

// node_modules context
let foreign_codes_options_context = ModuleOptionsContext {
enable_typeof_window_inlining: None,
enable_webpack_loaders: foreign_enable_webpack_loaders,
enable_postcss_transform: enable_foreign_postcss_transform,
custom_rules: foreign_next_client_rules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use turbopack_binding::{
turbopack::{
condition::ContextCondition,
module_options::{
JsxTransformOptions, ModuleOptionsContext, ModuleRule, TypescriptTransformOptions,
JsxTransformOptions, ModuleOptionsContext, ModuleRule, TypeofWindow,
TypescriptTransformOptions,
},
resolve_options_context::ResolveOptionsContext,
transition::Transition,
Expand Down Expand Up @@ -461,6 +462,7 @@ pub async fn get_server_module_options_context(
let styled_jsx_transform_rule = get_styled_jsx_transform_rule(next_config, versions).await?;

let module_options_context = ModuleOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Undefined),
execution_context: Some(execution_context),
use_swc_css,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
Expand Down Expand Up @@ -509,6 +511,7 @@ pub async fn get_server_module_options_context(
};

let foreign_code_module_options_context = ModuleOptionsContext {
enable_typeof_window_inlining: None,
custom_rules: foreign_next_server_rules.clone(),
enable_webpack_loaders: foreign_enable_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
Expand Down Expand Up @@ -563,6 +566,7 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(source_transform_rules);

let foreign_code_module_options_context = ModuleOptionsContext {
enable_typeof_window_inlining: None,
custom_rules: foreign_next_server_rules.clone(),
enable_webpack_loaders: foreign_enable_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
Expand Down
9 changes: 5 additions & 4 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"@opentelemetry/api": "^1.1.0",
"@playwright/test": "^1.41.2",
"babel-plugin-react-compiler": "*",
"react": "19.0.0-beta-04b058868c-20240508",
"react-dom": "19.0.0-beta-04b058868c-20240508",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"sass": "^1.3.0"
},
"peerDependenciesMeta": {
Expand All @@ -124,7 +124,7 @@
}
},
"optionalDependencies": {
"sharp": "^0.33.3"
"sharp": "^0.33.4"
},
"devDependencies": {
"@ampproject/toolbox-optimizer": "2.8.3",
Expand Down Expand Up @@ -206,7 +206,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.27.1",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240517.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240526.2",
"acorn": "8.11.3",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
Expand Down Expand Up @@ -271,6 +271,7 @@
"ora": "4.0.4",
"os-browserify": "0.3.0",
"p-limit": "3.1.0",
"p-queue": "6.6.2",
"path-browserify": "1.0.1",
"path-to-regexp": "6.1.0",
"picomatch": "4.0.1",
Expand Down
Loading

0 comments on commit d40efa9

Please sign in to comment.