Unverified Commit 014b2123 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

Update `swc_core` to `v0.87.28` (#60876)

# Turbopack

* https://github.com/vercel/turbo/pull/7027 <!-- Donny/강동윤 - Update `swc_core` to `v0.87.28` -->

---

### What?

Update swc crates

### Why?

Required for #57718.
`styled-jsx` crate now has a hook to transform CSS code using a
Rust-side API

### How?

Fixes #57718



Closes PACK-2256
parent ccf491dc
This diff is collapsed.
...@@ -30,18 +30,18 @@ next-core = { path = "packages/next-swc/crates/next-core" } ...@@ -30,18 +30,18 @@ next-core = { path = "packages/next-swc/crates/next-core" }
next-custom-transforms = { path = "packages/next-swc/crates/next-custom-transforms" } next-custom-transforms = { path = "packages/next-swc/crates/next-custom-transforms" }
# SWC crates # SWC crates
swc_core = { version = "0.87.16", features = [ swc_core = { version = "0.87.28", features = [
"ecma_loader_lru", "ecma_loader_lru",
"ecma_loader_parking_lot", "ecma_loader_parking_lot",
] } ] }
testing = { version = "0.35.14" } testing = { version = "0.35.14" }
# Turbo crates # Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.3" } turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240124.1" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros.. # [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-240123.3" } turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240124.1" }
# [TODO]: need to refactor embed_directory! macro usage in next-core # [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240123.3" } turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240124.1" }
# General Deps # General Deps
...@@ -92,10 +92,16 @@ indoc = "2.0.0" ...@@ -92,10 +92,16 @@ indoc = "2.0.0"
itertools = "0.10.5" itertools = "0.10.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.17" log = "0.4.17"
lightningcss = { version = "1.0.0-alpha.50", features = [
"serde",
"visitor",
"into_owned",
] }
mime = "0.3.16" mime = "0.3.16"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
once_cell = "1.17.1" once_cell = "1.17.1"
owo-colors = "3.5.0" owo-colors = "3.5.0"
parcel_selectors = "0.26.0"
parking_lot = "0.12.1" parking_lot = "0.12.1"
pathdiff = "0.2.1" pathdiff = "0.2.1"
pin-project-lite = "0.2.9" pin-project-lite = "0.2.9"
......
...@@ -242,13 +242,14 @@ pub async fn get_client_module_options_context( ...@@ -242,13 +242,14 @@ pub async fn get_client_module_options_context(
}); });
let use_lightningcss = *next_config.use_lightningcss().await?; let use_lightningcss = *next_config.use_lightningcss().await?;
let target_browsers = env.runtime_versions();
let source_transforms = vec![ let source_transforms = vec![
*get_swc_ecma_transform_plugin(project_path, next_config).await?, *get_swc_ecma_transform_plugin(project_path, next_config).await?,
*get_relay_transform_plugin(next_config).await?, *get_relay_transform_plugin(next_config).await?,
*get_emotion_transform_plugin(next_config).await?, *get_emotion_transform_plugin(next_config).await?,
*get_styled_components_transform_plugin(next_config).await?, *get_styled_components_transform_plugin(next_config).await?,
*get_styled_jsx_transform_plugin(use_lightningcss).await?, *get_styled_jsx_transform_plugin(use_lightningcss, target_browsers).await?,
] ]
.into_iter() .into_iter()
.flatten() .flatten()
......
...@@ -13,7 +13,9 @@ use turbopack_binding::{ ...@@ -13,7 +13,9 @@ use turbopack_binding::{
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReferences, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReferences,
}, },
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, ServerAddr}, environment::{
Environment, ExecutionEnvironment, NodeJsEnvironment, RuntimeVersions, ServerAddr,
},
free_var_references, free_var_references,
resolve::{parse::Request, pattern::Pattern}, resolve::{parse::Request, pattern::Pattern},
}, },
...@@ -330,11 +332,13 @@ pub async fn get_server_module_options_context( ...@@ -330,11 +332,13 @@ pub async fn get_server_module_options_context(
}); });
let use_lightningcss = *next_config.use_lightningcss().await?; let use_lightningcss = *next_config.use_lightningcss().await?;
let versions = RuntimeVersions(Default::default()).cell();
// EcmascriptTransformPlugins for custom transforms // EcmascriptTransformPlugins for custom transforms
let styled_components_transform_plugin = let styled_components_transform_plugin =
*get_styled_components_transform_plugin(next_config).await?; *get_styled_components_transform_plugin(next_config).await?;
let styled_jsx_transform_plugin = *get_styled_jsx_transform_plugin(use_lightningcss).await?; let styled_jsx_transform_plugin =
*get_styled_jsx_transform_plugin(use_lightningcss, versions).await?;
// ModuleOptionsContext related options // ModuleOptionsContext related options
let tsconfig = get_typescript_transform_options(project_path); let tsconfig = get_typescript_transform_options(project_path);
......
use anyhow::Result; use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::{
ecmascript::OptionTransformPlugin, core::environment::RuntimeVersions, ecmascript::OptionTransformPlugin,
ecmascript_plugin::transform::styled_jsx::StyledJsxTransformer, ecmascript_plugin::transform::styled_jsx::StyledJsxTransformer,
}; };
...@@ -9,8 +9,11 @@ use turbopack_binding::turbopack::{ ...@@ -9,8 +9,11 @@ use turbopack_binding::turbopack::{
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_styled_jsx_transform_plugin( pub async fn get_styled_jsx_transform_plugin(
use_lightningcss: bool, use_lightningcss: bool,
target_browsers: Vc<RuntimeVersions>,
) -> Result<Vc<OptionTransformPlugin>> { ) -> Result<Vc<OptionTransformPlugin>> {
let versions = *target_browsers.await?;
Ok(Vc::cell(Some(Vc::cell( Ok(Vc::cell(Some(Vc::cell(
Box::new(StyledJsxTransformer::new(use_lightningcss)) as _, Box::new(StyledJsxTransformer::new(use_lightningcss, versions)) as _,
)))) ))))
} }
...@@ -26,6 +26,8 @@ serde_json = { workspace = true, features = ["preserve_order"] } ...@@ -26,6 +26,8 @@ serde_json = { workspace = true, features = ["preserve_order"] }
sha1 = "0.10.1" sha1 = "0.10.1"
tracing = { version = "0.1.37" } tracing = { version = "0.1.37" }
anyhow = { workspace = true } anyhow = { workspace = true }
lazy_static = { workspace = true }
lightningcss = {workspace = true}
turbopack-binding = { workspace = true, features = [ turbopack-binding = { workspace = true, features = [
"__swc_core", "__swc_core",
...@@ -38,8 +40,9 @@ turbopack-binding = { workspace = true, features = [ ...@@ -38,8 +40,9 @@ turbopack-binding = { workspace = true, features = [
] } ] }
# To allow quote! macro works # To allow quote! macro works
swc_core = { workspace = true, features = ["ecma_quote"]} swc_core = { workspace = true, features = ["ecma_quote"]}
react_remove_properties = "0.19.0" react_remove_properties = "0.22.0"
remove_console = "0.20.0" remove_console = "0.23.0"
preset_env_base = "0.4.10"
[dev-dependencies] [dev-dependencies]
turbopack-binding = { workspace = true, features = [ turbopack-binding = { workspace = true, features = [
......
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc}; use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use anyhow::bail;
use either::Either; use either::Either;
use fxhash::FxHashSet; use fxhash::FxHashSet;
use lightningcss::targets::Browsers;
use preset_env_base::query::targets_to_versions;
use serde::Deserialize; use serde::Deserialize;
use swc_core::ecma::visit::as_folder; use swc_core::ecma::visit::as_folder;
use turbopack_binding::swc::{ use turbopack_binding::swc::{
...@@ -13,7 +16,11 @@ use turbopack_binding::swc::{ ...@@ -13,7 +16,11 @@ use turbopack_binding::swc::{
FileName, Mark, SourceFile, SourceMap, SyntaxContext, FileName, Mark, SourceFile, SourceMap, SyntaxContext,
}, },
ecma::{ ecma::{
ast::EsVersion, parser::parse_file_as_module, transforms::base::pass::noop, visit::Fold, ast::EsVersion,
parser::parse_file_as_module,
preset_env::{Version, Versions},
transforms::base::pass::noop,
visit::Fold,
}, },
}, },
custom_transform::modularize_imports, custom_transform::modularize_imports,
...@@ -146,6 +153,63 @@ where ...@@ -146,6 +153,63 @@ where
}, },
}; };
let target_browsers = opts
.swc
.config
.env
.as_ref()
.map(|env| targets_to_versions(env.targets.clone()).expect("failed to parse env.targets"))
.unwrap_or_default();
let styled_jsx = if let Some(config) = opts.styled_jsx {
Either::Left(
turbopack_binding::swc::custom_transform::styled_jsx::visitor::styled_jsx(
cm.clone(),
file.name.clone(),
turbopack_binding::swc::custom_transform::styled_jsx::visitor::Config {
use_lightningcss: config.use_lightningcss,
browsers: target_browsers,
},
turbopack_binding::swc::custom_transform::styled_jsx::visitor::NativeConfig {
process_css: if config.use_lightningcss || target_browsers.is_any_target() {
None
} else {
let targets = lightningcss::targets::Targets {
browsers: Some(convert_browsers_to_lightningcss(&target_browsers)),
..Default::default()
};
Some(Box::new(move |css| {
let ss = lightningcss::stylesheet::StyleSheet::parse(
css,
Default::default(),
);
let ss = match ss {
Ok(v) => v,
Err(err) => {
bail!("failed to parse css: {}", err)
}
};
let output = ss.to_css(lightningcss::stylesheet::PrinterOptions {
minify: true,
source_map: None,
project_root: None,
targets,
analyze_dependencies: None,
pseudo_classes: None,
})?;
Ok(output.code)
}))
},
},
),
)
} else {
Either::Right(noop())
};
chain!( chain!(
crate::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file), crate::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file),
match &opts.server_components { match &opts.server_components {
...@@ -158,17 +222,7 @@ where ...@@ -158,17 +222,7 @@ where
)), )),
_ => Either::Right(noop()), _ => Either::Right(noop()),
}, },
if let Some(config) = opts.styled_jsx { styled_jsx,
Either::Left(
turbopack_binding::swc::custom_transform::styled_jsx::visitor::styled_jsx(
cm.clone(),
file.name.clone(),
config,
),
)
} else {
Either::Right(noop())
},
match &opts.styled_components { match &opts.styled_components {
Some(config) => Either::Left( Some(config) => Either::Left(
turbopack_binding::swc::custom_transform::styled_components::styled_components( turbopack_binding::swc::custom_transform::styled_components::styled_components(
...@@ -308,3 +362,21 @@ impl TransformOptions { ...@@ -308,3 +362,21 @@ impl TransformOptions {
self self
} }
} }
fn convert_browsers_to_lightningcss(browsers: &Versions) -> Browsers {
fn convert(v: Option<Version>) -> Option<u32> {
v.map(|v| v.major << 16 | v.minor << 8 | v.patch)
}
Browsers {
android: convert(browsers.android),
chrome: convert(browsers.chrome),
edge: convert(browsers.edge),
firefox: convert(browsers.firefox),
ie: convert(browsers.ie),
ios_saf: convert(browsers.ios),
opera: convert(browsers.opera),
safari: convert(browsers.safari),
samsung: convert(browsers.samsung),
}
}
...@@ -194,7 +194,7 @@ ...@@ -194,7 +194,7 @@
"@types/ws": "8.2.0", "@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0", "@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.2", "@vercel/nft": "0.26.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3", "@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1",
"acorn": "8.5.0", "acorn": "8.5.0",
"amphtml-validator": "1.0.35", "amphtml-validator": "1.0.35",
"anser": "1.4.9", "anser": "1.4.9",
......
...@@ -1077,8 +1077,8 @@ importers: ...@@ -1077,8 +1077,8 @@ importers:
specifier: 0.26.2 specifier: 0.26.2
version: 0.26.2 version: 0.26.2
'@vercel/turbopack-ecmascript-runtime': '@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3 specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3(react-refresh@0.12.0)(webpack@5.86.0)' version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1(react-refresh@0.12.0)(webpack@5.86.0)'
acorn: acorn:
specifier: 8.5.0 specifier: 8.5.0
version: 8.5.0 version: 8.5.0
...@@ -25590,9 +25590,9 @@ packages: ...@@ -25590,9 +25590,9 @@ packages:
/zwitch@2.0.4: /zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3(react-refresh@0.12.0)(webpack@5.86.0)': '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3} resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240123.3' id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240124.1'
name: '@vercel/turbopack-ecmascript-runtime' name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
......
...@@ -226,7 +226,9 @@ export default function (next: NextInstance, render, fetch, ctx) { ...@@ -226,7 +226,9 @@ export default function (next: NextInstance, render, fetch, ctx) {
const styleId = $('#blue-box').attr('class') const styleId = $('#blue-box').attr('class')
const style = $('style') const style = $('style')
expect(style.text().includes(`p.${styleId}{color:blue`)).toBeTruthy() expect(style.text()).toMatch(
new RegExp(`p.${styleId}{color:(?:blue|#00f)`)
)
}) })
test('renders styled jsx external', async () => { test('renders styled jsx external', async () => {
...@@ -234,7 +236,9 @@ export default function (next: NextInstance, render, fetch, ctx) { ...@@ -234,7 +236,9 @@ export default function (next: NextInstance, render, fetch, ctx) {
const styleId = $('#blue-box').attr('class') const styleId = $('#blue-box').attr('class')
const style = $('style') const style = $('style')
expect(style.text().includes(`p.${styleId}{color:blue`)).toBeTruthy() expect(style.text()).toMatch(
new RegExp(`p.${styleId}{color:(?:blue|#00f)`)
)
}) })
test('renders properties populated asynchronously', async () => { test('renders properties populated asynchronously', async () => {
......
...@@ -340,10 +340,10 @@ createNextDescribe( ...@@ -340,10 +340,10 @@ createNextDescribe(
// from styled-jsx // from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)hotpink;?}/) // styled-jsx/css expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components // from styled-components
expect(head).toMatch(/{color:(\s*)blue;?}/) expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
}) })
it('should render initial styles of css-in-js in edge SSR correctly', async () => { it('should render initial styles of css-in-js in edge SSR correctly', async () => {
...@@ -352,10 +352,10 @@ createNextDescribe( ...@@ -352,10 +352,10 @@ createNextDescribe(
// from styled-jsx // from styled-jsx
expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style expect(head).toMatch(/{color:(\s*)purple;?}/) // styled-jsx/style
expect(head).toMatch(/{color:(\s*)hotpink;?}/) // styled-jsx/css expect(head).toMatch(/{color:(\s*)(?:hotpink|#ff69b4);?}/) // styled-jsx/css
// from styled-components // from styled-components
expect(head).toMatch(/{color:(\s*)blue;?}/) expect(head).toMatch(/{color:(\s*)(?:blue|#00f);?}/)
}) })
it('should render css-in-js suspense boundary correctly', async () => { it('should render css-in-js suspense boundary correctly', async () => {
......
...@@ -27,7 +27,7 @@ createNextDescribe( ...@@ -27,7 +27,7 @@ createNextDescribe(
it('should render styled-jsx styles in streaming', async () => { it('should render styled-jsx styles in streaming', async () => {
const html = await renderViaHTTP(next.url, '/') const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('color:blue') expect(html).toMatch(/color:(?:blue|#00f)/)
}) })
it('should redirect paths without trailing-slash and render when slash is appended', async () => { it('should redirect paths without trailing-slash and render when slash is appended', async () => {
......
...@@ -230,7 +230,7 @@ describe('AMP Usage', () => { ...@@ -230,7 +230,7 @@ describe('AMP Usage', () => {
const html = await renderViaHTTP(appPort, '/styled?amp=1') const html = await renderViaHTTP(appPort, '/styled?amp=1')
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('style[amp-custom]').first().text()).toMatch( expect($('style[amp-custom]').first().text()).toMatch(
/div.jsx-[a-zA-Z0-9]{1,}{color:red}span.jsx-[a-zA-Z0-9]{1,}{color:blue}body{background-color:green}/ /div.jsx-[a-zA-Z0-9]{1,}{color:red}span.jsx-[a-zA-Z0-9]{1,}{color:(?:blue|#00f)}body{background-color:green}/
) )
}) })
......
...@@ -21,13 +21,13 @@ export default (context, _render) => { ...@@ -21,13 +21,13 @@ export default (context, _render) => {
context.appPort, context.appPort,
'/use-flush-effect/styled-jsx' '/use-flush-effect/styled-jsx'
) )
const stylesOccurrence = html.match(/color:(\s)*blue/g) || [] const stylesOccurrence = html.match(/color:(\s)*(?:blue|#00f)/g) || []
expect(stylesOccurrence.length).toBe(1) expect(stylesOccurrence.length).toBe(1)
await withBrowser('/use-flush-effect/styled-jsx', async (browser) => { await withBrowser('/use-flush-effect/styled-jsx', async (browser) => {
await check( await check(
() => browser.waitForElementByCss('#__jsx-900f996af369fc74').text(), () => browser.waitForElementByCss('#__jsx-900f996af369fc74').text(),
/blue/ /(?:blue|#00f)/
) )
await check( await check(
() => browser.waitForElementByCss('#__jsx-8b0811664c4e575e').text(), () => browser.waitForElementByCss('#__jsx-8b0811664c4e575e').text(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment