Unverified Commit 5802ba81 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack Build: Implement browserslist for CSS / JS (#80603)

## What?

Implement browserslist for CSS / JS handling in Turbopack.

For JS it's not fully implemented yet, but for CSS it should be good
now.
parent 5c15979b
......@@ -3606,11 +3606,11 @@ dependencies = [
[[package]]
name = "lightningcss"
version = "1.0.0-alpha.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a73ffa17de66534e4b527232f44aa0a89fad22c4f4e0735f9be35494f058e54"
source = "git+https://github.com/timneutkens/lightningcss?branch=add%2Fbrowserslist-options#4e73e2321bd50d254aa81556f7ff2c4f7ac95da8"
dependencies = [
"ahash 0.8.11",
"bitflags 2.9.0",
"browserslist-rs",
"const-str",
"cssparser",
"cssparser-color",
......@@ -3634,8 +3634,7 @@ dependencies = [
[[package]]
name = "lightningcss-derive"
version = "1.0.0-alpha.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12744d1279367caed41739ef094c325d53fb0ffcd4f9b84a368796f870252"
source = "git+https://github.com/timneutkens/lightningcss?branch=add%2Fbrowserslist-options#4e73e2321bd50d254aa81556f7ff2c4f7ac95da8"
dependencies = [
"convert_case",
"proc-macro2",
......@@ -4801,8 +4800,7 @@ dependencies = [
[[package]]
name = "parcel_selectors"
version = "0.28.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54fd03f1ad26cb6b3ec1b7414fa78a3bd639e7dbb421b1a60513c96ce886a196"
source = "git+https://github.com/timneutkens/lightningcss?branch=add%2Fbrowserslist-options#4e73e2321bd50d254aa81556f7ff2c4f7ac95da8"
dependencies = [
"bitflags 2.9.0",
"cssparser",
......@@ -6768,8 +6766,7 @@ dependencies = [
[[package]]
name = "static-self"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6635404b73efc136af3a7956e53c53d4f34b2f16c95a15c438929add0f69412"
source = "git+https://github.com/timneutkens/lightningcss?branch=add%2Fbrowserslist-options#4e73e2321bd50d254aa81556f7ff2c4f7ac95da8"
dependencies = [
"indexmap 2.7.1",
"smallvec",
......@@ -6779,8 +6776,7 @@ dependencies = [
[[package]]
name = "static-self-derive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5268c96d4b907c558a9a52d8492522d6c7b559651a5e1d8f2d551e461b9425d5"
source = "git+https://github.com/timneutkens/lightningcss?branch=add%2Fbrowserslist-options#4e73e2321bd50d254aa81556f7ff2c4f7ac95da8"
dependencies = [
"proc-macro2",
"quote",
......
......@@ -362,6 +362,7 @@ lightningcss = { version = "1.0.0-alpha.66", features = [
"serde",
"visitor",
"into_owned",
"browserslist"
] }
lightningcss-napi = { version = "0.4.4", default-features = false, features = [
"visitor",
......@@ -434,4 +435,6 @@ vergen-gitcl = { version = "1.0.8", features = [
webbrowser = "0.8.7"
[patch.crates-io]
lightningcss = { git = "https://github.com/timneutkens/lightningcss", branch = "add/browserslist-options"}
parcel_selectors = { git = "https://github.com/timneutkens/lightningcss", branch = "add/browserslist-options"}
mdxjs = { git = "https://github.com/kdy1/mdxjs-rs.git", branch = "swc-core-29" }
extern crate turbo_tasks_malloc;
use std::{
env,
fs::{create_dir_all, write},
mem::forget,
path::{Path, PathBuf},
......@@ -183,7 +184,10 @@ impl HmrBenchmark {
project_path: RcStr::from(project_path.clone()),
next_config: load_next_config(),
js_config: RcStr::from("{}"),
env: vec![],
env: vec![(
RcStr::from("PATH"),
RcStr::from(env::var("PATH").unwrap_or_default()),
)],
define_env: DefineEnv {
client: vec![],
edge: vec![],
......
......@@ -242,6 +242,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -255,6 +256,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......@@ -268,6 +270,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -281,6 +284,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......@@ -592,6 +596,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -605,6 +610,7 @@ impl AppProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......
......@@ -450,6 +450,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -465,6 +466,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......@@ -480,6 +482,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -495,6 +498,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......@@ -510,6 +514,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
self.project().server_compile_time_info().environment(),
))
}
......@@ -527,6 +532,7 @@ impl PagesProject {
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
self.project().edge_compile_time_info().environment(),
))
}
......
......@@ -986,6 +986,7 @@ impl Project {
Ok(get_edge_compile_time_info(
self.project_path(),
this.define_env.edge(),
self.env(),
))
}
......@@ -1306,6 +1307,7 @@ impl Project {
self.next_config(),
NextRuntime::Edge,
self.encryption_key(),
self.edge_compile_time_info().environment(),
),
get_edge_resolve_options_context(
self.project_path(),
......@@ -1361,6 +1363,7 @@ impl Project {
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
self.server_compile_time_info().environment(),
),
get_server_resolve_options_context(
self.project_path(),
......@@ -1473,6 +1476,7 @@ impl Project {
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
self.server_compile_time_info().environment(),
),
get_server_resolve_options_context(
self.project_path(),
......@@ -1528,6 +1532,7 @@ impl Project {
self.next_config(),
NextRuntime::Edge,
self.encryption_key(),
self.edge_compile_time_info().environment(),
),
get_edge_resolve_options_context(
self.project_path(),
......
......@@ -342,7 +342,7 @@ pub async fn get_client_module_options_context(
source_maps,
..Default::default()
},
preset_env_versions: Some(env),
environment: Some(env),
execution_context: Some(execution_context),
tree_shaking_mode: tree_shaking_mode_for_user_code,
enable_postcss_transform,
......
use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, OptionVcExt, ResolvedVc, Vc};
use turbo_tasks_env::EnvMap;
use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::FileSystemPath;
use turbopack::{css::chunk::CssChunkType, resolve_options_context::ResolveOptionsContext};
use turbopack_browser::BrowserChunkingContext;
......@@ -14,7 +14,7 @@ use turbopack_core::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReference, FreeVarReferences,
},
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment},
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, NodeJsVersion},
free_var_references,
};
use turbopack_ecmascript::chunk::EcmascriptChunkType;
......@@ -83,10 +83,16 @@ async fn next_edge_free_vars(
pub async fn get_edge_compile_time_info(
project_path: Vc<FileSystemPath>,
define_env: Vc<EnvMap>,
process_env: Vc<Box<dyn ProcessEnv>>,
) -> Result<Vc<CompileTimeInfo>> {
CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::EdgeWorker(
EdgeWorkerEnvironment {}.resolved_cell(),
EdgeWorkerEnvironment {
node_version: NodeJsVersion::resolved_cell(NodeJsVersion::Current(
process_env.to_resolved().await?,
)),
}
.resolved_cell(),
))
.to_resolved()
.await?,
......
......@@ -414,6 +414,7 @@ pub async fn get_server_module_options_context(
next_config: Vc<NextConfig>,
next_runtime: NextRuntime,
encryption_key: ResolvedVc<RcStr>,
environment: ResolvedVc<Environment>,
) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?;
let mut next_server_rules = get_next_server_transforms_rules(
......@@ -553,6 +554,7 @@ pub async fn get_server_module_options_context(
..Default::default()
},
execution_context: Some(execution_context),
environment: Some(environment),
css: CssOptionsContext {
source_maps,
..Default::default()
......
......@@ -28,7 +28,7 @@ export function lightningCssTransformStyleAttribute(
/* auto-generated by NAPI-RS */
export class ExternalObject<T> {
export declare class ExternalObject<T> {
readonly '': {
readonly '': unique symbol
[K: symbol]: T
......
......@@ -21,6 +21,7 @@ import { Telemetry } from '../../telemetry/storage'
import { setGlobal } from '../../trace'
import { isCI } from '../../server/ci-info'
import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'
import { getSupportedBrowsers } from '../utils'
export async function turbopackBuild(): Promise<{
duration: number
......@@ -47,10 +48,7 @@ export async function turbopackBuild(): Promise<{
const bindings = await loadBindings(config?.experimental?.useWasmBinary)
const dev = false
// const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const supportedBrowsers = [
'last 1 Chrome versions, last 1 Firefox versions, last 1 Safari versions, last 1 Edge versions',
]
const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const persistentCaching = isPersistentCachingEnabled(config)
const project = await bindings.turbo.createProject(
......
......@@ -197,7 +197,9 @@ describe('next.rs api', () => {
'.next'
)
project = await bindings.turbo.createProject({
env: {},
env: {
PATH: process.env.PATH,
},
jsConfig: {
compilerOptions: {},
},
......
......@@ -310,21 +310,21 @@ describe('next/font', () => {
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-classname")).fontFamily'
)
).toMatch(/^"Open Sans", system-ui, Arial$/)
).toMatch(/^"Open Sans", .*system-ui.*, Arial$/)
// .style
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-style")).fontFamily'
)
).toMatch(/^"Open Sans", system-ui, Arial$/)
).toMatch(/^"Open Sans", .*system-ui.*, Arial$/)
// .variable
expect(
await browser.eval(
'getComputedStyle(document.querySelector("#with-fallback-fonts-variable")).fontFamily'
)
).toMatch(/^"Open Sans", system-ui, Arial$/)
).toMatch(/^"Open Sans", .*system-ui.*, Arial$/)
})
})
......
......@@ -54,9 +54,15 @@ describe('Browserslist: Old', () => {
.replace(/\/\*.*?\*\/\n?/g, '')
.trim()
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{-webkit-animation:none 0s ease 0s 1 normal none running;animation:none 0s ease 0s 1 normal none running;-webkit-backface-visibility:visible;backface-visibility:visible;background:transparent none repeat 0 0/auto auto padding-box border-box scroll;border:none;border-collapse:separate;-webkit-border-image:none;border-image:none;-webkit-border-radius:0;border-radius:0;border-spacing:0;bottom:auto;-webkit-box-shadow:none;box-shadow:none;-webkit-box-sizing:content-box;box-sizing:content-box;caption-side:top;clear:none;clip:auto;color:#000;-webkit-columns:auto;-webkit-column-count:auto;-webkit-column-fill:balance;column-fill:balance;-webkit-column-gap:normal;column-gap:normal;-webkit-column-rule:medium none currentColor;column-rule:medium none currentColor;-webkit-column-span:1;column-span:1;-webkit-column-width:auto;columns:auto;content:normal;counter-increment:none;counter-reset:none;cursor:auto;direction:ltr;display:inline;empty-cells:show;float:none;font-family:serif;font-size:medium;font-style:normal;font-variant:normal;font-weight:400;font-stretch:normal;line-height:normal;height:auto;-ms-hyphens:none;hyphens:none;left:auto;letter-spacing:normal;list-style:disc none outside;margin:0;max-height:none;max-width:none;min-height:0;min-width:0;opacity:1;orphans:2;outline:medium none invert;overflow:visible;overflow-x:visible;overflow-y:visible;padding:0;page-break-after:auto;page-break-before:auto;page-break-inside:auto;-webkit-perspective:none;perspective:none;-webkit-perspective-origin:50% 50%;perspective-origin:50% 50%;position:static;right:auto;tab-size:8;table-layout:auto;text-align:left;text-align-last:auto;text-decoration:none;text-indent:0;text-shadow:none;text-transform:none;top:auto;-webkit-transform:none;transform:none;-webkit-transform-origin:50% 50% 0;transform-origin:50% 50% 0;-webkit-transform-style:flat;transform-style:flat;-webkit-transition:none 0s ease 0s;transition:none 0s ease 0s;unicode-bidi:normal;vertical-align:baseline;visibility:visible;white-space:normal;widows:2;width:auto;word-spacing:normal;z-index:auto;all:initial}@media (-webkit-min-device-pixel-ratio:2),(min-resolution:2dppx){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
if (process.env.IS_TURBOPACK_TEST) {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{all:initial}@media (-webkit-min-device-pixel-ratio:2),(min-resolution:2dppx){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
} else {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{-webkit-animation:none 0s ease 0s 1 normal none running;animation:none 0s ease 0s 1 normal none running;-webkit-backface-visibility:visible;backface-visibility:visible;background:transparent none repeat 0 0/auto auto padding-box border-box scroll;border:none;border-collapse:separate;-webkit-border-image:none;border-image:none;-webkit-border-radius:0;border-radius:0;border-spacing:0;bottom:auto;-webkit-box-shadow:none;box-shadow:none;-webkit-box-sizing:content-box;box-sizing:content-box;caption-side:top;clear:none;clip:auto;color:#000;-webkit-columns:auto;-webkit-column-count:auto;-webkit-column-fill:balance;column-fill:balance;-webkit-column-gap:normal;column-gap:normal;-webkit-column-rule:medium none currentColor;column-rule:medium none currentColor;-webkit-column-span:1;column-span:1;-webkit-column-width:auto;columns:auto;content:normal;counter-increment:none;counter-reset:none;cursor:auto;direction:ltr;display:inline;empty-cells:show;float:none;font-family:serif;font-size:medium;font-style:normal;font-variant:normal;font-weight:400;font-stretch:normal;line-height:normal;height:auto;-ms-hyphens:none;hyphens:none;left:auto;letter-spacing:normal;list-style:disc none outside;margin:0;max-height:none;max-width:none;min-height:0;min-width:0;opacity:1;orphans:2;outline:medium none invert;overflow:visible;overflow-x:visible;overflow-y:visible;padding:0;page-break-after:auto;page-break-before:auto;page-break-inside:auto;-webkit-perspective:none;perspective:none;-webkit-perspective-origin:50% 50%;perspective-origin:50% 50%;position:static;right:auto;tab-size:8;table-layout:auto;text-align:left;text-align-last:auto;text-decoration:none;text-indent:0;text-shadow:none;text-transform:none;top:auto;-webkit-transform:none;transform:none;-webkit-transform-origin:50% 50% 0;transform-origin:50% 50% 0;-webkit-transform-style:flat;transform-style:flat;-webkit-transition:none 0s ease 0s;transition:none 0s ease 0s;unicode-bidi:normal;vertical-align:baseline;visibility:visible;white-space:normal;widows:2;width:auto;word-spacing:normal;z-index:auto;all:initial}@media (-webkit-min-device-pixel-ratio:2),(min-resolution:2dppx){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
}
})
}
)
......@@ -102,9 +108,15 @@ describe('Browserslist: New', () => {
.replace(/\/\*.*?\*\/\n?/g, '')
.trim()
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{all:initial}@media (min-resolution:2dppx){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
if (process.env.IS_TURBOPACK_TEST) {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{all:initial}@media (resolution>=2x){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
} else {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"a{all:initial}@media (min-resolution:2dppx){.image{background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==)}}"`
)
}
})
}
)
......
......@@ -137,7 +137,7 @@ describe('3rd Party CSS Module Support', () => {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(
`".index-module__jAE1EW__foo{position:relative}.index-module__jAE1EW__foo .bar,.index-module__jAE1EW__foo .baz{height:100%;overflow:hidden}.index-module__jAE1EW__foo .lol{width:80%}.index-module__jAE1EW__foo>.lel{width:80%}"`
`".index-module__jAE1EW__foo{position:relative}.index-module__jAE1EW__foo .bar{height:100%;overflow:hidden}.index-module__jAE1EW__foo .baz{height:100%;overflow:hidden}.index-module__jAE1EW__foo .lol{width:80%}.index-module__jAE1EW__foo>.lel{width:80%}"`
)
} else {
expect(
......
......@@ -79,11 +79,11 @@ module.exports = {
if (process.env.IS_TURBOPACK_TEST && useLightningcss) {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"@media (480px<=width<768px){::placeholder{color:green}}.flex-parsing{flex:0 0 calc(50% - var(--vertical-gutter))}.transform-parsing{transform:translate3d(0px,0px)}.css-grid-shorthand{grid-column:span 2}.g-docs-sidenav .filter::-webkit-input-placeholder{opacity:.8}"`
`"@media (min-width:480px) and (not (min-width:768px)){::placeholder{color:green}}.flex-parsing{flex:0 0 calc(50% - var(--vertical-gutter))}.transform-parsing{transform:translate3d(0px,0px)}.css-grid-shorthand{grid-column:span 2}.g-docs-sidenav .filter::-webkit-input-placeholder{opacity:.8}"`
)
} else if (process.env.IS_TURBOPACK_TEST && !useLightningcss) {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
`"@media (480px<=width<768px){::placeholder{color:green}}.flex-parsing{flex:0 0 calc(50% - var(--vertical-gutter))}.transform-parsing{transform:translate3d(0px,0px)}.css-grid-shorthand{grid-column:span 2}.g-docs-sidenav .filter::-webkit-input-placeholder{opacity:.8}"`
`"@media (min-width:480px) and (not (min-width:768px)){::placeholder{color:green}}.flex-parsing{flex:0 0 calc(50% - var(--vertical-gutter))}.transform-parsing{transform:translate3d(0px,0px)}.css-grid-shorthand{grid-column:span 2}.g-docs-sidenav .filter::-webkit-input-placeholder{opacity:.8}"`
)
} else if (useLightningcss) {
expect(cssContentWithoutSourceMap).toMatchInlineSnapshot(
......@@ -119,7 +119,7 @@ module.exports = {
if (process.env.IS_TURBOPACK_TEST && useLightningcss) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{
"mappings": "AAAA,4BACE,2BAKF,0DAIA,kDAIA,uCAIA",
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
"names": [],
"sourcesContent": [
"@media (480px <= width < 768px) {
......@@ -151,7 +151,7 @@ module.exports = {
} else if (process.env.IS_TURBOPACK_TEST && !useLightningcss) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{
"mappings": "AAAA,4BACE,2BAKF,0DAIA,kDAIA,uCAIA",
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
"names": [],
"sourcesContent": [
"@media (480px <= width < 768px) {
......
......@@ -111,7 +111,7 @@ async fn get_client_module_options_context(
) -> Result<Vc<ModuleOptionsContext>> {
let is_dev = matches!(*node_env.await?, NodeEnv::Development);
let module_options_context = ModuleOptionsContext {
preset_env_versions: Some(env),
environment: Some(env),
execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
keep_last_successful_parse: is_dev,
......
......@@ -4,6 +4,7 @@ use std::{
};
use anyhow::{Context, Result, anyhow};
use browserslist::Distrib;
use swc_core::ecma::preset_env::{Version, Versions};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TaskInput, Vc};
......@@ -62,6 +63,16 @@ pub enum ExecutionEnvironment {
Custom(u8),
}
async fn resolve_browserslist(browser_env: ResolvedVc<BrowserEnvironment>) -> Result<Vec<Distrib>> {
Ok(browserslist::resolve(
browser_env.await?.browserslist_query.split(','),
&browserslist::Opts {
ignore_unknown_versions: true,
..Default::default()
},
)?)
}
#[turbo_tasks::value_impl]
impl Environment {
#[turbo_tasks::function]
......@@ -81,12 +92,31 @@ impl Environment {
ExecutionEnvironment::NodeJsBuildTime(node_env, ..)
| ExecutionEnvironment::NodeJsLambda(node_env) => node_env.runtime_versions(),
ExecutionEnvironment::Browser(browser_env) => {
Vc::cell(Versions::parse_versions(browserslist::resolve(
browser_env.await?.browserslist_query.split(','),
&browserslist::Opts::default(),
)?)?)
let distribs = resolve_browserslist(browser_env).await?;
Vc::cell(Versions::parse_versions(distribs)?)
}
ExecutionEnvironment::EdgeWorker(edge_env) => edge_env.runtime_versions(),
ExecutionEnvironment::Custom(_) => todo!(),
})
}
#[turbo_tasks::function]
pub async fn browserslist_query(&self) -> Result<Vc<RcStr>> {
Ok(match self.execution {
ExecutionEnvironment::NodeJsBuildTime(_)
| ExecutionEnvironment::NodeJsLambda(_)
| ExecutionEnvironment::EdgeWorker(_) =>
// TODO: This is a hack, browserslist_query is only used by CSS processing for
// LightningCSS However, there is an issue where the CSS is not transitioned
// to the client which we still have to solve. It does apply the
// browserslist correctly because CSS Modules in client components is double-processed,
// once for server once for browser.
{
Vc::cell("".into())
}
ExecutionEnvironment::Browser(browser_env) => {
Vc::cell(browser_env.await?.browserslist_query.clone())
}
ExecutionEnvironment::EdgeWorker(_) => todo!(),
ExecutionEnvironment::Custom(_) => todo!(),
})
}
......@@ -292,7 +322,28 @@ pub struct BrowserEnvironment {
}
#[turbo_tasks::value(shared)]
pub struct EdgeWorkerEnvironment {}
pub struct EdgeWorkerEnvironment {
pub node_version: ResolvedVc<NodeJsVersion>,
}
#[turbo_tasks::value_impl]
impl EdgeWorkerEnvironment {
#[turbo_tasks::function]
pub async fn runtime_versions(&self) -> Result<Vc<RuntimeVersions>> {
let str = match *self.node_version.await? {
NodeJsVersion::Current(process_env) => get_current_nodejs_version(*process_env),
NodeJsVersion::Static(version) => *version,
}
.await?;
Ok(Vc::cell(Versions {
node: Some(
Version::from_str(&str).map_err(|_| anyhow!("Node.js version parse error"))?,
),
..Default::default()
}))
}
}
// TODO preset_env_base::Version implements Serialize/Deserialize incorrectly
#[turbo_tasks::value(transparent, serialization = "none")]
......
......@@ -6,6 +6,7 @@ use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MinifyType},
context::AssetContext,
environment::Environment,
ident::AssetIdent,
module::{Module, OptionStyleType, StyleType},
module_graph::ModuleGraph,
......@@ -38,6 +39,7 @@ pub struct CssModuleAsset {
import_context: Option<ResolvedVc<ImportContext>>,
ty: CssModuleAssetType,
minify_type: MinifyType,
environment: Option<ResolvedVc<Environment>>,
}
#[turbo_tasks::value_impl]
......@@ -50,6 +52,7 @@ impl CssModuleAsset {
ty: CssModuleAssetType,
minify_type: MinifyType,
import_context: Option<ResolvedVc<ImportContext>>,
environment: Option<ResolvedVc<Environment>>,
) -> Vc<Self> {
Self::cell(CssModuleAsset {
source,
......@@ -57,10 +60,11 @@ impl CssModuleAsset {
import_context,
ty,
minify_type,
environment,
})
}
/// Retrns the asset ident of the source without the "css" modifier
/// Returns the asset ident of the source without the "css" modifier
#[turbo_tasks::function]
pub fn source_ident(&self) -> Vc<AssetIdent> {
self.source.ident()
......@@ -78,6 +82,7 @@ impl ParseCss for CssModuleAsset {
Vc::upcast(self),
this.import_context.map(|v| *v),
this.ty,
this.environment.as_deref().copied(),
))
}
}
......@@ -85,10 +90,14 @@ impl ParseCss for CssModuleAsset {
#[turbo_tasks::value_impl]
impl ProcessCss for CssModuleAsset {
#[turbo_tasks::function]
fn get_css_with_placeholder(self: Vc<Self>) -> Vc<CssWithPlaceholderResult> {
async fn get_css_with_placeholder(self: Vc<Self>) -> Result<Vc<CssWithPlaceholderResult>> {
let this = self.await?;
let parse_result = self.parse_css();
process_css_with_placeholder(parse_result)
Ok(process_css_with_placeholder(
parse_result,
this.environment.as_deref().copied(),
))
}
#[turbo_tasks::function]
......@@ -99,8 +108,9 @@ impl ProcessCss for CssModuleAsset {
) -> Result<Vc<FinalCssResult>> {
let process_result = self.get_css_with_placeholder();
let this = self.await?;
let origin_source_map =
match ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(self.await?.source) {
match ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(this.source) {
Some(gsm) => gsm.generate_source_map(),
None => Vc::cell(None),
};
......@@ -109,6 +119,7 @@ impl ProcessCss for CssModuleAsset {
chunking_context,
minify_type,
origin_source_map,
this.environment.as_deref().copied(),
))
}
}
......
......@@ -3,8 +3,8 @@ use std::sync::{Arc, RwLock};
use anyhow::{Context, Result, bail};
use lightningcss::{
css_modules::{CssModuleExport, CssModuleExports, Pattern, Segment},
stylesheet::{ParserOptions, PrinterOptions, StyleSheet, ToCssResult},
targets::{Features, Targets},
stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet, ToCssResult},
targets::{BrowserslistConfig, Features, Targets},
traits::ToCss,
values::url::Url,
visit_types,
......@@ -21,6 +21,7 @@ use turbopack_core::{
SOURCE_URL_PROTOCOL,
asset::{Asset, AssetContent},
chunk::{ChunkingContext, MinifyType},
environment::Environment,
issue::{
Issue, IssueExt, IssueSource, IssueStage, OptionIssueSource, OptionStyledString,
StyledString,
......@@ -53,6 +54,51 @@ impl PartialEq for StyleSheetLike<'_, '_> {
pub type CssOutput = (ToCssResult, Option<Rope>);
#[turbo_tasks::value(transparent, serialization = "none", eq = "manual")]
struct LightningCssTargets(#[turbo_tasks(trace_ignore)] pub Targets);
impl PartialEq for LightningCssTargets {
fn eq(&self, _: &Self) -> bool {
false
}
}
/// Returns the LightningCSS targets for the given browserslist query.
#[turbo_tasks::function]
async fn get_lightningcss_browser_targets(
environment: Option<ResolvedVc<Environment>>,
handle_nesting: bool,
) -> Result<Vc<LightningCssTargets>> {
match environment {
Some(environment) => {
let browserslist_query = (*environment.browserslist_query().await?).clone();
let browserslist_browsers =
lightningcss::targets::Browsers::from_browserslist_with_config(
browserslist_query.split(','),
BrowserslistConfig {
ignore_unknown_versions: true,
..Default::default()
},
)?;
Ok(if handle_nesting {
Vc::cell(Targets {
browsers: browserslist_browsers,
include: Features::Nesting,
..Default::default()
})
} else {
Vc::cell(Targets {
browsers: browserslist_browsers,
..Default::default()
})
})
}
// Default when empty environment is passed.
None => Ok(Vc::cell(Default::default())),
}
}
impl StyleSheetLike<'_, '_> {
pub fn to_static(
&self,
......@@ -61,13 +107,14 @@ impl StyleSheetLike<'_, '_> {
StyleSheetLike(stylesheet_into_static(&self.0, options))
}
pub fn to_css(
pub async fn to_css(
&self,
code: &str,
minify_type: MinifyType,
enable_srcmap: bool,
handle_nesting: bool,
mut origin_source_map: Option<parcel_sourcemap::SourceMap>,
environment: Option<ResolvedVc<Environment>>,
) -> Result<CssOutput> {
let ss = &self.0;
let mut srcmap = if enable_srcmap {
......@@ -76,14 +123,9 @@ impl StyleSheetLike<'_, '_> {
None
};
let targets = if handle_nesting {
Targets {
include: Features::Nesting,
..Default::default()
}
} else {
Default::default()
};
let targets =
*get_lightningcss_browser_targets(environment.as_deref().copied(), handle_nesting)
.await?;
let result = ss.to_css(PrinterOptions {
minify: matches!(minify_type, MinifyType::Minify { .. }),
......@@ -180,6 +222,7 @@ impl PartialEq for FinalCssResult {
#[turbo_tasks::function]
pub async fn process_css_with_placeholder(
parse_result: ResolvedVc<ParseCssResult>,
environment: Option<ResolvedVc<Environment>>,
) -> Result<Vc<CssWithPlaceholderResult>> {
let result = parse_result.await?;
......@@ -199,7 +242,9 @@ pub async fn process_css_with_placeholder(
// We use NoMinify because this is not a final css. We need to replace url references,
// and we do final codegen with proper minification.
let (result, _) = stylesheet.to_css(&code, MinifyType::NoMinify, false, false, None)?;
let (result, _) = stylesheet
.to_css(&code, MinifyType::NoMinify, false, false, None, environment)
.await?;
let exports = result.exports.map(|exports| {
let mut exports = exports.into_iter().collect::<FxIndexMap<_, _>>();
......@@ -229,6 +274,7 @@ pub async fn finalize_css(
chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType,
origin_source_map: Vc<OptionStringifiedSourceMap>,
environment: Option<ResolvedVc<Environment>>,
) -> Result<Vc<FinalCssResult>> {
let result = result.await?;
match &*result {
......@@ -273,8 +319,16 @@ pub async fn finalize_css(
None
};
let (result, srcmap) =
stylesheet.to_css(&code, minify_type, true, true, origin_source_map)?;
let (result, srcmap) = stylesheet
.to_css(
&code,
minify_type,
true,
true,
origin_source_map,
environment,
)
.await?;
Ok(FinalCssResult::Ok {
output_code: result.code,
......@@ -313,6 +367,7 @@ pub async fn parse_css(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
import_context: Option<ResolvedVc<ImportContext>>,
ty: CssModuleAssetType,
environment: Option<ResolvedVc<Environment>>,
) -> Result<Vc<ParseCssResult>> {
let span = {
let name = source.ident().to_string().await?.to_string();
......@@ -338,6 +393,7 @@ pub async fn parse_css(
origin,
import_context,
ty,
environment,
)
.await?
}
......@@ -358,6 +414,7 @@ async fn process_content(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
import_context: Option<ResolvedVc<ImportContext>>,
ty: CssModuleAssetType,
environment: Option<ResolvedVc<Environment>>,
) -> Result<Vc<ParseCssResult>> {
#[allow(clippy::needless_lifetimes)]
fn without_warnings<'o, 'i>(config: ParserOptions<'o, 'i>) -> ParserOptions<'o, 'static> {
......@@ -452,13 +509,20 @@ async fn process_content(
}
}
let targets =
*get_lightningcss_browser_targets(environment.as_deref().copied(), true)
.await?;
// minify() is actually transform, and it performs operations like CSS modules
// handling.
//
//
// See: https://github.com/parcel-bundler/lightningcss/issues/935#issuecomment-2739325537
ss.minify(Default::default())
.context("failed to transform css")?;
ss.minify(MinifyOptions {
targets,
..Default::default()
})
.context("failed to transform css")?;
stylesheet_into_static(&ss, without_warnings(config.clone()))
}
......
use turbo_rcstr::rcstr;
use turbo_tasks::{Result, Vc};
use turbo_tasks::{ResolvedVc, Result, Vc};
use turbopack::{
ModuleAssetContext,
module_options::{EcmascriptOptionsContext, ModuleOptionsContext, TypescriptTransformOptions},
......@@ -11,7 +11,7 @@ use turbopack_ecmascript::TreeShakingMode;
/// Returns the runtime asset context to use to process runtime code assets.
pub async fn get_runtime_asset_context(
environment: Vc<Environment>,
environment: ResolvedVc<Environment>,
) -> Result<Vc<Box<dyn AssetContext>>> {
let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
......@@ -21,14 +21,13 @@ pub async fn get_runtime_asset_context(
..Default::default()
},
// TODO: Somehow this fails to compile when enabled.
// preset_env_versions: Some(environment),
// environment: Some(environment),
environment: None,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
..Default::default()
}
.cell();
let compile_time_info = CompileTimeInfo::builder(environment.to_resolved().await?)
.cell()
.await?;
let compile_time_info = CompileTimeInfo::builder(environment).cell().await?;
let asset_context: Vc<Box<dyn AssetContext>> = Vc::upcast(ModuleAssetContext::new(
Default::default(),
......
......@@ -3,7 +3,7 @@ use std::io::Write;
use anyhow::Result;
use indoc::writedoc;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
code_builder::{Code, CodeBuilder},
context::AssetContext,
......@@ -16,7 +16,7 @@ use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::emb
/// Returns the code for the ECMAScript runtime.
#[turbo_tasks::function]
pub async fn get_browser_runtime_code(
environment: Vc<Environment>,
environment: ResolvedVc<Environment>,
chunk_base_path: Option<RcStr>,
chunk_suffix_path: Option<RcStr>,
runtime_type: RuntimeType,
......
use anyhow::Result;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
code_builder::{Code, CodeBuilder},
environment::Environment,
......@@ -10,7 +10,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod
/// Returns the code for the Node.js production ECMAScript runtime.
#[turbo_tasks::function]
pub async fn get_nodejs_runtime_code(
environment: Vc<Environment>,
environment: ResolvedVc<Environment>,
generate_source_map: bool,
) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(environment).await?;
......
......@@ -373,7 +373,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
import_externals: true,
..Default::default()
},
preset_env_versions: Some(env),
environment: Some(env),
tree_shaking_mode: options.tree_shaking_mode,
rules: vec![(
ContextCondition::InDirectory("node_modules".into()),
......
......@@ -303,14 +303,14 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
css: CssOptionsContext {
..Default::default()
},
preset_env_versions: Some(env),
environment: Some(env),
rules: vec![(
ContextCondition::InDirectory("node_modules".into()),
ModuleOptionsContext {
css: CssOptionsContext {
..Default::default()
},
preset_env_versions: Some(env),
environment: Some(env),
tree_shaking_mode: options.tree_shaking_mode,
remove_unused_exports: options.remove_unused_exports,
..Default::default()
......
......@@ -236,7 +236,7 @@ async fn apply_module_type(
.await?,
),
ModuleType::Css { ty } => ResolvedVc::upcast(
ModuleType::Css { ty, environment } => ResolvedVc::upcast(
CssModuleAsset::new(
*source,
Vc::upcast(module_asset_context),
......@@ -247,6 +247,7 @@ async fn apply_module_type(
.css
.minify_type,
css_import_context,
environment.as_deref().copied(),
)
.to_resolved()
.await?,
......@@ -564,7 +565,7 @@ async fn process_default_internal(
}
}
ModuleRuleEffect::ModuleType(module) => {
current_module_type = Some(*module);
current_module_type = Some(module.clone());
}
ModuleRuleEffect::ExtendEcmascriptTransforms { prepend, append } => {
current_module_type = match current_module_type {
......@@ -687,6 +688,9 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
source_maps: SourceMapsType::None,
..Default::default()
},
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
..Default::default()
}
.cell(),
......
......@@ -139,7 +139,7 @@ impl ModuleOptions {
},
ref enable_postcss_transform,
ref enable_webpack_loaders,
preset_env_versions,
environment,
ref module_rules,
execution_context,
tree_shaking_mode,
......@@ -180,8 +180,8 @@ impl ModuleOptions {
};
let ecmascript_options_vc = ecmascript_options.resolved_cell();
if let Some(env) = preset_env_versions {
transforms.push(EcmascriptInputTransform::PresetEnv(env));
if let Some(environment) = environment {
transforms.push(EcmascriptInputTransform::PresetEnv(environment));
}
if let Some(enable_typeof_window_inlining) = enable_typeof_window_inlining {
......@@ -446,6 +446,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Default,
environment,
})],
),
ModuleRule::new(
......@@ -455,6 +456,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
environment,
})],
),
]);
......@@ -507,6 +509,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Default,
environment,
})],
),
ModuleRule::new(
......@@ -537,6 +540,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
environment,
})],
),
// Ecmascript CSS Modules referencing the actual CSS module to include it
......@@ -547,6 +551,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
environment,
})],
),
// Ecmascript CSS Modules referencing the actual CSS module to list the classes
......@@ -562,6 +567,7 @@ impl ModuleOptions {
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
environment,
})],
),
]);
......
......@@ -147,7 +147,7 @@ pub struct ModuleOptionsContext {
pub enable_mdx: bool,
pub enable_mdx_rs: Option<ResolvedVc<MdxTransformOptions>>,
pub preset_env_versions: Option<ResolvedVc<Environment>>,
pub environment: Option<ResolvedVc<Environment>>,
pub execution_context: Option<ResolvedVc<ExecutionContext>>,
pub side_effect_free_packages: Vec<RcStr>,
pub tree_shaking_mode: Option<TreeShakingMode>,
......
......@@ -3,7 +3,8 @@ use serde::{Deserialize, Serialize};
use turbo_tasks::{NonLocalValue, ResolvedVc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
reference_type::ReferenceType, source::Source, source_transform::SourceTransforms,
environment::Environment, reference_type::ReferenceType, source::Source,
source_transform::SourceTransforms,
};
use turbopack_css::CssModuleAssetType;
use turbopack_ecmascript::{EcmascriptInputTransforms, EcmascriptOptions};
......@@ -77,7 +78,7 @@ pub enum ModuleRuleEffect {
}
#[turbo_tasks::value(shared)]
#[derive(Hash, Debug, Copy, Clone)]
#[derive(Hash, Debug, Clone)]
pub enum ModuleType {
Ecmascript {
transforms: ResolvedVc<EcmascriptInputTransforms>,
......@@ -103,6 +104,7 @@ pub enum ModuleType {
CssModule,
Css {
ty: CssModuleAssetType,
environment: Option<ResolvedVc<Environment>>,
},
StaticUrlJs,
StaticUrlCss,
......
......@@ -337,14 +337,15 @@ async fn node_file_trace_operation(
let output_dir = output_fs.root().to_resolved().await?;
let source = FileSource::new(input);
let environment = Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment::default().resolved_cell(),
));
let module_asset_context = ModuleAssetContext::new(
Default::default(),
// TODO It's easy to make a mistake here as this should match the config in the
// binary. TODO These test cases should move into the
// `node-file-trace` crate and use the same config.
CompileTimeInfo::new(Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment::default().resolved_cell(),
))),
CompileTimeInfo::new(environment),
ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_types: true,
......@@ -354,6 +355,9 @@ async fn node_file_trace_operation(
enable_raw_css: true,
..Default::default()
},
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
..Default::default()
}
.cell(),
......
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