Unverified Commit bf6784f7 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Add postcss.config.ts support (#89049)

### What?

Adds support for TypeScript PostCSS configuration files in Turbopack.

### Why?

Users can write their PostCSS configuration in TypeScript for better
type safety and IDE support. This was already supported in webpack but
was missing in Turbopack.

### How?

Added the following TypeScript config file names to Turbopack's PostCSS
config detection:
- `postcss.config.ts`, `postcss.config.mts`, `postcss.config.cts`
- `.postcssrc.ts`, `.postcssrc.mts`, `.postcssrc.cts`
- `.config/postcssrc.ts`, `.config/postcssrc.mts`,
`.config/postcssrc.cts`

Also updated the Turbopack documentation to reflect the supported config
file extensions (`.js`, `.mjs`, `.cjs`, `.ts`, `.mts`, `.cts`).

### Testing

Added e2e tests that verify the TypeScript PostCSS config is actually
loaded and applied by using a custom plugin that transforms colors.
Tests cover:
- `postcss.config.ts`
- `postcss.config.mts`
- `postcss.config.cts`
- `.postcssrc.ts`
parent f873879b
...@@ -78,7 +78,7 @@ Turbopack in Next.js has **zero-configuration** for the common use cases. Below ...@@ -78,7 +78,7 @@ Turbopack in Next.js has **zero-configuration** for the common use cases. Below
| **CSS Modules** | **Supported** | `.module.css` files work natively (Lightning CSS). | | **CSS Modules** | **Supported** | `.module.css` files work natively (Lightning CSS). |
| **CSS Nesting** | **Supported** | Lightning CSS supports [modern CSS nesting](https://lightningcss.dev/). | | **CSS Nesting** | **Supported** | Lightning CSS supports [modern CSS nesting](https://lightningcss.dev/). |
| **@import syntax** | **Supported** | Combine multiple CSS files. | | **@import syntax** | **Supported** | Combine multiple CSS files. |
| **PostCSS** | **Supported** | Automatically processes `postcss.config.js` in a Node.js worker pool. Useful for Tailwind, Autoprefixer, etc. | | **PostCSS** | **Supported** | Automatically processes PostCSS config files (`postcss.config.js`, `.mjs`, `.cjs`, `.ts`, `.mts`, `.cts`) in a Node.js worker pool. Useful for Tailwind, Autoprefixer, etc. |
| **Sass / SCSS** | **Supported** (Next.js) | For Next.js, Sass is supported out of the box. Custom Sass functions (`sassOptions.functions`) are not supported because Turbopack's Rust-based architecture cannot directly execute JavaScript functions, unlike webpack's Node.js environment. Use webpack if you need this feature. In the future, Turbopack standalone usage will likely require a loader config. | | **Sass / SCSS** | **Supported** (Next.js) | For Next.js, Sass is supported out of the box. Custom Sass functions (`sassOptions.functions`) are not supported because Turbopack's Rust-based architecture cannot directly execute JavaScript functions, unlike webpack's Node.js environment. Use webpack if you need this feature. In the future, Turbopack standalone usage will likely require a loader config. |
| **Less** | Planned via plugins | Not yet supported by default. Will likely require a loader config once custom loaders are stable. | | **Less** | Planned via plugins | Not yet supported by default. Will likely require a loader config once custom loaders are stable. |
| **Lightning CSS** | **In Use** | Handles CSS transformations. Some low-usage CSS Modules features (like `:local/:global` as standalone pseudo-classes) are not yet supported. [See below for more details.](#unsupported-and-unplanned-features) | | **Lightning CSS** | **In Use** | Handles CSS transformations. Some low-usage CSS Modules features (like `:local/:global` as standalone pseudo-classes) are not yet supported. [See below for more details.](#unsupported-and-unplanned-features) |
......
import { FileRef, nextTestSetup } from 'e2e-utils'
import { join } from 'path'
// Not supported when using webpack.
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'postcss-config-ts',
() => {
describe('postcss.config.ts', () => {
const { next } = nextTestSetup({
files: new FileRef(join(__dirname, 'postcss-config')),
})
it('works with postcss.config.ts files', async () => {
const browser = await next.browser('/')
try {
const text = await browser.elementByCss('#test').text()
expect(text).toBe('Hello World')
// The CSS has `color: red` but the PostCSS plugin transforms it to green.
// If this is green, it proves the TypeScript PostCSS config was loaded and applied.
const color = await browser
.elementByCss('#test')
.getComputedCss('color')
expect(color).toBe('rgb(0, 128, 0)')
} finally {
await browser.close()
}
})
})
describe('.postcssrc.ts', () => {
const { next } = nextTestSetup({
files: new FileRef(join(__dirname, 'postcssrc')),
})
it('works with .postcssrc.ts files', async () => {
const browser = await next.browser('/')
try {
const text = await browser.elementByCss('#test').text()
expect(text).toBe('Hello World')
// The CSS has `color: red` but the PostCSS plugin transforms it to green.
// If this is green, it proves the TypeScript PostCSS config was loaded and applied.
const color = await browser
.elementByCss('#test')
.getComputedCss('color')
expect(color).toBe('rgb(0, 128, 0)')
} finally {
await browser.close()
}
})
})
describe('postcss.config.mts', () => {
const { next } = nextTestSetup({
files: new FileRef(join(__dirname, 'postcss-config-mts')),
})
it('works with postcss.config.mts files', async () => {
const browser = await next.browser('/')
try {
const text = await browser.elementByCss('#test').text()
expect(text).toBe('Hello World')
// The CSS has `color: red` but the PostCSS plugin transforms it to green.
// If this is green, it proves the TypeScript PostCSS config was loaded and applied.
const color = await browser
.elementByCss('#test')
.getComputedCss('color')
expect(color).toBe('rgb(0, 128, 0)')
} finally {
await browser.close()
}
})
})
describe('postcss.config.cts', () => {
const { next } = nextTestSetup({
files: new FileRef(join(__dirname, 'postcss-config-cts')),
})
it('works with postcss.config.cts files', async () => {
const browser = await next.browser('/')
try {
const text = await browser.elementByCss('#test').text()
expect(text).toBe('Hello World')
// The CSS has `color: red` but the PostCSS plugin transforms it to green.
// If this is green, it proves the TypeScript PostCSS config was loaded and applied.
const color = await browser
.elementByCss('#test')
.getComputedCss('color')
expect(color).toBe('rgb(0, 128, 0)')
} finally {
await browser.close()
}
})
})
}
)
.test-class {
/* This red color should be transformed to green by the PostCSS plugin */
color: red;
display: flex;
}
import './globals.css'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return (
<div>
<p id="test" className="test-class">
Hello World
</p>
</div>
)
}
// Custom PostCSS plugin that transforms color: red to color: green
// This allows us to verify that the PostCSS config is actually being applied
const plugin = () => {
return {
postcssPlugin: 'color-transform',
Declaration: {
color(decl) {
if (decl.value === 'red') {
decl.value = 'green'
}
},
},
}
}
plugin.postcss = true
module.exports = plugin
// TypeScript PostCSS config file (.cts)
const plugin = require('./plugin.cjs')
type PluginConfig = Record<string, unknown> | boolean | (() => unknown)
interface PostCSSConfig {
plugins: PluginConfig[]
}
const config: PostCSSConfig = {
plugins: [plugin],
}
module.exports = config
.test-class {
/* This red color should be transformed to green by the PostCSS plugin */
color: red;
display: flex;
}
import './globals.css'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return (
<div>
<p id="test" className="test-class">
Hello World
</p>
</div>
)
}
// Custom PostCSS plugin that transforms color: red to color: green
// This allows us to verify that the PostCSS config is actually being applied
const plugin = () => {
return {
postcssPlugin: 'color-transform',
Declaration: {
color(decl) {
if (decl.value === 'red') {
decl.value = 'green'
}
},
},
}
}
plugin.postcss = true
export default plugin
// TypeScript PostCSS config file (.mts)
import plugin from './plugin.mjs'
type PluginConfig = Record<string, unknown> | boolean | (() => unknown)
interface PostCSSConfig {
plugins: PluginConfig[]
}
const config: PostCSSConfig = {
plugins: [plugin],
}
export default config
.test-class {
/* This red color should be transformed to green by the PostCSS plugin */
color: red;
display: flex;
}
import './globals.css'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return (
<div>
<p id="test" className="test-class">
Hello World
</p>
</div>
)
}
// Custom PostCSS plugin that transforms color: red to color: green
// This allows us to verify that the PostCSS config is actually being applied
const plugin = () => {
return {
postcssPlugin: 'color-transform',
Declaration: {
color(decl: { value: string }) {
if (decl.value === 'red') {
decl.value = 'green'
}
},
},
}
}
plugin.postcss = true
export default plugin
// TypeScript PostCSS config file
import plugin from './plugin'
type PluginConfig = Record<string, unknown> | boolean | (() => unknown)
interface PostCSSConfig {
plugins: PluginConfig[]
}
const config: PostCSSConfig = {
plugins: [plugin],
}
export default config
// TypeScript PostCSS config file
import plugin from './plugin'
type PluginConfig = Record<string, unknown> | boolean | (() => unknown)
interface PostCSSConfig {
plugins: PluginConfig[]
}
const config: PostCSSConfig = {
plugins: [plugin],
}
export default config
.test-class {
/* This red color should be transformed to green by the PostCSS plugin */
color: red;
display: flex;
}
import './globals.css'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return (
<div>
<p id="test" className="test-class">
Hello World
</p>
</div>
)
}
// Custom PostCSS plugin that transforms color: red to color: green
// This allows us to verify that the PostCSS config is actually being applied
const plugin = () => {
return {
postcssPlugin: 'color-transform',
Declaration: {
color(decl: { value: string }) {
if (decl.value === 'red') {
decl.value = 'green'
}
},
},
}
}
plugin.postcss = true
export default plugin
...@@ -84,6 +84,9 @@ fn postcss_configs() -> Vc<Vec<RcStr>> { ...@@ -84,6 +84,9 @@ fn postcss_configs() -> Vc<Vec<RcStr>> {
".postcssrc.js", ".postcssrc.js",
".postcssrc.mjs", ".postcssrc.mjs",
".postcssrc.cjs", ".postcssrc.cjs",
".postcssrc.ts",
".postcssrc.mts",
".postcssrc.cts",
".config/postcssrc", ".config/postcssrc",
".config/postcssrc.json", ".config/postcssrc.json",
".config/postcssrc.yaml", ".config/postcssrc.yaml",
...@@ -91,9 +94,15 @@ fn postcss_configs() -> Vc<Vec<RcStr>> { ...@@ -91,9 +94,15 @@ fn postcss_configs() -> Vc<Vec<RcStr>> {
".config/postcssrc.js", ".config/postcssrc.js",
".config/postcssrc.mjs", ".config/postcssrc.mjs",
".config/postcssrc.cjs", ".config/postcssrc.cjs",
".config/postcssrc.ts",
".config/postcssrc.mts",
".config/postcssrc.cts",
"postcss.config.js", "postcss.config.js",
"postcss.config.mjs", "postcss.config.mjs",
"postcss.config.cjs", "postcss.config.cjs",
"postcss.config.ts",
"postcss.config.mts",
"postcss.config.cts",
"postcss.config.json", "postcss.config.json",
] ]
.into_iter() .into_iter()
......
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