Skip to content

Commit

Permalink
feat(next-core): port remaining next.js custom transforms (#60498)
Browse files Browse the repository at this point in the history
<!-- 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?



-->

This PR ports over all of the remaining custom transforms for next-swc
into turbopack, and apply it into client / server rules accordingly.

Closes PACK-2225
  • Loading branch information
kwonoj authored Jan 17, 2024
1 parent 0c0ef86 commit 5956ff7
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ futures = { workspace = true }
lazy_static = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
rustc-hash = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__swc_transform_modularize_imports",
"__swc_transform_relay",
Expand Down
15 changes: 14 additions & 1 deletion packages/next-swc/crates/next-core/src/next_client/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use crate::{
next_shared::transforms::{
get_next_dynamic_transform_rule, get_next_font_transform_rule, get_next_image_rule,
get_next_modularize_imports_rule, get_next_pages_transforms_rule,
get_server_actions_transform_rule, server_actions::ActionsTransform,
get_server_actions_transform_rule, next_amp_attributes::get_next_amp_attr_rule,
next_cjs_optimizer::get_next_cjs_optimizer_rule,
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_page_config::get_next_page_config_rule, next_pure::get_next_pure_rule,
server_actions::ActionsTransform,
},
};

Expand Down Expand Up @@ -52,6 +56,15 @@ pub async fn get_next_client_transforms_rules(
ClientContextType::Fallback | ClientContextType::Other => None,
};

rules.push(get_next_amp_attr_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_rule(mdx_rs));
rules.push(get_next_disallow_export_all_in_page_rule(
mdx_rs,
pages_dir.is_some(),
));
rules.push(get_next_page_config_rule(mdx_rs, pages_dir.is_some()));
rules.push(get_next_pure_rule(mdx_rs));

rules.push(get_next_dynamic_transform_rule(false, false, pages_dir, mode, mdx_rs).await?);

rules.push(get_next_image_rule());
Expand Down
18 changes: 17 additions & 1 deletion packages/next-swc/crates/next-core/src/next_server/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
next_shared::transforms::{
get_next_dynamic_transform_rule, get_next_font_transform_rule, get_next_image_rule,
get_next_modularize_imports_rule, get_next_pages_transforms_rule,
get_server_actions_transform_rule,
get_server_actions_transform_rule, next_amp_attributes::get_next_amp_attr_rule,
next_cjs_optimizer::get_next_cjs_optimizer_rule,
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_pure::get_next_pure_rule,
next_react_server_components::get_next_react_server_components_transform_rule,
server_actions::ActionsTransform,
},
Expand Down Expand Up @@ -85,6 +88,19 @@ pub async fn get_next_server_transforms_rules(
.await?,
);

rules.push(get_next_amp_attr_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_rule(mdx_rs));
rules.push(get_next_disallow_export_all_in_page_rule(
mdx_rs,
pages_dir.is_some(),
));
rules.push(get_next_pure_rule(mdx_rs));

// [NOTE]: this rule only works in prod config
// https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L409
// rules.push(get_next_optimize_server_react_rule(enable_mdx_rs,
// optimize_use_state))

rules.push(get_next_image_rule());

Ok(rules)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
pub(crate) mod emotion;
pub(crate) mod modularize_imports;
pub(crate) mod next_amp_attributes;
pub(crate) mod next_cjs_optimizer;
pub(crate) mod next_disallow_re_export_all_in_page;
pub(crate) mod next_dynamic;
pub(crate) mod next_font;
pub(crate) mod next_optimize_server_react;
pub(crate) mod next_page_config;
pub(crate) mod next_pure;
pub(crate) mod next_react_server_components;
pub(crate) mod next_shake_exports;
pub(crate) mod next_strip_page_exports;
pub(crate) mod relay;
pub(crate) mod server_actions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::amp_attributes::amp_attributes;
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::{
common::util::take::Take,
ecma::{ast::*, visit::FoldWith},
},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

pub fn get_next_amp_attr_rule(enable_mdx_rs: bool) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextAmpAttributes {}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextAmpAttributes {}

#[async_trait]
impl CustomTransformer for NextAmpAttributes {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));
*program = p.fold_with(&mut amp_attributes());
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config, PackageConfig};
use rustc_hash::FxHashMap;
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::{
common::SyntaxContext,
ecma::{ast::*, visit::VisitMutWith},
},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
// [NOTE]: This isn't user configurable config
// (https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L395)
// build it internally without accepting customization.
let config = Config {
packages: FxHashMap::from_iter([(
"next/server".to_string(),
PackageConfig {
transforms: FxHashMap::from_iter([
(
"NextRequest".into(),
"next/dist/server/web/spec-extension/request".into(),
),
(
"NextResponse".into(),
"next/dist/server/web/spec-extension/response".into(),
),
(
"ImageResponse".into(),
"next/dist/server/web/spec-extension/image-response".into(),
),
(
"userAgentFromString".into(),
"next/dist/server/web/spec-extension/user-agent".into(),
),
(
"userAgent".into(),
"next/dist/server/web/spec-extension/user-agent".into(),
),
]),
},
)]),
};

let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextCjsOptimizer { config }) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextCjsOptimizer {
config: Config,
}

#[async_trait]
impl CustomTransformer for NextCjsOptimizer {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
let mut visitor = cjs_optimizer(
self.config.clone(),
SyntaxContext::empty().apply_mark(ctx.unresolved_mark),
);

program.visit_mut_with(&mut visitor);
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page;
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::{
common::util::take::Take,
ecma::{ast::*, visit::FoldWith},
},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

pub fn get_next_disallow_export_all_in_page_rule(
enable_mdx_rs: bool,
is_page_file: bool,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextDisallowReExportAllInPage {
is_page_file,
}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextDisallowReExportAllInPage {
is_page_file: bool,
}

#[async_trait]
impl CustomTransformer for NextDisallowReExportAllInPage {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));
*program = p.fold_with(&mut disallow_re_export_all_in_page(self.is_page_file));
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::optimize_server_react::{optimize_server_react, Config};
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::{
common::util::take::Take,
ecma::{ast::*, visit::FoldWith},
},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

#[allow(dead_code)]
pub fn get_next_optimize_server_react_rule(
enable_mdx_rs: bool,
optimize_use_state: bool,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextOptimizeServerReact {
optimize_use_state,
}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextOptimizeServerReact {
optimize_use_state: bool,
}

#[async_trait]
impl CustomTransformer for NextOptimizeServerReact {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));

*program = p.fold_with(&mut optimize_server_react(Config {
optimize_use_state: self.optimize_use_state,
}));
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::page_config::page_config;
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::{
common::util::take::Take,
ecma::{ast::*, visit::FoldWith},
},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

pub fn get_next_page_config_rule(enable_mdx_rs: bool, is_page_file: bool) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPageConfig {
// [TODO]: update once turbopack build works
is_development: true,
is_page_file,
}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextPageConfig {
is_development: bool,
is_page_file: bool,
}

#[async_trait]
impl CustomTransformer for NextPageConfig {
async fn transform(&self, program: &mut Program, _ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));

*program = p.fold_with(&mut page_config(self.is_development, self.is_page_file));
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::pure::pure_magic;
use turbo_tasks::Vc;
use turbopack_binding::{
swc::core::ecma::{ast::*, visit::VisitMutWith},
turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;

pub fn get_next_pure_rule(enable_mdx_rs: bool) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPure {}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}

#[derive(Debug)]
struct NextPure {}

#[async_trait]
impl CustomTransformer for NextPure {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
program.visit_mut_with(&mut pure_magic(ctx.comments.clone()));
Ok(())
}
}
Loading

0 comments on commit 5956ff7

Please sign in to comment.