From 1b56ff9a903fd4bbe77ab6cf96f859d450ae324f Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:00:44 -0700 Subject: [PATCH 1/7] feat(turbopack): support initial compiler.emotion flag --- .../next-core/src/next_client/context.rs | 6 ++- .../crates/next-core/src/next_config.rs | 6 ++- .../next-core/src/next_server/context.rs | 7 +++- .../crates/next-core/src/transform_options.rs | 41 ++++++++++++++++++- packages/next/src/lib/turbopack-warning.ts | 1 + 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_client/context.rs b/packages/next-swc/crates/next-core/src/next_client/context.rs index dacbc5b36af90..22ee4822f5bf0 100644 --- a/packages/next-swc/crates/next-core/src/next_client/context.rs +++ b/packages/next-swc/crates/next-core/src/next_client/context.rs @@ -48,7 +48,7 @@ use crate::{ }, react_refresh::assert_can_resolve_react_refresh, transform_options::{ - get_decorators_transform_options, get_jsx_transform_options, + get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, get_typescript_transform_options, }, util::foreign_code_context_condition, @@ -175,6 +175,8 @@ pub async fn get_client_module_options_context( .clone_if() }; + let emotion_compiler_options = get_emotion_compiler_config(next_config); + let module_options_context = ModuleOptionsContext { custom_ecmascript_transforms: vec![EcmascriptInputTransform::ServerDirective( StringVc::cell("TODO".to_string()), @@ -189,7 +191,7 @@ pub async fn get_client_module_options_context( // 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: Some(emotion_compiler_options), enable_react_refresh, enable_styled_components: true, enable_styled_jsx: true, diff --git a/packages/next-swc/crates/next-core/src/next_config.rs b/packages/next-swc/crates/next-core/src/next_config.rs index 053be2104bacf..1898ce8460a23 100644 --- a/packages/next-swc/crates/next-core/src/next_config.rs +++ b/packages/next-swc/crates/next-core/src/next_config.rs @@ -55,9 +55,11 @@ pub struct NextConfig { pub rewrites: Rewrites, pub transpile_packages: Option>, + // Partially supported + pub compiler: Option, + // unsupported cross_origin: Option, - compiler: Option, amp: AmpConfig, analytics_id: String, asset_prefix: String, @@ -353,6 +355,7 @@ pub struct ExperimentalConfig { pub app_dir: Option, pub server_components_external_packages: Option>, pub turbo: Option, + pub compiler: Option, // unsupported adjust_font_fallbacks: Option, @@ -420,6 +423,7 @@ enum MiddlewarePrefetchType { pub struct CompilerConfig { pub react_remove_properties: Option, pub relay: Option, + pub emotion: Option, pub remove_console: Option, } diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index 75f427eee4f33..690cc29450cd1 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -32,7 +32,7 @@ use crate::{ next_config::NextConfigVc, next_import_map::get_next_server_import_map, transform_options::{ - get_decorators_transform_options, get_jsx_transform_options, + get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, get_typescript_transform_options, }, util::foreign_code_context_condition, @@ -235,6 +235,7 @@ pub async fn get_server_module_options_context( let tsconfig = get_typescript_transform_options(project_path); let decorators_options = get_decorators_transform_options(project_path); let jsx_runtime_options = get_jsx_transform_options(project_path); + let emotion_compiler_options = get_emotion_compiler_config(next_config); let module_options_context = match ty.into_value() { ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { @@ -245,6 +246,7 @@ pub async fn get_server_module_options_context( ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, + enable_emotion: Some(emotion_compiler_options), enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -270,6 +272,7 @@ pub async fn get_server_module_options_context( ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, + enable_emotion: Some(emotion_compiler_options), enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -300,6 +303,7 @@ pub async fn get_server_module_options_context( }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), + enable_emotion: Some(emotion_compiler_options), enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -337,6 +341,7 @@ pub async fn get_server_module_options_context( }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), + enable_emotion: Some(emotion_compiler_options), enable_styled_jsx: true, enable_postcss_transform, enable_webpack_loaders, diff --git a/packages/next-swc/crates/next-core/src/transform_options.rs b/packages/next-swc/crates/next-core/src/transform_options.rs index 335112ceccf20..edcd7b13cce35 100644 --- a/packages/next-swc/crates/next-core/src/transform_options.rs +++ b/packages/next-swc/crates/next-core/src/transform_options.rs @@ -7,7 +7,8 @@ use turbo_binding::turbopack::{ }, ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig}, turbopack::module_options::{ - DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, JsxTransformOptions, + DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, EmotionLabelKind, + EmotionTransformOptions, EmotionTransformOptionsVc, JsxTransformOptions, JsxTransformOptionsVc, TypescriptTransformOptions, TypescriptTransformOptionsVc, }, }; @@ -152,3 +153,41 @@ pub async fn get_jsx_transform_options( Ok(react_transform_options.cell()) } + +#[turbo_tasks::function] +pub async fn get_emotion_compiler_config( + next_config: NextConfigVc, +) -> Result { + let emotion_compiler_config = (*next_config.await?) + .compiler + .as_ref() + .map(|value| { + value + .emotion + .as_ref() + .map(|value| { + if value.is_boolean() { + EmotionTransformOptionsVc::cell(EmotionTransformOptions { + enabled: value.as_bool().unwrap_or(false), + ..Default::default() + }) + } else { + EmotionTransformOptionsVc::cell(EmotionTransformOptions { + enabled: true, + sourcemap: value["sourcemap"].as_bool(), + auto_label: match value["auto_label"].to_string().as_str() { + "dev-only" => Some(EmotionLabelKind::DevOnly), + "always" => Some(EmotionLabelKind::Always), + "never" => Some(EmotionLabelKind::Never), + _ => None, + }, + label_format: value["labelFormat"].as_str().map(|s| s.to_string()), + }) + } + }) + .unwrap_or_default() + }) + .unwrap_or_default(); + + Ok(emotion_compiler_config) +} diff --git a/packages/next/src/lib/turbopack-warning.ts b/packages/next/src/lib/turbopack-warning.ts index e9a5a4136b5e3..d9df169309f03 100644 --- a/packages/next/src/lib/turbopack-warning.ts +++ b/packages/next/src/lib/turbopack-warning.ts @@ -7,6 +7,7 @@ const supportedTurbopackNextConfigOptions = [ 'configFileName', 'env', 'experimental.appDir', + 'compiler.emotion', 'experimental.serverComponentsExternalPackages', 'experimental.turbo', 'images', From 9bcde9bcedf52b02ee359be5b01738b0fe836fc3 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:12:29 -0700 Subject: [PATCH 2/7] test(emotion): update test fixture --- .../next-swc/crates/next-core/src/transform_options.rs | 2 ++ test/development/basic/emotion-swc.test.ts | 8 +++++++- .../basic/emotion-swc/next.turbopack.config.js | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/development/basic/emotion-swc/next.turbopack.config.js diff --git a/packages/next-swc/crates/next-core/src/transform_options.rs b/packages/next-swc/crates/next-core/src/transform_options.rs index edcd7b13cce35..378fbd59d3ca9 100644 --- a/packages/next-swc/crates/next-core/src/transform_options.rs +++ b/packages/next-swc/crates/next-core/src/transform_options.rs @@ -14,6 +14,8 @@ use turbo_binding::turbopack::{ }; use turbo_tasks_fs::{FileJsonContentVc, FileSystemPathVc}; +use crate::next_config::NextConfigVc; + async fn get_typescript_options( project_path: FileSystemPathVc, ) -> Option> { diff --git a/test/development/basic/emotion-swc.test.ts b/test/development/basic/emotion-swc.test.ts index 83cd65f50aa26..20cba3297eb4e 100644 --- a/test/development/basic/emotion-swc.test.ts +++ b/test/development/basic/emotion-swc.test.ts @@ -2,11 +2,13 @@ import { join } from 'path' import webdriver from 'next-webdriver' import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' +import { shouldRunTurboDevTest } from 'test/lib/next-test-utils' describe('emotion SWC option', () => { let next: NextInstance beforeAll(async () => { + const useTurbo = !!process.env.TEST_WASM ? false : shouldRunTurboDevTest() next = await createNext({ files: { 'jsconfig.json': new FileRef( @@ -14,11 +16,15 @@ describe('emotion SWC option', () => { ), pages: new FileRef(join(__dirname, 'emotion-swc/pages')), shared: new FileRef(join(__dirname, 'emotion-swc/shared')), + // Stopgap until turobpack supports full next.config.js for `reactStrictMode` 'next.config.js': new FileRef( - join(__dirname, 'emotion-swc/next.config.js') + useTurbo + ? join(__dirname, 'emotion-swc/next.turbopack.config.js') + : join(__dirname, 'emotion-swc/next.config.js') ), }, dependencies: { + '@emotion/cache': '^10.0.29', '@emotion/core': '^10.0.35', '@emotion/styled': '^10.0.27', }, diff --git a/test/development/basic/emotion-swc/next.turbopack.config.js b/test/development/basic/emotion-swc/next.turbopack.config.js new file mode 100644 index 0000000000000..71dbfec033b16 --- /dev/null +++ b/test/development/basic/emotion-swc/next.turbopack.config.js @@ -0,0 +1,9 @@ +/** @type {import('next').NextConfig} */ + +const nextConfig = { + compiler: { + emotion: true, + }, +} + +module.exports = nextConfig From 5e94624ceec252bad80cbab3067cee9a26ed9567 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Thu, 6 Apr 2023 11:31:33 -0700 Subject: [PATCH 3/7] feat(turbopack): support initial compiler.emotion flag --- .../next-core/src/next_client/context.rs | 4 +- .../crates/next-core/src/next_config.rs | 14 ++++-- .../next-core/src/next_server/context.rs | 10 ++--- .../crates/next-core/src/transform_options.rs | 45 +++++++++---------- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_client/context.rs b/packages/next-swc/crates/next-core/src/next_client/context.rs index 22ee4822f5bf0..061aef30018dd 100644 --- a/packages/next-swc/crates/next-core/src/next_client/context.rs +++ b/packages/next-swc/crates/next-core/src/next_client/context.rs @@ -175,7 +175,7 @@ pub async fn get_client_module_options_context( .clone_if() }; - let emotion_compiler_options = get_emotion_compiler_config(next_config); + let enable_emotion = *get_emotion_compiler_config(next_config).await?; let module_options_context = ModuleOptionsContext { custom_ecmascript_transforms: vec![EcmascriptInputTransform::ServerDirective( @@ -191,7 +191,7 @@ pub async fn get_client_module_options_context( // 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: Some(emotion_compiler_options), + enable_emotion, enable_react_refresh, enable_styled_components: true, enable_styled_jsx: true, diff --git a/packages/next-swc/crates/next-core/src/next_config.rs b/packages/next-swc/crates/next-core/src/next_config.rs index 1898ce8460a23..288d47054e258 100644 --- a/packages/next-swc/crates/next-core/src/next_config.rs +++ b/packages/next-swc/crates/next-core/src/next_config.rs @@ -28,7 +28,9 @@ 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, + }, }, }; use turbo_tasks::{ @@ -355,7 +357,6 @@ pub struct ExperimentalConfig { pub app_dir: Option, pub server_components_external_packages: Option>, pub turbo: Option, - pub compiler: Option, // unsupported adjust_font_fallbacks: Option, @@ -418,12 +419,19 @@ 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(rename_all = "camelCase")] pub struct CompilerConfig { pub react_remove_properties: Option, pub relay: Option, - pub emotion: Option, + pub emotion: Option, pub remove_console: Option, } diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index 690cc29450cd1..d9db554d41658 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -235,7 +235,7 @@ pub async fn get_server_module_options_context( let tsconfig = get_typescript_transform_options(project_path); let decorators_options = get_decorators_transform_options(project_path); let jsx_runtime_options = get_jsx_transform_options(project_path); - let emotion_compiler_options = get_emotion_compiler_config(next_config); + let enable_emotion = *get_emotion_compiler_config(next_config).await?; let module_options_context = match ty.into_value() { ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { @@ -246,7 +246,7 @@ pub async fn get_server_module_options_context( ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, - enable_emotion: Some(emotion_compiler_options), + enable_emotion, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -272,7 +272,7 @@ pub async fn get_server_module_options_context( ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, - enable_emotion: Some(emotion_compiler_options), + enable_emotion, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -303,7 +303,7 @@ pub async fn get_server_module_options_context( }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), - enable_emotion: Some(emotion_compiler_options), + enable_emotion, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -341,7 +341,7 @@ pub async fn get_server_module_options_context( }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), - enable_emotion: Some(emotion_compiler_options), + enable_emotion, enable_styled_jsx: true, enable_postcss_transform, enable_webpack_loaders, diff --git a/packages/next-swc/crates/next-core/src/transform_options.rs b/packages/next-swc/crates/next-core/src/transform_options.rs index 378fbd59d3ca9..17031425deac9 100644 --- a/packages/next-swc/crates/next-core/src/transform_options.rs +++ b/packages/next-swc/crates/next-core/src/transform_options.rs @@ -7,14 +7,14 @@ use turbo_binding::turbopack::{ }, ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig}, turbopack::module_options::{ - DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, EmotionLabelKind, - EmotionTransformOptions, EmotionTransformOptionsVc, JsxTransformOptions, - JsxTransformOptionsVc, TypescriptTransformOptions, TypescriptTransformOptionsVc, + DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, EmotionTransformConfig, + EmotionTransformConfigVc, JsxTransformOptions, JsxTransformOptionsVc, + OptionEmotionTransformConfigVc, TypescriptTransformOptions, TypescriptTransformOptionsVc, }, }; use turbo_tasks_fs::{FileJsonContentVc, FileSystemPathVc}; -use crate::next_config::NextConfigVc; +use crate::next_config::{EmotionTransformOptionsOrBoolean, NextConfigVc}; async fn get_typescript_options( project_path: FileSystemPathVc, @@ -159,7 +159,7 @@ pub async fn get_jsx_transform_options( #[turbo_tasks::function] pub async fn get_emotion_compiler_config( next_config: NextConfigVc, -) -> Result { +) -> Result { let emotion_compiler_config = (*next_config.await?) .compiler .as_ref() @@ -168,28 +168,23 @@ pub async fn get_emotion_compiler_config( .emotion .as_ref() .map(|value| { - if value.is_boolean() { - EmotionTransformOptionsVc::cell(EmotionTransformOptions { - enabled: value.as_bool().unwrap_or(false), - ..Default::default() - }) - } else { - EmotionTransformOptionsVc::cell(EmotionTransformOptions { - enabled: true, - sourcemap: value["sourcemap"].as_bool(), - auto_label: match value["auto_label"].to_string().as_str() { - "dev-only" => Some(EmotionLabelKind::DevOnly), - "always" => Some(EmotionLabelKind::Always), - "never" => Some(EmotionLabelKind::Never), - _ => None, - }, - label_format: value["labelFormat"].as_str().map(|s| s.to_string()), - }) - } + let options = match value { + EmotionTransformOptionsOrBoolean::Boolean(true) => { + Some(EmotionTransformConfigVc::cell(EmotionTransformConfig { + ..Default::default() + })) + } + EmotionTransformOptionsOrBoolean::Boolean(false) => None, + EmotionTransformOptionsOrBoolean::Options(value) => { + Some(EmotionTransformConfigVc::cell(value.clone())) + } + }; + + OptionEmotionTransformConfigVc::cell(options) }) - .unwrap_or_default() + .unwrap_or_else(|| OptionEmotionTransformConfigVc::cell(None)) }) - .unwrap_or_default(); + .unwrap_or_else(|| OptionEmotionTransformConfigVc::cell(None)); Ok(emotion_compiler_config) } From c75ddfa8fc57d5ec56af0dd493b9bb1639153766 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Thu, 6 Apr 2023 11:31:39 -0700 Subject: [PATCH 4/7] test(emotion): update test fixture --- test/development/basic/emotion-swc.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/development/basic/emotion-swc.test.ts b/test/development/basic/emotion-swc.test.ts index 20cba3297eb4e..e10d58b8cbb8c 100644 --- a/test/development/basic/emotion-swc.test.ts +++ b/test/development/basic/emotion-swc.test.ts @@ -2,7 +2,7 @@ import { join } from 'path' import webdriver from 'next-webdriver' import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' -import { shouldRunTurboDevTest } from 'test/lib/next-test-utils' +import { shouldRunTurboDevTest } from '../../lib/next-test-utils' describe('emotion SWC option', () => { let next: NextInstance From f0f1de56ac7b09e1873b17320a273124957d61fb Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Tue, 11 Apr 2023 17:31:17 -0700 Subject: [PATCH 5/7] feat(turbopack): enable styled components transform --- .../next-core/src/next_client/context.rs | 6 ++- .../crates/next-core/src/next_config.rs | 11 ++++- .../next-core/src/next_server/context.rs | 7 +++- .../crates/next-core/src/transform_options.rs | 41 ++++++++++++++++++- packages/next/src/lib/turbopack-warning.ts | 1 + 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_client/context.rs b/packages/next-swc/crates/next-core/src/next_client/context.rs index 061aef30018dd..75d01dc2d25e5 100644 --- a/packages/next-swc/crates/next-core/src/next_client/context.rs +++ b/packages/next-swc/crates/next-core/src/next_client/context.rs @@ -49,7 +49,7 @@ use crate::{ react_refresh::assert_can_resolve_react_refresh, transform_options::{ get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, - get_typescript_transform_options, + get_styled_components_compiler_config, get_typescript_transform_options, }, util::foreign_code_context_condition, }; @@ -186,6 +186,8 @@ 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 @@ -193,7 +195,7 @@ pub async fn get_client_module_options_context( enable_jsx: Some(jsx_runtime_options), 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)), diff --git a/packages/next-swc/crates/next-core/src/next_config.rs b/packages/next-swc/crates/next-core/src/next_config.rs index 288d47054e258..1d02203a73480 100644 --- a/packages/next-swc/crates/next-core/src/next_config.rs +++ b/packages/next-swc/crates/next-core/src/next_config.rs @@ -29,7 +29,8 @@ use turbo_binding::{ transforms::webpack::{WebpackLoaderConfigItems, WebpackLoaderConfigItemsVc}, }, turbopack::{ - evaluate_context::node_evaluate_asset_context, module_options::EmotionTransformConfig, + evaluate_context::node_evaluate_asset_context, + module_options::{EmotionTransformConfig, StyledComponentsTransformConfig}, }, }, }; @@ -426,6 +427,13 @@ pub enum EmotionTransformOptionsOrBoolean { 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 { @@ -433,6 +441,7 @@ pub struct CompilerConfig { pub relay: Option, pub emotion: Option, pub remove_console: Option, + pub styled_components: Option, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index d9db554d41658..c8192c762b923 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -33,7 +33,7 @@ use crate::{ next_import_map::get_next_server_import_map, transform_options::{ get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, - get_typescript_transform_options, + get_styled_components_compiler_config, get_typescript_transform_options, }, util::foreign_code_context_condition, }; @@ -236,6 +236,7 @@ pub async fn get_server_module_options_context( let decorators_options = get_decorators_transform_options(project_path); let jsx_runtime_options = get_jsx_transform_options(project_path); let enable_emotion = *get_emotion_compiler_config(next_config).await?; + let enable_styled_components = *get_styled_components_compiler_config(next_config).await?; let module_options_context = match ty.into_value() { ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { @@ -247,6 +248,7 @@ pub async fn get_server_module_options_context( enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, enable_emotion, + enable_styled_components, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -273,6 +275,7 @@ pub async fn get_server_module_options_context( enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, enable_emotion, + enable_styled_components, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -304,6 +307,7 @@ pub async fn get_server_module_options_context( ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_emotion, + enable_styled_components, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), @@ -343,6 +347,7 @@ pub async fn get_server_module_options_context( enable_jsx: Some(jsx_runtime_options), enable_emotion, enable_styled_jsx: true, + enable_styled_components, enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), diff --git a/packages/next-swc/crates/next-core/src/transform_options.rs b/packages/next-swc/crates/next-core/src/transform_options.rs index 17031425deac9..95343635d29d3 100644 --- a/packages/next-swc/crates/next-core/src/transform_options.rs +++ b/packages/next-swc/crates/next-core/src/transform_options.rs @@ -9,12 +9,15 @@ use turbo_binding::turbopack::{ turbopack::module_options::{ DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, EmotionTransformConfig, EmotionTransformConfigVc, JsxTransformOptions, JsxTransformOptionsVc, - OptionEmotionTransformConfigVc, TypescriptTransformOptions, TypescriptTransformOptionsVc, + OptionEmotionTransformConfigVc, OptionStyledComponentsTransformConfigVc, + StyledComponentsTransformConfig, TypescriptTransformOptions, TypescriptTransformOptionsVc, }, }; use turbo_tasks_fs::{FileJsonContentVc, FileSystemPathVc}; -use crate::next_config::{EmotionTransformOptionsOrBoolean, NextConfigVc}; +use crate::next_config::{ + EmotionTransformOptionsOrBoolean, NextConfigVc, StyledComponentsTransformOptionsOrBoolean, +}; async fn get_typescript_options( project_path: FileSystemPathVc, @@ -188,3 +191,37 @@ pub async fn get_emotion_compiler_config( Ok(emotion_compiler_config) } + +#[turbo_tasks::function] +pub async fn get_styled_components_compiler_config( + next_config: NextConfigVc, +) -> Result { + let styled_components_compiler_config = (*next_config.await?) + .compiler + .as_ref() + .map(|value| { + value + .styled_components + .as_ref() + .map(|value| { + let options = match value { + StyledComponentsTransformOptionsOrBoolean::Boolean(true) => Some( + StyledComponentsTransformConfig { + ..Default::default() + } + .cell(), + ), + StyledComponentsTransformOptionsOrBoolean::Boolean(false) => None, + StyledComponentsTransformOptionsOrBoolean::Options(value) => { + Some(value.clone().cell()) + } + }; + + OptionStyledComponentsTransformConfigVc::cell(options) + }) + .unwrap_or_else(|| OptionStyledComponentsTransformConfigVc::cell(None)) + }) + .unwrap_or_else(|| OptionStyledComponentsTransformConfigVc::cell(None)); + + Ok(styled_components_compiler_config) +} diff --git a/packages/next/src/lib/turbopack-warning.ts b/packages/next/src/lib/turbopack-warning.ts index d9df169309f03..38e670d099d2c 100644 --- a/packages/next/src/lib/turbopack-warning.ts +++ b/packages/next/src/lib/turbopack-warning.ts @@ -8,6 +8,7 @@ const supportedTurbopackNextConfigOptions = [ 'env', 'experimental.appDir', 'compiler.emotion', + 'compiler.styledComponents', 'experimental.serverComponentsExternalPackages', 'experimental.turbo', 'images', From 46c0346dcce820ab27acc9b2498efd09f49110e3 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:34:25 -0700 Subject: [PATCH 6/7] build(cargo): update turbopack --- packages/next-swc/Cargo.lock | 60 ++++++++++++++++++------------------ packages/next-swc/Cargo.toml | 6 ++-- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/next-swc/Cargo.lock b/packages/next-swc/Cargo.lock index 84ee3881d93d4..6d4abb41faf2a 100644 --- a/packages/next-swc/Cargo.lock +++ b/packages/next-swc/Cargo.lock @@ -386,7 +386,7 @@ dependencies = [ [[package]] name = "auto-hash-map" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "serde", ] @@ -3302,7 +3302,7 @@ dependencies = [ [[package]] name = "node-file-trace" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "serde", @@ -6687,7 +6687,7 @@ dependencies = [ [[package]] name = "turbo-binding" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "auto-hash-map", "mdxjs", @@ -6724,7 +6724,7 @@ dependencies = [ [[package]] name = "turbo-malloc" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "mimalloc", ] @@ -6732,7 +6732,7 @@ dependencies = [ [[package]] name = "turbo-tasks" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "auto-hash-map", @@ -6762,7 +6762,7 @@ dependencies = [ [[package]] name = "turbo-tasks-build" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "cargo-lock", @@ -6774,7 +6774,7 @@ dependencies = [ [[package]] name = "turbo-tasks-bytes" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "bytes", @@ -6789,7 +6789,7 @@ dependencies = [ [[package]] name = "turbo-tasks-env" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "dotenvy", @@ -6803,7 +6803,7 @@ dependencies = [ [[package]] name = "turbo-tasks-fetch" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "indexmap", @@ -6820,7 +6820,7 @@ dependencies = [ [[package]] name = "turbo-tasks-fs" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "auto-hash-map", @@ -6849,7 +6849,7 @@ dependencies = [ [[package]] name = "turbo-tasks-hash" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "base16", "hex", @@ -6861,7 +6861,7 @@ dependencies = [ [[package]] name = "turbo-tasks-macros" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "convert_case 0.6.0", @@ -6875,7 +6875,7 @@ dependencies = [ [[package]] name = "turbo-tasks-macros-shared" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "proc-macro2", "quote", @@ -6885,7 +6885,7 @@ dependencies = [ [[package]] name = "turbo-tasks-memory" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "auto-hash-map", @@ -6907,7 +6907,7 @@ dependencies = [ [[package]] name = "turbo-tasks-testing" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "auto-hash-map", @@ -6919,7 +6919,7 @@ dependencies = [ [[package]] name = "turbopack" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-recursion", @@ -6945,7 +6945,7 @@ dependencies = [ [[package]] name = "turbopack-cli-utils" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "clap 4.1.11", @@ -6961,7 +6961,7 @@ dependencies = [ [[package]] name = "turbopack-core" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-trait", @@ -6988,7 +6988,7 @@ dependencies = [ [[package]] name = "turbopack-create-test-app" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "clap 4.1.11", @@ -7001,7 +7001,7 @@ dependencies = [ [[package]] name = "turbopack-css" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-trait", @@ -7023,7 +7023,7 @@ dependencies = [ [[package]] name = "turbopack-dev" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "indexmap", @@ -7042,7 +7042,7 @@ dependencies = [ [[package]] name = "turbopack-dev-server" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-compression", @@ -7075,7 +7075,7 @@ dependencies = [ [[package]] name = "turbopack-ecmascript" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-trait", @@ -7111,7 +7111,7 @@ dependencies = [ [[package]] name = "turbopack-env" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "serde", @@ -7126,7 +7126,7 @@ dependencies = [ [[package]] name = "turbopack-json" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "serde", @@ -7141,7 +7141,7 @@ dependencies = [ [[package]] name = "turbopack-mdx" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "mdxjs", @@ -7156,7 +7156,7 @@ dependencies = [ [[package]] name = "turbopack-node" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "async-stream", @@ -7190,7 +7190,7 @@ dependencies = [ [[package]] name = "turbopack-static" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "serde", @@ -7206,7 +7206,7 @@ dependencies = [ [[package]] name = "turbopack-swc-utils" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "swc_core", "turbo-tasks", @@ -7217,7 +7217,7 @@ dependencies = [ [[package]] name = "turbopack-test-utils" version = "0.1.0" -source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230411.2#8a6c4b81f4952627cfac60b1a77240c1ea02ec4b" +source = "git+https://github.com/vercel/turbo.git?tag=turbopack-230412.2#d1df1a810f0fec704ebcf2201691261d1249703d" dependencies = [ "anyhow", "once_cell", diff --git a/packages/next-swc/Cargo.toml b/packages/next-swc/Cargo.toml index c6ac26954d797..adc29e75a7134 100644 --- a/packages/next-swc/Cargo.toml +++ b/packages/next-swc/Cargo.toml @@ -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 From e171cb1c4c2d715ea6c15087aacb7eab4b971a08 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Wed, 12 Apr 2023 10:19:38 -0700 Subject: [PATCH 7/7] refactor(transform): apply fmt --- packages/next-swc/crates/next-core/src/transform_options.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/transform_options.rs b/packages/next-swc/crates/next-core/src/transform_options.rs index 95343635d29d3..05de3fddef40f 100644 --- a/packages/next-swc/crates/next-core/src/transform_options.rs +++ b/packages/next-swc/crates/next-core/src/transform_options.rs @@ -163,7 +163,8 @@ pub async fn get_jsx_transform_options( pub async fn get_emotion_compiler_config( next_config: NextConfigVc, ) -> Result { - let emotion_compiler_config = (*next_config.await?) + let emotion_compiler_config = next_config + .await? .compiler .as_ref() .map(|value| { @@ -196,7 +197,8 @@ pub async fn get_emotion_compiler_config( pub async fn get_styled_components_compiler_config( next_config: NextConfigVc, ) -> Result { - let styled_components_compiler_config = (*next_config.await?) + let styled_components_compiler_config = next_config + .await? .compiler .as_ref() .map(|value| {