Unverified Commit 15a05670 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Remove undocumented legacy syntax for built-in conditions (e.g.…

Turbopack: Remove undocumented legacy syntax for built-in conditions (e.g. foreign, browser) (#83068)

- https://github.com/vercel/next.js/pull/82857 provides a better way to do this that we intend to document.
- This syntax was never documented (outside of the types and tests). You wouldn't be able to figure out the built-in conditions without digging through the Turbopack source code.
parent 040e4676
use std::{collections::BTreeSet, str::FromStr};
use anyhow::{Context, Result, bail};
use either::Either;
use rustc_hash::FxHashSet;
......@@ -667,21 +665,11 @@ impl TryFrom<ConfigConditionItem> for ConditionItem {
}
}
#[derive(
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue,
)]
#[serde(rename_all = "camelCase", untagged)]
pub enum RuleConfigItem {
Options(RuleConfigItemOptions),
LegacyConditional(FxIndexMap<RcStr, RuleConfigItem>),
LegacyBoolean(bool),
}
#[derive(
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue,
)]
#[serde(rename_all = "camelCase")]
pub struct RuleConfigItemOptions {
pub struct RuleConfigItem {
pub loaders: Vec<LoaderItem>,
#[serde(default, alias = "as")]
pub rename_as: Option<RcStr>,
......@@ -1401,11 +1389,7 @@ impl NextConfig {
}
#[turbo_tasks::function]
pub async fn webpack_rules(
&self,
active_conditions: BTreeSet<WebpackLoaderBuiltinCondition>,
project_path: FileSystemPath,
) -> Result<Vc<WebpackRules>> {
pub async fn webpack_rules(&self, project_path: FileSystemPath) -> Result<Vc<WebpackRules>> {
let Some(turbo_rules) = self.turbopack.as_ref().and_then(|t| t.rules.as_ref()) else {
return Ok(Vc::cell(Vec::new()));
};
......@@ -1429,43 +1413,6 @@ impl NextConfig {
.collect(),
)
}
enum FindRuleResult<'a> {
Found(&'a RuleConfigItemOptions),
NotFound,
Break,
}
// This logic is needed for the `LegacyConditional`/`LegacyBoolean` configuration
// syntax. This is technically public syntax, but was never documented and it is
// unlikely that anyone is depending on it (outside of some Next.js internals).
fn find_rule<'a>(
rule: &'a RuleConfigItem,
active_conditions: &BTreeSet<WebpackLoaderBuiltinCondition>,
) -> FindRuleResult<'a> {
match rule {
RuleConfigItem::Options(rule) => FindRuleResult::Found(rule),
RuleConfigItem::LegacyConditional(map) => {
for (condition, rule) in map.iter() {
let condition = WebpackLoaderBuiltinCondition::from_str(condition);
if let Ok(condition) = condition
&& (condition == WebpackLoaderBuiltinCondition::Default
|| active_conditions.contains(&condition))
{
match find_rule(rule, active_conditions) {
FindRuleResult::Found(rule) => {
return FindRuleResult::Found(rule);
}
FindRuleResult::Break => {
return FindRuleResult::Break;
}
FindRuleResult::NotFound => {}
}
}
}
FindRuleResult::NotFound
}
RuleConfigItem::LegacyBoolean(_) => FindRuleResult::Break,
}
}
let config_file_path = || project_path.join(&self.config_file_name);
for item in &rule_collection.0 {
match item {
......@@ -1479,55 +1426,52 @@ impl NextConfig {
},
));
}
RuleConfigCollectionItem::Full(rule_config_item) => {
if let FindRuleResult::Found(RuleConfigItemOptions {
loaders,
rename_as,
condition,
}) = find_rule(rule_config_item, &active_conditions)
RuleConfigCollectionItem::Full(RuleConfigItem {
loaders,
rename_as,
condition,
}) => {
// If the extension contains a wildcard, and the rename_as does not,
// emit an issue to prevent users from encountering duplicate module
// names.
if glob.contains("*")
&& let Some(rename_as) = rename_as.as_ref()
&& !rename_as.contains("*")
{
// If the extension contains a wildcard, and the rename_as does not,
// emit an issue to prevent users from encountering duplicate module
// names.
if glob.contains("*")
&& let Some(rename_as) = rename_as.as_ref()
&& !rename_as.contains("*")
{
InvalidLoaderRuleRenameAsIssue {
glob: glob.clone(),
InvalidLoaderRuleRenameAsIssue {
glob: glob.clone(),
config_file_path: config_file_path()?,
rename_as: rename_as.clone(),
}
.resolved_cell()
.emit();
}
// convert from Next.js-specific condition type to internal Turbopack
// condition type
let condition = if let Some(condition) = condition {
if let Ok(cond) = ConditionItem::try_from(condition.clone()) {
Some(cond)
} else {
InvalidLoaderRuleConditionIssue {
condition: condition.clone(),
config_file_path: config_file_path()?,
rename_as: rename_as.clone(),
}
.resolved_cell()
.emit();
}
// convert from Next.js-specific condition type to internal Turbopack
// condition type
let condition = if let Some(condition) = condition {
if let Ok(cond) = ConditionItem::try_from(condition.clone()) {
Some(cond)
} else {
InvalidLoaderRuleConditionIssue {
condition: condition.clone(),
config_file_path: config_file_path()?,
}
.resolved_cell()
.emit();
None
}
} else {
None
};
rules.push((
glob.clone(),
LoaderRuleItem {
loaders: transform_loaders(&mut loaders.iter()),
rename_as: rename_as.clone(),
condition,
},
));
}
}
} else {
None
};
rules.push((
glob.clone(),
LoaderRuleItem {
loaders: transform_loaders(&mut loaders.iter()),
rename_as: rename_as.clone(),
condition,
},
));
}
}
}
......@@ -2001,11 +1945,11 @@ mod tests {
}
});
let rule_config: RuleConfigItemOptions = serde_json::from_value(json_value).unwrap();
let rule_config: RuleConfigItem = serde_json::from_value(json_value).unwrap();
assert_eq!(
rule_config,
RuleConfigItemOptions {
RuleConfigItem {
loaders: vec![],
rename_as: Some(rcstr!("*.js")),
condition: Some(ConfigConditionItem::All(
......
......@@ -5,7 +5,7 @@ use regex::Regex;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{self, FileSystemEntryType, FileSystemPath};
use turbopack::module_options::LoaderRuleItem;
use turbopack::module_options::{ConditionItem, LoaderRuleItem};
use turbopack_core::{
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
reference_type::{CommonJsReferenceSubType, ReferenceType},
......@@ -13,6 +13,8 @@ use turbopack_core::{
};
use turbopack_node::transforms::webpack::WebpackLoaderItem;
use crate::next_shared::webpack_rules::WebpackLoaderBuiltinCondition;
// https://babeljs.io/docs/config-files
// TODO: Also support a `babel` key in a package.json file
const BABEL_CONFIG_FILES: &[&str] = &[
......@@ -89,7 +91,9 @@ pub async fn get_babel_loader_rules(
options: Default::default(),
}]),
rename_as: Some(rcstr!("*")),
condition: None,
condition: Some(ConditionItem::Not(Box::new(ConditionItem::Builtin(
RcStr::from(WebpackLoaderBuiltinCondition::Foreign.as_str()),
)))),
},
)])
}
......
......@@ -149,7 +149,7 @@ pub async fn webpack_loader_options(
builtin_conditions: BTreeSet<WebpackLoaderBuiltinCondition>,
) -> Result<Vc<OptionWebpackLoadersOptions>> {
let mut rules = next_config
.webpack_rules(builtin_conditions.clone(), project_path.clone())
.webpack_rules(project_path.clone())
.owned()
.await?;
......
......@@ -16,7 +16,6 @@ import type {
TurbopackRuleCondition,
TurbopackRuleConfigCollection,
TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
} from '../../server/config-shared'
import { isDeepStrictEqual } from 'util'
import { type DefineEnvOptions, getDefineEnv } from '../define-env'
......@@ -836,29 +835,32 @@ function bindingToApi(
for (const key of ruleKeys) {
nextConfig.turbopack.conditions[`#reactCompiler/${key}`] = {
path: key,
content:
options.compilationMode === 'annotation'
? /['"]use memo['"]/
: !options.compilationMode ||
options.compilationMode === 'infer'
? // Matches declaration or useXXX or </ (closing jsx) or /> (self closing jsx)
/['"]use memo['"]|\Wuse[A-Z]|<\/|\/>/
: undefined,
all: [
'browser',
{ not: 'foreign' },
{
path: key,
content:
options.compilationMode === 'annotation'
? /['"]use memo['"]/
: !options.compilationMode ||
options.compilationMode === 'infer'
? // Matches declaration or useXXX or </ (closing jsx) or /> (self closing jsx)
/['"]use memo['"]|\Wuse[A-Z]|<\/|\/>/
: undefined,
},
],
}
nextConfig.turbopack.rules[`#reactCompiler/${key}`] = {
browser: {
foreign: false,
loaders: [
getReactCompilerLoader(
reactCompilerOptions,
projectPath,
nextConfig.dev,
/* isServer */ false,
/* reactCompilerExclude */ undefined
),
],
},
loaders: [
getReactCompilerLoader(
reactCompilerOptions,
projectPath,
nextConfig.dev,
/* isServer */ false,
/* reactCompilerExclude */ undefined
),
],
}
}
}
......@@ -1039,26 +1041,14 @@ function bindingToApi(
glob: string
): any {
if (!rule) return rule
for (const item of rule.loaders) {
checkLoaderItem(item, glob)
}
let serializedRule: any = rule
if ('loaders' in rule) {
const narrowedRule = rule as TurbopackRuleConfigItemOptions
for (const item of narrowedRule.loaders) {
checkLoaderItem(item, glob)
}
if (narrowedRule.condition != null) {
serializedRule = {
...rule,
condition: serializeRuleCondition(narrowedRule.condition),
}
}
} else {
serializedRule = {}
for (const [key, value] of Object.entries(rule)) {
if (typeof value === 'object' && value) {
serializedRule[key] = serializeConfigItem(value, glob)
} else {
serializedRule[key] = value
}
if (rule.condition != null) {
serializedRule = {
...rule,
condition: serializeRuleCondition(rule.condition),
}
}
return serializedRule
......
......@@ -11,7 +11,6 @@ import type {
DeprecatedExperimentalTurboOptions,
TurbopackOptions,
TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
TurbopackRuleConfigCollection,
TurbopackRuleCondition,
TurbopackLoaderBuiltinCondition,
......@@ -135,26 +134,17 @@ const zTurbopackCondition: zod.ZodType<TurbopackRuleCondition> = z.union([
}),
])
const zTurbopackRuleConfigItemOptions: zod.ZodType<TurbopackRuleConfigItemOptions> =
const zTurbopackRuleConfigItem: zod.ZodType<TurbopackRuleConfigItem> =
z.strictObject({
loaders: z.array(zTurbopackLoaderItem),
as: z.string().optional(),
condition: zTurbopackCondition.optional(),
})
const zTurbopackRuleConfigItem: zod.ZodType<TurbopackRuleConfigItem> = z.union([
z.literal(false),
z.record(
zTurbopackLoaderBuiltinCondition,
z.lazy(() => zTurbopackRuleConfigItem)
),
zTurbopackRuleConfigItemOptions,
])
const zTurbopackRuleConfigCollection: zod.ZodType<TurbopackRuleConfigCollection> =
z.union([
zTurbopackRuleConfigItem,
z.array(z.union([zTurbopackLoaderItem, zTurbopackRuleConfigItemOptions])),
z.array(z.union([zTurbopackLoaderItem, zTurbopackRuleConfigItem])),
])
const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({
......
......@@ -278,17 +278,12 @@ export type TurbopackRuleCondition =
content?: RegExp
}
export type TurbopackRuleConfigItemOptions = {
export type TurbopackRuleConfigItem = {
loaders: TurbopackLoaderItem[]
as?: string
condition?: TurbopackRuleCondition
}
export type TurbopackRuleConfigItem =
| TurbopackRuleConfigItemOptions
| { [condition in TurbopackLoaderBuiltinCondition]?: TurbopackRuleConfigItem }
| false
/**
* This can be an object representing a single configuration, or a list of
* loaders and/or rule configuration objects.
......@@ -300,7 +295,7 @@ export type TurbopackRuleConfigItem =
*/
export type TurbopackRuleConfigCollection =
| TurbopackRuleConfigItem
| (TurbopackLoaderItem | TurbopackRuleConfigItemOptions)[]
| (TurbopackLoaderItem | TurbopackRuleConfigItem)[]
export interface TurbopackOptions {
/**
......
......@@ -4,27 +4,27 @@
const nextConfig = {
turbopack: {
rules: {
'*.test-file.js': {
browser: {
foreign: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true, foreign: true },
},
],
},
default: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true },
},
],
},
'*.test-file.js': [
{
condition: { all: ['browser', 'foreign'] },
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true, foreign: true },
},
],
},
{
condition: { all: ['browser', { not: 'foreign' }] },
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true },
},
],
},
foreign: false,
default: {
{
condition: { not: { any: ['browser', 'foreign'] } },
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
......@@ -32,7 +32,7 @@ const nextConfig = {
},
],
},
},
],
},
},
}
......
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