Skip to content

Commit

Permalink
Enable minification for Turbopack (#62994)
Browse files Browse the repository at this point in the history
## What?

Implementation for vercel/turbo#7660. Enables
minification of browser files.

<!-- 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:

## For Contributors

### Improving Documentation

- 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

### Adding or Updating Examples

- 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

### Fixing a bug

- 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

### Adding a feature

- 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


## For Maintainers

- 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

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2742

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
  • Loading branch information
timneutkens and sokra committed Mar 8, 2024
1 parent ff1d4cb commit 36e401e
Show file tree
Hide file tree
Showing 20 changed files with 177 additions and 221 deletions.
128 changes: 64 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ swc_core = { version = "0.90.17", features = [
testing = { version = "0.35.20" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240307.1" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240307.3" }
# [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-240307.1" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240307.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240307.1" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240307.3" }

# General Deps

Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ turbopack-binding = { workspace = true, features = [
"__turbo_tasks_env",
"__turbo_tasks_fs",
"__turbopack",
"__turbopack_build",
"__turbopack_nodejs",
"__turbopack_core",
"__turbopack_dev",
"__turbopack_browser",
"__turbopack_env",
"__turbopack_cli_utils",
"__turbopack_node",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use turbopack_binding::{
tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPath},
},
turbopack::{
build::EntryChunkGroupResult,
core::{
asset::{Asset, AssetContent},
chunk::{availability_info::AvailabilityInfo, ChunkingContextExt, EvaluatableAssets},
Expand All @@ -50,6 +49,7 @@ use turbopack_binding::{
output::{OutputAsset, OutputAssets},
virtual_output::VirtualOutputAsset,
},
nodejs::EntryChunkGroupResult,
turbopack::{
module_options::ModuleOptionsContext, resolve_options_context::ResolveOptionsContext,
transition::ContextTransition, ModuleAssetContext,
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-api/src/dynamic_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use turbopack_binding::{
visit::{Visit, VisitWith},
},
turbopack::{
build::BuildChunkingContext,
core::{
chunk::{
availability_info::AvailabilityInfo, ChunkableModule, ChunkingContextExt,
Expand All @@ -31,6 +30,7 @@ use turbopack_binding::{
resolve::esm_resolve,
EcmascriptModuleAsset,
},
nodejs::NodeJsChunkingContext,
},
};

Expand Down Expand Up @@ -79,7 +79,7 @@ where
}

pub(crate) async fn collect_chunk_group(
chunking_context: Vc<BuildChunkingContext>,
chunking_context: Vc<NodeJsChunkingContext>,
dynamic_import_entries: IndexMap<Vc<Box<dyn Module>>, DynamicImportedModules>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<DynamicImportedChunks>> {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use turbo_tasks::{Completion, Value, Vc};
use turbopack_binding::{
turbo::tasks_fs::{File, FileContent},
turbopack::{
build::EntryChunkGroupResult,
core::{
asset::AssetContent,
chunk::{availability_info::AvailabilityInfo, ChunkingContextExt},
Expand All @@ -22,6 +21,7 @@ use turbopack_binding::{
virtual_output::VirtualOutputAsset,
},
ecmascript::chunk::EcmascriptChunkPlaceable,
nodejs::EntryChunkGroupResult,
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ shadow_rs::shadow!(build);

pub fn register() {
next_core::register();
turbopack_binding::turbopack::build::register();
turbopack_binding::turbopack::nodejs::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use turbopack_binding::{
File, FileContent, FileSystem, FileSystemPath, FileSystemPathOption, VirtualFileSystem,
},
turbopack::{
build::{BuildChunkingContext, EntryChunkGroupResult},
core::{
asset::AssetContent,
chunk::{availability_info::AvailabilityInfo, ChunkingContextExt, EvaluatableAssets},
Expand All @@ -57,6 +56,7 @@ use turbopack_binding::{
resolve::esm_resolve,
EcmascriptModuleAsset,
},
nodejs::{EntryChunkGroupResult, NodeJsChunkingContext},
turbopack::{
module_options::ModuleOptionsContext,
resolve_options_context::ResolveOptionsContext,
Expand Down Expand Up @@ -658,7 +658,7 @@ impl PageEndpoint {
project_root: Vc<FileSystemPath>,
module_context: Vc<ModuleAssetContext>,
edge_module_context: Vc<ModuleAssetContext>,
chunking_context: Vc<BuildChunkingContext>,
chunking_context: Vc<NodeJsChunkingContext>,
edge_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
runtime_entries: Vc<EvaluatableAssets>,
edge_runtime_entries: Vc<EvaluatableAssets>,
Expand Down
24 changes: 15 additions & 9 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use turbopack_binding::{
tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, VirtualFileSystem},
},
turbopack::{
build::BuildChunkingContext,
browser::BrowserChunkingContext,
core::{
changed::content_changed,
compile_time_info::CompileTimeInfo,
Expand All @@ -48,9 +48,9 @@ use turbopack_binding::{
version::{Update, Version, VersionState, VersionedContent},
PROJECT_FILESYSTEM_NAME,
},
dev::DevChunkingContext,
ecmascript::chunk::EcmascriptChunkingContext,
node::execution_context::ExecutionContext,
nodejs::NodeJsChunkingContext,
turbopack::{evaluate_context::node_build_environment, ModuleAssetContext},
},
};
Expand Down Expand Up @@ -497,7 +497,7 @@ impl Project {
let node_root = self.node_root();

let node_execution_chunking_context = Vc::upcast(
DevChunkingContext::builder(
BrowserChunkingContext::builder(
self.project_path(),
node_root,
node_root,
Expand Down Expand Up @@ -555,25 +555,31 @@ impl Project {
}

#[turbo_tasks::function]
pub(super) fn server_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
get_server_chunking_context(
pub(super) async fn server_chunking_context(
self: Vc<Self>,
) -> Result<Vc<NodeJsChunkingContext>> {
Ok(get_server_chunking_context(
self.next_mode(),
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.server_compile_time_info().environment(),
)
))
}

#[turbo_tasks::function]
pub(super) fn edge_chunking_context(self: Vc<Self>) -> Vc<Box<dyn EcmascriptChunkingContext>> {
get_edge_chunking_context(
pub(super) fn edge_chunking_context(
self: Vc<Self>,
) -> Result<Vc<Box<dyn EcmascriptChunkingContext>>> {
Ok(get_edge_chunking_context(
self.next_mode(),
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.edge_compile_time_info().environment(),
)
))
}

/// Emit a telemetry event corresponding to [webpack configuration telemetry](https://github.com/vercel/next.js/blob/9da305fe320b89ee2f8c3cfb7ecbf48856368913/packages/next/src/build/webpack-config.ts#L2516)
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ turbopack-binding = { workspace = true, features = [
"__turbo_tasks_fs",
"__turbo_tasks_memory",
"__turbopack",
"__turbopack_build",
"__turbopack_nodejs",
"__turbopack_core",
"__turbopack_dev",
"__turbopack_browser",
"__turbopack_ecmascript",
"__turbopack_ecmascript_runtime",
"__turbopack_env",
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn register() {
turbopack_binding::turbopack::turbopack::register();
turbopack_binding::turbopack::core::register();
turbopack_binding::turbopack::node::register();
turbopack_binding::turbopack::dev::register();
turbopack_binding::turbopack::build::register();
turbopack_binding::turbopack::browser::register();
turbopack_binding::turbopack::nodejs::register();
next_core::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
6 changes: 3 additions & 3 deletions packages/next-swc/crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ turbopack-binding = { workspace = true, features = [
"__turbo_tasks_fs",
"__turbo_tasks_hash",
"__turbopack",
"__turbopack_build",
"__turbopack_nodejs",
"__turbopack_core",
"__turbopack_dev",
"__turbopack_browser",
"__turbopack_ecmascript",
"__turbopack_ecmascript_plugin",
"__turbopack_ecmascript_runtime",
Expand Down Expand Up @@ -74,5 +74,5 @@ image-avif = ["turbopack-binding/__turbopack_image_avif"]
# enable "HMR" for embedded assets
dynamic_embed_contents = [
"turbopack-binding/__turbo_tasks_fs_dynamic_embed_contents",
"turbopack-binding/__turbopack_dev_dynamic_embed_contents",
"turbopack-binding/__turbopack_browser_dynamic_embed_contents",
]
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn register() {
turbo::tasks_bytes::register();
turbo::tasks_fs::register();
turbo::tasks_fetch::register();
turbopack::dev::register();
turbopack::browser::register();
turbopack::node::register();
turbopack::turbopack::register();
turbopack::image::register();
Expand Down
7 changes: 7 additions & 0 deletions packages/next-swc/crates/next-core/src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ impl NextMode {
NextMode::Build => true,
}
}

pub fn should_minify(&self) -> bool {
match self {
NextMode::Development => false,
NextMode::Build => true,
}
}
}
13 changes: 10 additions & 3 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use turbo_tasks_fs::FileSystem;
use turbopack_binding::{
turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{
browser::{react_refresh::assert_can_resolve_react_refresh, BrowserChunkingContext},
core::{
chunk::MinifyType,
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReference,
FreeVarReferences,
Expand All @@ -16,7 +18,6 @@ use turbopack_binding::{
free_var_references,
resolve::{parse::Request, pattern::Pattern},
},
dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContext},
ecmascript::{chunk::EcmascriptChunkingContext, TreeShakingMode},
node::{
execution_context::ExecutionContext,
Expand Down Expand Up @@ -336,7 +337,8 @@ pub async fn get_client_chunking_context(
environment: Vc<Environment>,
mode: Vc<NextMode>,
) -> Result<Vc<Box<dyn EcmascriptChunkingContext>>> {
let mut builder = DevChunkingContext::builder(
let next_mode = mode.await?;
let mut builder = BrowserChunkingContext::builder(
project_path,
client_root,
client_root,
Expand All @@ -345,9 +347,14 @@ pub async fn get_client_chunking_context(
environment,
)
.chunk_base_path(asset_prefix)
.minify_type(if next_mode.should_minify() {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.asset_base_path(asset_prefix);

if mode.await?.is_development() {
if next_mode.is_development() {
builder = builder.hot_module_replacement();
}

Expand Down
19 changes: 13 additions & 6 deletions packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ use turbo_tasks::{Value, Vc};
use turbopack_binding::{
turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{
browser::BrowserChunkingContext,
core::{
chunk::MinifyType,
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReference,
FreeVarReferences,
},
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment},
free_var_references,
},
dev::DevChunkingContext,
ecmascript::chunk::EcmascriptChunkingContext,
node::{debug::should_debug, execution_context::ExecutionContext},
turbopack::resolve_options_context::ResolveOptionsContext,
Expand Down Expand Up @@ -148,16 +149,17 @@ pub async fn get_edge_resolve_options_context(
}

#[turbo_tasks::function]
pub fn get_edge_chunking_context(
pub async fn get_edge_chunking_context(
mode: Vc<NextMode>,
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
asset_prefix: Vc<Option<String>>,
environment: Vc<Environment>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
) -> Result<Vc<Box<dyn EcmascriptChunkingContext>>> {
let output_root = node_root.join("server/edge".to_string());
Vc::upcast(
DevChunkingContext::builder(
Ok(Vc::upcast(
BrowserChunkingContext::builder(
project_path,
output_root,
client_root,
Expand All @@ -167,6 +169,11 @@ pub fn get_edge_chunking_context(
)
.asset_base_path(asset_prefix)
.reference_chunk_source_maps(should_debug("edge"))
.minify_type(if mode.await?.should_minify() {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.build(),
)
))
}
18 changes: 12 additions & 6 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use turbopack_binding::{
tasks_fs::FileSystemPath,
},
turbopack::{
build::{BuildChunkingContext, MinifyType},
core::{
chunk::MinifyType,
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReferences,
},
Expand All @@ -22,6 +22,7 @@ use turbopack_binding::{
execution_context::ExecutionContext,
transforms::postcss::{PostCssConfigLocation, PostCssTransformOptions},
},
nodejs::NodeJsChunkingContext,
turbopack::{
condition::ContextCondition,
module_options::{
Expand Down Expand Up @@ -725,19 +726,20 @@ pub fn get_server_runtime_entries(
}

#[turbo_tasks::function]
pub fn get_server_chunking_context(
pub async fn get_server_chunking_context(
mode: Vc<NextMode>,
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
// TODO(alexkirsz) Is this even necessary? Are assets not always on the client chunking context
// anyway?
client_root: Vc<FileSystemPath>,
asset_prefix: Vc<Option<String>>,
environment: Vc<Environment>,
) -> Vc<BuildChunkingContext> {
) -> Result<Vc<NodeJsChunkingContext>> {
// TODO(alexkirsz) This should return a trait that can be implemented by the
// different server chunking contexts. OR the build chunking context should
// support both production and development modes.
BuildChunkingContext::builder(
Ok(NodeJsChunkingContext::builder(
project_path,
node_root,
client_root,
Expand All @@ -746,6 +748,10 @@ pub fn get_server_chunking_context(
environment,
)
.asset_prefix(asset_prefix)
.minify_type(MinifyType::NoMinify)
.build()
.minify_type(if mode.await?.should_minify() {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.build())
}

0 comments on commit 36e401e

Please sign in to comment.