Unverified Commit 6016558b authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: use a source content regexp for the react compiler (#82631)

### What?

Depending on compilation mode of the react compiler, check the source code if matches a certain regex

This also adds `content` as condition for webpack loaders

Closes PACK-5252
parent 88b3223a
...@@ -577,24 +577,32 @@ impl TryInto<ConditionPath> for ConfigConditionPath { ...@@ -577,24 +577,32 @@ impl TryInto<ConditionPath> for ConfigConditionPath {
fn try_into(self) -> Result<ConditionPath> { fn try_into(self) -> Result<ConditionPath> {
Ok(match self { Ok(match self {
ConfigConditionPath::Glob(path) => ConditionPath::Glob(path), ConfigConditionPath::Glob(path) => ConditionPath::Glob(path),
ConfigConditionPath::Regex(path) => { ConfigConditionPath::Regex(path) => ConditionPath::Regex(path.try_into()?),
ConditionPath::Regex(EsRegex::new(&path.source, &path.flags)?.resolved_cell())
}
}) })
} }
type Error = anyhow::Error; type Error = anyhow::Error;
} }
impl TryInto<ResolvedVc<EsRegex>> for RegexComponents {
fn try_into(self) -> Result<ResolvedVc<EsRegex>> {
Ok(EsRegex::new(&self.source, &self.flags)?.resolved_cell())
}
type Error = anyhow::Error;
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ConfigConditionItem { pub struct ConfigConditionItem {
pub path: ConfigConditionPath, pub path: Option<ConfigConditionPath>,
pub content: Option<RegexComponents>,
} }
impl TryInto<ConditionItem> for ConfigConditionItem { impl TryInto<ConditionItem> for ConfigConditionItem {
fn try_into(self) -> Result<ConditionItem> { fn try_into(self) -> Result<ConditionItem> {
Ok(ConditionItem { Ok(ConditionItem {
path: self.path.try_into()?, path: self.path.map(|p| p.try_into()).transpose()?,
content: self.content.map(|r| r.try_into()).transpose()?,
}) })
} }
......
...@@ -10,6 +10,7 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile' ...@@ -10,6 +10,7 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc' import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
import type { import type {
NextConfigComplete, NextConfigComplete,
ReactCompilerOptions,
TurbopackLoaderItem, TurbopackLoaderItem,
TurbopackRuleConfigItem, TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions, TurbopackRuleConfigItemOptions,
...@@ -805,13 +806,15 @@ function bindingToApi( ...@@ -805,13 +806,15 @@ function bindingToApi(
originalNextConfig: NextConfigComplete, originalNextConfig: NextConfigComplete,
projectPath: string projectPath: string
): Record<string, any> { ): Record<string, any> {
let nextConfig = { ...(originalNextConfig as any) } let nextConfig = { ...(originalNextConfig as NextConfigComplete) }
const reactCompilerOptions = nextConfig.experimental?.reactCompiler const reactCompilerOptions = nextConfig.experimental?.reactCompiler
// It is not easy to set the rules inside of rust as resolving, and passing the context identical to the webpack // It is not easy to set the rules inside of rust as resolving, and passing the context identical to the webpack
// config is bit hard, also we can reuse same codes between webpack config in here. // config is bit hard, also we can reuse same codes between webpack config in here.
if (reactCompilerOptions) { if (reactCompilerOptions) {
const options: ReactCompilerOptions =
typeof reactCompilerOptions === 'object' ? reactCompilerOptions : {}
const ruleKeys = ['*.ts', '*.js', '*.jsx', '*.tsx'] const ruleKeys = ['*.ts', '*.js', '*.jsx', '*.tsx']
if ( if (
Object.keys(nextConfig?.turbopack?.rules ?? {}).some((key) => Object.keys(nextConfig?.turbopack?.rules ?? {}).some((key) =>
...@@ -826,15 +829,27 @@ function bindingToApi( ...@@ -826,15 +829,27 @@ function bindingToApi(
) )
} else { } else {
nextConfig.turbopack ??= {} nextConfig.turbopack ??= {}
nextConfig.turbopack.conditions ??= {}
nextConfig.turbopack.rules ??= {} nextConfig.turbopack.rules ??= {}
for (const key of ruleKeys) { for (const key of ruleKeys) {
nextConfig.turbopack.rules[key] = { 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,
}
nextConfig.turbopack.rules[`#reactCompiler/${key}`] = {
browser: { browser: {
foreign: false, foreign: false,
loaders: [ loaders: [
getReactCompilerLoader( getReactCompilerLoader(
originalNextConfig.experimental.reactCompiler, reactCompilerOptions,
projectPath, projectPath,
nextConfig.dev, nextConfig.dev,
/* isServer */ false, /* isServer */ false,
...@@ -905,9 +920,20 @@ function bindingToApi( ...@@ -905,9 +920,20 @@ function bindingToApi(
if (conditions) { if (conditions) {
type SerializedConditions = { type SerializedConditions = {
[key: string]: { [key: string]: {
path: path?:
| { type: 'regex'; value: { source: string; flags: string } } | { type: 'regex'; value: { source: string; flags: string } }
| { type: 'glob'; value: string } | { type: 'glob'; value: string }
content?: { source: string; flags: string }
}
}
function regexComponents(regex: RegExp): {
source: string
flags: string
} {
return {
source: regex.source,
flags: regex.flags,
} }
} }
...@@ -915,13 +941,15 @@ function bindingToApi( ...@@ -915,13 +941,15 @@ function bindingToApi(
for (const [key, value] of Object.entries(conditions)) { for (const [key, value] of Object.entries(conditions)) {
serializedConditions[key] = { serializedConditions[key] = {
...value, ...value,
path: path: !value.path
value.path instanceof RegExp ? undefined
: value.path instanceof RegExp
? { ? {
type: 'regex', type: 'regex',
value: { source: value.path.source, flags: value.path.flags }, value: regexComponents(value.path),
} }
: { type: 'glob', value: value.path }, : { type: 'glob', value: value.path },
content: !value.content ? undefined : regexComponents(value.content),
} }
} }
nextConfigSerializable.turbopack.conditions = serializedConditions nextConfigSerializable.turbopack.conditions = serializedConditions
......
...@@ -129,7 +129,8 @@ const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurbopackRuleConfigItemOrShort ...@@ -129,7 +129,8 @@ const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurbopackRuleConfigItemOrShort
z.union([z.array(zTurboLoaderItem), zTurboRuleConfigItem]) z.union([z.array(zTurboLoaderItem), zTurboRuleConfigItem])
const zTurboCondition: zod.ZodType<TurbopackRuleCondition> = z.object({ const zTurboCondition: zod.ZodType<TurbopackRuleCondition> = z.object({
path: z.union([z.string(), z.instanceof(RegExp)]), path: z.union([z.string(), z.instanceof(RegExp)]).optional(),
content: z.instanceof(RegExp).optional(),
}) })
const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({ const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({
......
...@@ -262,7 +262,8 @@ export type TurbopackLoaderItem = ...@@ -262,7 +262,8 @@ export type TurbopackLoaderItem =
} }
export type TurbopackRuleCondition = { export type TurbopackRuleCondition = {
path: string | RegExp path?: string | RegExp
content?: RegExp
} }
export type TurbopackRuleConfigItemOrShortcut = export type TurbopackRuleConfigItemOrShortcut =
......
...@@ -489,43 +489,58 @@ impl ModuleOptions { ...@@ -489,43 +489,58 @@ impl ModuleOptions {
) )
}; };
for (key, rule) in webpack_loaders_options.rules.await?.iter() { for (key, rule) in webpack_loaders_options.rules.await?.iter() {
rules.push(ModuleRule::new( let mut rule_conditions = Vec::new();
RuleCondition::All(vec![ if key.starts_with("#") {
if key.starts_with("#") { // This is a custom marker requiring a corresponding condition entry
// This is a custom marker requiring a corresponding condition entry let conditions = (*webpack_loaders_options.conditions.await?)
let conditions = (*webpack_loaders_options.conditions.await?) .context(
.context( "Expected a condition entry for the webpack loader rule matching \
"Expected a condition entry for the webpack loader rule \ {key}. Create a `conditions` mapping in your next.config.js",
matching {key}. Create a `conditions` mapping in your \ )?
next.config.js", .await?;
)?
.await?; let condition = conditions.get(key).context(
"Expected a condition entry for the webpack loader rule matching {key}.",
let condition = conditions.get(key).context( )?;
"Expected a condition entry for the webpack loader rule matching \
{key}.", let ConditionItem { path, content } = &condition;
)?;
match &path {
match &condition.path { Some(ConditionPath::Glob(glob)) => {
ConditionPath::Glob(glob) => RuleCondition::ResourcePathGlob { if glob.contains('/') {
rule_conditions.push(RuleCondition::ResourcePathGlob {
base: execution_context.project_path().owned().await?, base: execution_context.project_path().owned().await?,
glob: Glob::new(glob.clone()).await?, glob: Glob::new(glob.clone()).await?,
}, });
ConditionPath::Regex(regex) => { } else {
RuleCondition::ResourcePathEsRegex(regex.await?) rule_conditions.push(RuleCondition::ResourceBasePathGlob(
} Glob::new(glob.clone()).await?,
} ));
} else if key.contains('/') {
RuleCondition::ResourcePathGlob {
base: execution_context.project_path().owned().await?,
glob: Glob::new(key.clone()).await?,
} }
} else { }
RuleCondition::ResourceBasePathGlob(Glob::new(key.clone()).await?) Some(ConditionPath::Regex(regex)) => {
}, rule_conditions.push(RuleCondition::ResourcePathEsRegex(regex.await?));
RuleCondition::not(RuleCondition::ResourceIsVirtualSource), }
module_css_external_transform_conditions.clone(), None => {}
]), }
if let Some(content) = content {
rule_conditions.push(RuleCondition::ResourceContentEsRegex(content.await?));
}
} else if key.contains('/') {
rule_conditions.push(RuleCondition::ResourcePathGlob {
base: execution_context.project_path().owned().await?,
glob: Glob::new(key.clone()).await?,
});
} else {
rule_conditions.push(RuleCondition::ResourceBasePathGlob(
Glob::new(key.clone()).await?,
));
};
rule_conditions.push(RuleCondition::not(RuleCondition::ResourceIsVirtualSource));
rule_conditions.push(module_css_external_transform_conditions.clone());
rules.push(ModuleRule::new(
RuleCondition::All(rule_conditions),
vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![ vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![
ResolvedVc::upcast( ResolvedVc::upcast(
WebpackLoaders::new( WebpackLoaders::new(
......
...@@ -50,7 +50,8 @@ pub enum ConditionPath { ...@@ -50,7 +50,8 @@ pub enum ConditionPath {
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ConditionItem { pub struct ConditionItem {
pub path: ConditionPath, pub path: Option<ConditionPath>,
pub content: Option<ResolvedVc<EsRegex>>,
} }
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
......
...@@ -3,9 +3,9 @@ use serde::{Deserialize, Serialize}; ...@@ -3,9 +3,9 @@ use serde::{Deserialize, Serialize};
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_esregex::EsRegex; use turbo_esregex::EsRegex;
use turbo_tasks::{NonLocalValue, ReadRef, ResolvedVc, primitives::Regex, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ReadRef, ResolvedVc, primitives::Regex, trace::TraceRawVcs};
use turbo_tasks_fs::{FileSystemPath, glob::Glob}; use turbo_tasks_fs::{FileContent, FileSystemPath, glob::Glob};
use turbopack_core::{ use turbopack_core::{
reference_type::ReferenceType, source::Source, virtual_source::VirtualSource, asset::Asset, reference_type::ReferenceType, source::Source, virtual_source::VirtualSource,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, NonLocalValue)] #[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, NonLocalValue)]
...@@ -24,6 +24,7 @@ pub enum RuleCondition { ...@@ -24,6 +24,7 @@ pub enum RuleCondition {
ContentTypeEmpty, ContentTypeEmpty,
ResourcePathRegex(#[turbo_tasks(trace_ignore)] Regex), ResourcePathRegex(#[turbo_tasks(trace_ignore)] Regex),
ResourcePathEsRegex(#[turbo_tasks(trace_ignore)] ReadRef<EsRegex>), ResourcePathEsRegex(#[turbo_tasks(trace_ignore)] ReadRef<EsRegex>),
ResourceContentEsRegex(#[turbo_tasks(trace_ignore)] ReadRef<EsRegex>),
/// For paths that are within the same filesystem as the `base`, it need to /// For paths that are within the same filesystem as the `base`, it need to
/// match the relative path from base to resource. This includes `./` or /// match the relative path from base to resource. This includes `./` or
/// `../` prefix. For paths in a different filesystem, it need to match /// `../` prefix. For paths in a different filesystem, it need to match
...@@ -166,6 +167,15 @@ impl RuleCondition { ...@@ -166,6 +167,15 @@ impl RuleCondition {
RuleCondition::ResourcePathEsRegex(regex) => { RuleCondition::ResourcePathEsRegex(regex) => {
return Ok(regex.is_match(&path.path)); return Ok(regex.is_match(&path.path));
} }
RuleCondition::ResourceContentEsRegex(regex) => {
let content = source.content().file_content().await?;
match &*content {
FileContent::Content(file_content) => {
return Ok(regex.is_match(&file_content.content().to_str()?));
}
FileContent::NotFound => return Ok(false),
}
}
} }
} }
} }
......
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