Skip to content

Commit

Permalink
feat(turbopack): support initial compiler.emotion / compiler.styledCo…
Browse files Browse the repository at this point in the history
…mponents flag (#47991)

<!-- 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 or adding/fixing 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

### Why?

### How?

Closes NEXT-
Fixes #

-->

### What?

Related with WEB-669 and initial support for
[WEB-670](https://linear.app/vercel/issue/WEB-670), allows to consume
compiler.emotion with latest turbopack.

I was trying to make additional changes to make test fully pass, but
there are some other failures around so this change cannot able to pass
existing tests yet.

Turbopack changes: vercel/turborepo#4482
  • Loading branch information
kwonoj authored Apr 12, 2023
1 parent ccaa7d6 commit e97100c
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 44 deletions.
60 changes: 30 additions & 30 deletions packages/next-swc/Cargo.lock

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

6 changes: 3 additions & 3 deletions packages/next-swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ swc_emotion = { version = "0.29.10" }
testing = { version = "0.31.31" }

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

# General Deps

Expand Down
12 changes: 8 additions & 4 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ use crate::{
},
react_refresh::assert_can_resolve_react_refresh,
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
get_typescript_transform_options,
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options,
},
util::foreign_code_context_condition,
};
Expand Down Expand Up @@ -175,6 +175,8 @@ pub async fn get_client_module_options_context(
.clone_if()
};

let enable_emotion = *get_emotion_compiler_config(next_config).await?;

let module_options_context = ModuleOptionsContext {
custom_ecmascript_transforms: vec![EcmascriptInputTransform::ServerDirective(
StringVc::cell("TODO".to_string()),
Expand All @@ -184,14 +186,16 @@ pub async fn get_client_module_options_context(
..Default::default()
};

let enable_styled_components = *get_styled_components_compiler_config(next_config).await?;

let module_options_context = ModuleOptionsContext {
// We don't need to resolve React Refresh for each module. Instead,
// we try resolve it once at the root and pass down a context to all
// the modules.
enable_jsx: Some(jsx_runtime_options),
enable_emotion: true,
enable_emotion,
enable_react_refresh,
enable_styled_components: true,
enable_styled_components,
enable_styled_jsx: true,
enable_postcss_transform: Some(PostCssTransformOptions {
postcss_package: Some(get_postcss_package_mapping(project_path)),
Expand Down
25 changes: 23 additions & 2 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use turbo_binding::{
execution_context::{ExecutionContext, ExecutionContextVc},
transforms::webpack::{WebpackLoaderConfigItems, WebpackLoaderConfigItemsVc},
},
turbopack::evaluate_context::node_evaluate_asset_context,
turbopack::{
evaluate_context::node_evaluate_asset_context,
module_options::{EmotionTransformConfig, StyledComponentsTransformConfig},
},
},
};
use turbo_tasks::{
Expand All @@ -55,9 +58,11 @@ pub struct NextConfig {
pub rewrites: Rewrites,
pub transpile_packages: Option<Vec<String>>,

// Partially supported
pub compiler: Option<CompilerConfig>,

// unsupported
cross_origin: Option<String>,
compiler: Option<CompilerConfig>,
amp: AmpConfig,
analytics_id: String,
asset_prefix: String,
Expand Down Expand Up @@ -415,12 +420,28 @@ enum MiddlewarePrefetchType {
Flexible,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(untagged)]
pub enum EmotionTransformOptionsOrBoolean {
Boolean(bool),
Options(EmotionTransformConfig),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(untagged)]
pub enum StyledComponentsTransformOptionsOrBoolean {
Boolean(bool),
Options(StyledComponentsTransformConfig),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct CompilerConfig {
pub react_remove_properties: Option<bool>,
pub relay: Option<RelayConfig>,
pub emotion: Option<EmotionTransformOptionsOrBoolean>,
pub remove_console: Option<RemoveConsoleConfig>,
pub styled_components: Option<StyledComponentsTransformOptionsOrBoolean>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
Expand Down
Loading

0 comments on commit e97100c

Please sign in to comment.