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

Implement typeof window inlining for Turbopack #66128

Merged
merged 7 commits into from
May 26, 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
305 changes: 46 additions & 259 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
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
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-240521.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
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

7 changes: 7 additions & 0 deletions test/e2e/app-dir/typeof-window/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/typeof-window/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client'
if (typeof window !== 'undefined') {
import('my-differentiated-files/browser').then((mod) => {
console.log({ TEST: mod.default })
})
}

export default function Page() {
return <h1>Page loaded</h1>
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'BROWSER'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'SERVER'
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "my-differentiated-files",
"type": "module",
"exports": {
"./browser": {
"browser": "./my-file-browser.js",
"node": null
},
"./server": {
"browser": null,
"node": "./my-file-server.js"
}
}
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/typeof-window/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
6 changes: 6 additions & 0 deletions test/e2e/app-dir/typeof-window/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"my-differentiated-files": "file:./my-differentiated-files.tar"
}
}
16 changes: 16 additions & 0 deletions test/e2e/app-dir/typeof-window/typeof-window.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { nextTestSetup } from 'e2e-utils'
import path from 'path'

describe('typeof-window', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
'my-differentiated-files': `file:${path.join(__dirname, 'my-differentiated-files.tar')}`,
},
})

it('should work using cheerio', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('Page loaded')
})
})
Loading