Unverified Commit 43d91140 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add conditions for webpack loader rules (#63333)

### What?

* Allow to apply webpack loaders depending on "conditions".
* add a few next specific conditions depending on context type

### Why?

Some loaders need different behavior depending on context

### How?


Closes PACK-2748
parent f9cfccc2
...@@ -20,6 +20,14 @@ impl NextMode { ...@@ -20,6 +20,14 @@ impl NextMode {
} }
} }
/// Returns the exports condition for the current mode.
pub fn condition(&self) -> &'static str {
match self {
NextMode::Development => "development",
NextMode::Build => "production",
}
}
/// Returns true if the development React runtime should be used. /// Returns true if the development React runtime should be used.
pub fn is_react_development(&self) -> bool { pub fn is_react_development(&self) -> bool {
match self { match self {
......
...@@ -136,6 +136,17 @@ pub enum ClientContextType { ...@@ -136,6 +136,17 @@ pub enum ClientContextType {
Other, Other,
} }
impl ClientContextType {
pub fn conditions(&self) -> &'static [&'static str] {
match self {
ClientContextType::Pages { .. } => &["next-client", "next-pages"],
ClientContextType::App { .. } => &["next-client", "next-app"],
ClientContextType::Fallback => &["next-client", "next-fallback"],
ClientContextType::Other => &["next-client", "next-other"],
}
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_client_resolve_options_context( pub async fn get_client_resolve_options_context(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
...@@ -149,9 +160,11 @@ pub async fn get_client_resolve_options_context( ...@@ -149,9 +160,11 @@ pub async fn get_client_resolve_options_context(
let next_client_fallback_import_map = get_next_client_fallback_import_map(ty); let next_client_fallback_import_map = get_next_client_fallback_import_map(ty);
let next_client_resolved_map = let next_client_resolved_map =
get_next_client_resolved_map(project_path, project_path, *mode.await?); get_next_client_resolved_map(project_path, project_path, *mode.await?);
let mut custom_conditions = vec![mode.await?.condition().to_string()];
custom_conditions.extend(ty.conditions().iter().map(ToString::to_string));
let module_options_context = ResolveOptionsContext { let module_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?), enable_node_modules: Some(project_path.root().resolve().await?),
custom_conditions: vec![mode.await?.node_env().to_string()], custom_conditions,
import_map: Some(next_client_import_map), import_map: Some(next_client_import_map),
fallback_import_map: Some(next_client_fallback_import_map), fallback_import_map: Some(next_client_fallback_import_map),
resolved_map: Some(next_client_resolved_map), resolved_map: Some(next_client_resolved_map),
...@@ -215,11 +228,11 @@ pub async fn get_client_module_options_context( ...@@ -215,11 +228,11 @@ pub async fn get_client_module_options_context(
// foreign_code_context_condition. This allows to import codes from // foreign_code_context_condition. This allows to import codes from
// node_modules that requires webpack loaders, which next-dev implicitly // node_modules that requires webpack loaders, which next-dev implicitly
// does by default. // does by default.
let foreign_webpack_rules = maybe_add_sass_loader( let mut conditions = vec!["browser".to_string(), mode.await?.condition().to_string()];
next_config.sass_config(), conditions.extend(ty.conditions().iter().map(ToString::to_string));
*next_config.webpack_rules().await?, let foreign_webpack_rules = *next_config.webpack_rules(conditions).await?;
) let foreign_webpack_rules =
.await?; maybe_add_sass_loader(next_config.sass_config(), foreign_webpack_rules).await?;
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| { let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
WebpackLoadersOptions { WebpackLoadersOptions {
rules, rules,
......
use std::collections::HashSet;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
...@@ -387,21 +389,32 @@ pub enum RemotePatternProtocal { ...@@ -387,21 +389,32 @@ pub enum RemotePatternProtocal {
pub struct ExperimentalTurboConfig { pub struct ExperimentalTurboConfig {
/// This option has been replaced by `rules`. /// This option has been replaced by `rules`.
pub loaders: Option<JsonValue>, pub loaders: Option<JsonValue>,
pub rules: Option<IndexMap<String, RuleConfigItem>>, pub rules: Option<IndexMap<String, RuleConfigItemOrShortcut>>,
pub resolve_alias: Option<IndexMap<String, JsonValue>>, pub resolve_alias: Option<IndexMap<String, JsonValue>>,
pub resolve_extensions: Option<Vec<String>>, pub resolve_extensions: Option<Vec<String>>,
pub use_swc_css: Option<bool>, pub use_swc_css: Option<bool>,
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct RuleConfigItemOptions {
pub loaders: Vec<LoaderItem>,
#[serde(default, alias = "as")]
pub rename_as: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase", untagged)]
pub enum RuleConfigItem { pub enum RuleConfigItemOrShortcut {
Loaders(Vec<LoaderItem>), Loaders(Vec<LoaderItem>),
Options { Advanced(RuleConfigItem),
loaders: Vec<LoaderItem>, }
#[serde(default, alias = "as")]
rename_as: Option<String>, #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
}, #[serde(rename_all = "camelCase", untagged)]
pub enum RuleConfigItem {
Options(RuleConfigItemOptions),
Conditional(IndexMap<String, RuleConfigItem>),
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
...@@ -691,7 +704,10 @@ impl NextConfig { ...@@ -691,7 +704,10 @@ impl NextConfig {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn webpack_rules(self: Vc<Self>) -> Result<Vc<OptionWebpackRules>> { pub async fn webpack_rules(
self: Vc<Self>,
active_conditions: Vec<String>,
) -> Result<Vc<OptionWebpackRules>> {
let this = self.await?; let this = self.await?;
let Some(turbo_rules) = this let Some(turbo_rules) = this
.experimental .experimental
...@@ -704,8 +720,9 @@ impl NextConfig { ...@@ -704,8 +720,9 @@ impl NextConfig {
if turbo_rules.is_empty() { if turbo_rules.is_empty() {
return Ok(Vc::cell(None)); return Ok(Vc::cell(None));
} }
let active_conditions = active_conditions.into_iter().collect::<HashSet<_>>();
let mut rules = IndexMap::new(); let mut rules = IndexMap::new();
for (ext, rule) in turbo_rules { for (ext, rule) in turbo_rules.iter() {
fn transform_loaders(loaders: &[LoaderItem]) -> Vc<WebpackLoaderItems> { fn transform_loaders(loaders: &[LoaderItem]) -> Vc<WebpackLoaderItems> {
Vc::cell( Vc::cell(
loaders loaders
...@@ -720,18 +737,50 @@ impl NextConfig { ...@@ -720,18 +737,50 @@ impl NextConfig {
.collect(), .collect(),
) )
} }
let rule = match rule { fn find_rule<'a>(
RuleConfigItem::Loaders(loaders) => LoaderRuleItem { rule: &'a RuleConfigItem,
active_conditions: &HashSet<String>,
) -> Option<&'a RuleConfigItemOptions> {
match rule {
RuleConfigItem::Options(rule) => {
return Some(rule);
}
RuleConfigItem::Conditional(map) => {
for (condition, rule) in map.iter() {
if condition == "default" || active_conditions.contains(condition) {
if let Some(rule) = find_rule(rule, active_conditions) {
return Some(rule);
}
}
}
}
}
None
}
match rule {
RuleConfigItemOrShortcut::Loaders(loaders) => {
rules.insert(
ext.to_string(),
LoaderRuleItem {
loaders: transform_loaders(loaders), loaders: transform_loaders(loaders),
rename_as: None, rename_as: None,
}, },
RuleConfigItem::Options { loaders, rename_as } => LoaderRuleItem { );
}
RuleConfigItemOrShortcut::Advanced(rule) => {
if let Some(RuleConfigItemOptions { loaders, rename_as }) =
find_rule(rule, &active_conditions)
{
rules.insert(
ext.to_string(),
LoaderRuleItem {
loaders: transform_loaders(loaders), loaders: transform_loaders(loaders),
rename_as: rename_as.clone(), rename_as: rename_as.clone(),
}, },
}; );
}
rules.insert(ext.clone(), rule); }
}
} }
Ok(Vc::cell(Some(Vc::cell(rules)))) Ok(Vc::cell(Some(Vc::cell(rules))))
} }
......
...@@ -101,7 +101,7 @@ pub async fn get_edge_resolve_options_context( ...@@ -101,7 +101,7 @@ pub async fn get_edge_resolve_options_context(
// https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102 // https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102
let mut custom_conditions = vec![ let mut custom_conditions = vec![
mode.await?.node_env().to_string(), mode.await?.condition().to_string(),
"edge-light".to_string(), "edge-light".to_string(),
"worker".to_string(), "worker".to_string(),
]; ];
......
...@@ -97,6 +97,21 @@ pub enum ServerContextType { ...@@ -97,6 +97,21 @@ pub enum ServerContextType {
Instrumentation, Instrumentation,
} }
impl ServerContextType {
pub fn conditions(&self) -> &'static [&'static str] {
match self {
ServerContextType::Pages { .. } => &["next-server", "next-pages", "next-ssr"],
ServerContextType::PagesApi { .. } => &["next-server", "next-pages", "next-api"],
ServerContextType::PagesData { .. } => &["next-server", "next-pages", "next-data"],
ServerContextType::AppSSR { .. } => &["next-server", "next-app", "next-ssr"],
ServerContextType::AppRSC { .. } => &["next-server", "next-app", "next-rsc"],
ServerContextType::AppRoute { .. } => &["next-server", "next-app", "next-route"],
ServerContextType::Middleware => &["next-server", "next-middleware"],
ServerContextType::Instrumentation => &["next-server", "next-instrumentation"],
}
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_server_resolve_options_context( pub async fn get_server_resolve_options_context(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
...@@ -142,7 +157,8 @@ pub async fn get_server_resolve_options_context( ...@@ -142,7 +157,8 @@ pub async fn get_server_resolve_options_context(
); );
let ty = ty.into_value(); let ty = ty.into_value();
let mut custom_conditions = vec![mode.await?.node_env().to_string(), "node".to_string()]; let mut custom_conditions = vec![mode.await?.condition().to_string(), "node".to_string()];
custom_conditions.extend(ty.conditions().iter().map(ToString::to_string));
match ty { match ty {
ServerContextType::AppRSC { .. } => custom_conditions.push("react-server".to_string()), ServerContextType::AppRSC { .. } => custom_conditions.push("react-server".to_string()),
...@@ -337,11 +353,11 @@ pub async fn get_server_module_options_context( ...@@ -337,11 +353,11 @@ pub async fn get_server_module_options_context(
// foreign_code_context_condition. This allows to import codes from // foreign_code_context_condition. This allows to import codes from
// node_modules that requires webpack loaders, which next-dev implicitly // node_modules that requires webpack loaders, which next-dev implicitly
// does by default. // does by default.
let foreign_webpack_rules = maybe_add_sass_loader( let mut conditions = vec!["server".to_string(), mode.await?.condition().to_string()];
next_config.sass_config(), conditions.extend(ty.conditions().iter().map(ToString::to_string));
*next_config.webpack_rules().await?, let foreign_webpack_rules = *next_config.webpack_rules(conditions).await?;
) let foreign_webpack_rules =
.await?; maybe_add_sass_loader(next_config.sass_config(), foreign_webpack_rules).await?;
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| { let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
WebpackLoadersOptions { WebpackLoadersOptions {
rules, rules,
......
...@@ -8,7 +8,13 @@ import { getParserOptions } from './options' ...@@ -8,7 +8,13 @@ import { getParserOptions } from './options'
import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure' import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure'
import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile' import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc' import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
import type { NextConfigComplete, TurboRule } from '../../server/config-shared' import type {
TurboRuleConfigItem,
NextConfigComplete,
TurboLoaderItem,
TurboRuleConfigItemOrShortcut,
TurboRuleConfigItemOptions,
} from '../../server/config-shared'
import { isDeepStrictEqual } from 'util' import { isDeepStrictEqual } from 'util'
import type { DefineEnvPluginOptions } from '../webpack/plugins/define-env-plugin' import type { DefineEnvPluginOptions } from '../webpack/plugins/define-env-plugin'
import { getDefineEnv } from '../webpack/plugins/define-env-plugin' import { getDefineEnv } from '../webpack/plugins/define-env-plugin'
...@@ -1147,10 +1153,30 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -1147,10 +1153,30 @@ function bindingToApi(binding: any, _wasm: boolean) {
} }
function ensureLoadersHaveSerializableOptions( function ensureLoadersHaveSerializableOptions(
turbopackRules: Record<string, TurboRule> turbopackRules: Record<string, TurboRuleConfigItemOrShortcut>
) { ) {
for (const [glob, rule] of Object.entries(turbopackRules)) { for (const [glob, rule] of Object.entries(turbopackRules)) {
const loaderItems = Array.isArray(rule) ? rule : rule.loaders if (Array.isArray(rule)) {
checkLoaderItems(rule, glob)
} else {
checkConfigItem(rule, glob)
}
}
function checkConfigItem(rule: TurboRuleConfigItem, glob: string) {
if ('loaders' in rule) {
checkLoaderItems((rule as TurboRuleConfigItemOptions).loaders, glob)
} else {
for (const key in rule) {
const inner = rule[key]
if (typeof inner === 'object' && inner) {
checkConfigItem(inner, glob)
}
}
}
}
function checkLoaderItems(loaderItems: TurboLoaderItem[], glob: string) {
for (const loaderItem of loaderItems) { for (const loaderItem of loaderItems) {
if ( if (
typeof loaderItem !== 'string' && typeof loaderItem !== 'string' &&
......
...@@ -5,7 +5,11 @@ import { z } from 'next/dist/compiled/zod' ...@@ -5,7 +5,11 @@ import { z } from 'next/dist/compiled/zod'
import type zod from 'next/dist/compiled/zod' import type zod from 'next/dist/compiled/zod'
import type { SizeLimit } from '../../types' import type { SizeLimit } from '../../types'
import type { ExportPathMap, TurboLoaderItem, TurboRule } from './config-shared' import type {
ExportPathMap,
TurboLoaderItem,
TurboRuleConfigItemOrShortcut,
} from './config-shared'
import type { import type {
Header, Header,
Rewrite, Rewrite,
...@@ -99,7 +103,7 @@ const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([ ...@@ -99,7 +103,7 @@ const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([
}), }),
]) ])
const zTurboRule: zod.ZodType<TurboRule> = z.union([ const zTurboRule: zod.ZodType<TurboRuleConfigItemOrShortcut> = z.union([
z.array(zTurboLoaderItem), z.array(zTurboLoaderItem),
z.object({ z.object({
loaders: z.array(zTurboLoaderItem), loaders: z.array(zTurboLoaderItem),
......
...@@ -96,12 +96,18 @@ export type TurboLoaderItem = ...@@ -96,12 +96,18 @@ export type TurboLoaderItem =
options: Record<string, JSONValue> options: Record<string, JSONValue>
} }
export type TurboRule = export type TurboRuleConfigItemOrShortcut =
| TurboLoaderItem[] | TurboLoaderItem[]
| { | TurboRuleConfigItem
export type TurboRuleConfigItemOptions = {
loaders: TurboLoaderItem[] loaders: TurboLoaderItem[]
as: string as: string
} }
export type TurboRuleConfigItem =
| TurboRuleConfigItemOptions
| { [condition: string]: TurboRuleConfigItem }
export interface ExperimentalTurboOptions { export interface ExperimentalTurboOptions {
/** /**
...@@ -133,7 +139,7 @@ export interface ExperimentalTurboOptions { ...@@ -133,7 +139,7 @@ export interface ExperimentalTurboOptions {
* *
* @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders) * @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders)
*/ */
rules?: Record<string, TurboRule> rules?: Record<string, TurboRuleConfigItemOrShortcut>
/** /**
* Use swc_css instead of lightningcss for turbopakc * Use swc_css instead of lightningcss for turbopakc
......
'use client'
import test from './file.test-file.js'
export default function Client() {
return <p>client: {JSON.stringify(test)}</p>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Client from './client'
import rsc from './file.test-file.js'
export default function Page() {
return (
<div>
<p>server: {JSON.stringify(rsc)}</p>
<Client />
</div>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.test-file.js': {
browser: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true },
},
],
},
'next-app': {
'next-ssr': {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { nextSsr: true },
},
],
},
},
default: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { default: true },
},
],
},
},
},
},
},
}
module.exports = nextConfig
module.exports = function () {
const options = this.getOptions()
return `export default ${JSON.stringify(options)};`
}
import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-conditions', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
if (!isTurbopack) {
it('should only run the test in turbopack', () => {})
return
}
it('should render correctly on server site', async () => {
const res = await next.fetch('/')
const html = (await res.text()).replaceAll(/<!-- -->/g, '')
expect(html).toContain(`server: {&quot;default&quot;:true}`)
expect(html).toContain(`client: {&quot;nextSsr&quot;:true}`)
})
it('should render correctly on client side', async () => {
const browser = await next.browser('/')
const text = await browser.elementByCss('body').text()
expect(text).toContain(`server: ${JSON.stringify({ default: true })}`)
expect(text).toContain(`client: ${JSON.stringify({ browser: true })}`)
})
})
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