Unverified Commit 77dcd4c6 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack build: Fix CSS modules tests (#68834)

## What?

Added some additional docs on unsupported CSS Modules features, the ones
LightningCSS documents, as well as some cases that the tests checked
like `//` that are invalid CSS comments.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent fa620e2f
...@@ -30,6 +30,13 @@ Turbopack currently only supports `next dev` and does not support `next build`. ...@@ -30,6 +30,13 @@ Turbopack currently only supports `next dev` and does not support `next build`.
These features are currently not supported: These features are currently not supported:
- Turbopack leverages [Lightning CSS](https://lightningcss.dev/) which doesn't support some low usage CSS Modules features
- `:local` and `:global` as standalone pseudo classes. Only the function variant is supported, for example: `:global(a)`.
- The @value rule which has been superseded by CSS variables.
- `:import` and `:export` ICSS rules.
- [Invalid CSS comment syntax](https://stackoverflow.com/questions/51843506/are-double-slash-comments-valid-in-css) such as `//`
- CSS comments should be written as `/* comment */` per the specification.
- Preprocessors such as Sass do support this alternative syntax for comments.
- [`webpack()`](/docs/app/api-reference/next-config-js/webpack) configuration in `next.config.js` - [`webpack()`](/docs/app/api-reference/next-config-js/webpack) configuration in `next.config.js`
- Turbopack replaces Webpack, this means that webpack configuration is not supported. - Turbopack replaces Webpack, this means that webpack configuration is not supported.
- To configure Turbopack, [see the documentation](/docs/app/api-reference/next-config-js/turbo). - To configure Turbopack, [see the documentation](/docs/app/api-reference/next-config-js/turbo).
......
/* eslint-env jest */ /* eslint-env jest */
import { readdir, readFile, remove } from 'fs-extra' import { remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils' import {
findPort,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
fetchViaHTTP,
} from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
const fixturesDir = join(__dirname, '../fixtures') const fixturesDir = join(__dirname, '../fixtures')
describe('Custom Properties: Fail for :root {} in CSS Modules', () => { // Turbopack uses LightningCSS which supports scoping `:root` to the CSS module.
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'Custom Properties: Fail for :root {} in CSS Modules',
() => { () => {
const appDir = join(fixturesDir, 'cp-global-modules') ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
beforeAll(async () => { () => {
await remove(join(appDir, '.next')) const appDir = join(fixturesDir, 'cp-global-modules')
})
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => { it('should fail to build', async () => {
const { code, stderr } = await nextBuild(appDir, [], { const { code, stderr } = await nextBuild(appDir, [], {
stderr: true, stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('pages/styles.module.css')
expect(stderr).toContain('Selector ":root" is not pure')
}) })
expect(code).not.toBe(0) }
expect(stderr).toContain('Failed to compile') )
expect(stderr).toContain('pages/styles.module.css') }
expect(stderr).toContain('Selector ":root" is not pure') )
})
}
)
})
describe('Custom Properties: Fail for global element in CSS Modules', () => { describe('Custom Properties: Fail for global element in CSS Modules', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
...@@ -58,32 +70,48 @@ describe('CSS Modules: Import Global CSS', () => { ...@@ -58,32 +70,48 @@ describe('CSS Modules: Import Global CSS', () => {
() => { () => {
const appDir = join(fixturesDir, 'module-import-global') const appDir = join(fixturesDir, 'module-import-global')
let stdout let appPort
let code let app
beforeAll(async () => { beforeAll(async () => {
await remove(join(appDir, '.next')) await remove(join(appDir, '.next'))
;({ code, stdout } = await nextBuild(appDir, [], { const { code } = await nextBuild(appDir)
stdout: true, if (code !== 0) {
})) throw new Error('failed to build')
}
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterAll(async () => {
it('should have compiled successfully', () => { await killApp(app)
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
}) })
it(`should've emitted a single CSS file`, async () => { it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css') const content = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(content)
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f)) const cssSheet = $('link[rel="stylesheet"]')
expect(cssSheet.length).toBe(1)
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8') const stylesheet = cssSheet.attr('href')
expect( const cssContent = (
cssContent.replace(/\/\*.*?\*\//g, '').trim() await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
).toMatchInlineSnapshot(`"a .styles_foo__G5630{all:initial}"`) )
.replace(/\/\*.*?\*\//g, '')
.trim()
if (process.env.TURBOPACK) {
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim())
.toMatchInlineSnapshot(`
"a .foo {
all: initial;
}"
`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`"a .styles_foo__G5630{all:initial}"`)
}
}) })
} }
) )
...@@ -112,39 +140,50 @@ describe('CSS Modules: Importing Invalid Global CSS', () => { ...@@ -112,39 +140,50 @@ describe('CSS Modules: Importing Invalid Global CSS', () => {
) )
}) })
describe('CSS Modules: Import Exports', () => { // Turbopack uses LightningCSS which doesn't support `@value`.
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'CSS Modules: Import Exports',
() => { () => {
const appDir = join(fixturesDir, 'module-import-exports') ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
let stdout () => {
let code const appDir = join(fixturesDir, 'module-import-exports')
beforeAll(async () => {
await remove(join(appDir, '.next')) let appPort
;({ code, stdout } = await nextBuild(appDir, [], { let app
stdout: true, beforeAll(async () => {
})) await remove(join(appDir, '.next'))
}) const { code } = await nextBuild(appDir)
if (code !== 0) {
throw new Error('failed to build')
}
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
it('should have compiled successfully', () => { it(`should've emitted a single CSS file`, async () => {
expect(code).toBe(0) const content = await renderViaHTTP(appPort, '/')
expect(stdout).toMatch(/Compiled successfully/) const $ = cheerio.load(content)
})
it(`should've emitted a single CSS file`, async () => { const cssSheet = $('link[rel="stylesheet"]')
const cssFolder = join(appDir, '.next/static/css') expect(cssSheet.length).toBe(1)
const files = await readdir(cssFolder) const stylesheet = cssSheet.attr('href')
const cssFiles = files.filter((f) => /\.css$/.test(f))
expect(cssFiles.length).toBe(1) const cssContent = (
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8') await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
)
.replace(/\/\*.*?\*\//g, '')
.trim()
expect( expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim() cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`".styles_blk__480DC{color:#000}"`) ).toMatchInlineSnapshot(`".styles_blk__480DC{color:#000}"`)
}) })
} }
) )
}) }
)
/* eslint-env jest */ /* eslint-env jest */
import { readdir, readFile, remove } from 'fs-extra' import { remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils' import {
findPort,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
fetchViaHTTP,
} from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
const fixturesDir = join(__dirname, '../fixtures') const fixturesDir = join(__dirname, '../fixtures')
...@@ -12,32 +21,52 @@ describe('Custom Properties: Pass-Through IE11', () => { ...@@ -12,32 +21,52 @@ describe('Custom Properties: Pass-Through IE11', () => {
() => { () => {
const appDir = join(fixturesDir, 'cp-ie-11') const appDir = join(fixturesDir, 'cp-ie-11')
let stdout let appPort
let code let app
beforeAll(async () => { beforeAll(async () => {
await remove(join(appDir, '.next')) await remove(join(appDir, '.next'))
;({ code, stdout } = await nextBuild(appDir, [], { const { code } = await nextBuild(appDir)
stdout: true, if (code !== 0) {
})) throw new Error('failed to build')
}
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterAll(async () => {
it('should have compiled successfully', () => { await killApp(app)
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
}) })
it(`should've emitted a single CSS file`, async () => { it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css') const content = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(content)
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f)) const cssSheet = $('link[rel="stylesheet"]')
expect(cssSheet.length).toBe(1)
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8') const stylesheet = cssSheet.attr('href')
expect( const cssContent = (
cssContent.replace(/\/\*.*?\*\//g, '').trim() await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
).toMatchInlineSnapshot(`":root{--color:red}h1{color:var(--color)}"`) )
.replace(/\/\*.*?\*\//g, '')
.trim()
if (process.env.TURBOPACK) {
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim())
.toMatchInlineSnapshot(`
":root {
--color: red;
}
h1 {
color: var(--color);
}"
`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`":root{--color:red}h1{color:var(--color)}"`)
}
}) })
} }
) )
...@@ -49,70 +78,107 @@ describe('Custom Properties: Pass-Through Modern', () => { ...@@ -49,70 +78,107 @@ describe('Custom Properties: Pass-Through Modern', () => {
() => { () => {
const appDir = join(fixturesDir, 'cp-modern') const appDir = join(fixturesDir, 'cp-modern')
let stdout let appPort
let code let app
beforeAll(async () => { beforeAll(async () => {
await remove(join(appDir, '.next')) await remove(join(appDir, '.next'))
;({ code, stdout } = await nextBuild(appDir, [], { const { code } = await nextBuild(appDir)
stdout: true, if (code !== 0) {
})) throw new Error('failed to build')
}
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterAll(async () => {
it('should have compiled successfully', () => { await killApp(app)
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
}) })
it(`should've emitted a single CSS file`, async () => { it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css') const content = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(content)
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f)) const cssSheet = $('link[rel="stylesheet"]')
expect(cssSheet.length).toBe(1)
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8') const stylesheet = cssSheet.attr('href')
expect( const cssContent = (
cssContent.replace(/\/\*.*?\*\//g, '').trim() await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
).toMatchInlineSnapshot(`":root{--color:red}h1{color:var(--color)}"`) )
.replace(/\/\*.*?\*\//g, '')
.trim()
if (process.env.TURBOPACK) {
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim())
.toMatchInlineSnapshot(`
":root {
--color: red;
}
h1 {
color: var(--color);
}"
`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`":root{--color:red}h1{color:var(--color)}"`)
}
}) })
} }
) )
}) })
describe('Inline Comments: Minify', () => { // This is checking invalid CSS that Lightning CSS in Turbopack interprets differently. Since it's invalid syntax it's documented as unsupported.
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'Inline Comments: Minify',
() => { () => {
const appDir = join(fixturesDir, 'inline-comments') ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
let stdout () => {
let code const appDir = join(fixturesDir, 'inline-comments')
beforeAll(async () => {
await remove(join(appDir, '.next')) let appPort
;({ code, stdout } = await nextBuild(appDir, [], { let app
stdout: true, beforeAll(async () => {
})) await remove(join(appDir, '.next'))
}) const { code } = await nextBuild(appDir)
if (code !== 0) {
it('should have compiled successfully', () => { throw new Error('failed to build')
expect(code).toBe(0) }
expect(stdout).toMatch(/Compiled successfully/) appPort = await findPort()
}) app = await nextStart(appDir, appPort)
})
it(`should've emitted a single CSS file`, async () => { afterAll(async () => {
const cssFolder = join(appDir, '.next/static/css') await killApp(app)
})
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f)) it(`should've emitted a single CSS file`, async () => {
const content = await renderViaHTTP(appPort, '/')
expect(cssFiles.length).toBe(1) const $ = cheerio.load(content)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
const cssSheet = $('link[rel="stylesheet"]')
expect( expect(cssSheet.length).toBe(1)
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`"*{box-sizing:border-box}"`) const stylesheet = cssSheet.attr('href')
})
} const cssContent = (
) await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
}) )
.replace(/\/\*.*?\*\//g, '')
.trim()
if (process.env.TURBOPACK) {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`""`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`"*{box-sizing:border-box}"`)
}
})
}
)
}
)
...@@ -7668,32 +7668,29 @@ ...@@ -7668,32 +7668,29 @@
}, },
"test/integration/css-features/test/css-modules.test.js": { "test/integration/css-features/test/css-modules.test.js": {
"passed": [ "passed": [
"CSS Modules: Import Exports production mode should have compiled successfully", "CSS Modules: Import Global CSS production mode should've emitted a single CSS file"
"CSS Modules: Import Global CSS production mode should have compiled successfully"
], ],
"failed": [ "failed": [
"CSS Modules: Import Exports production mode should've emitted a single CSS file",
"CSS Modules: Import Global CSS production mode should've emitted a single CSS file",
"CSS Modules: Importing Invalid Global CSS production mode should fail to build", "CSS Modules: Importing Invalid Global CSS production mode should fail to build",
"Custom Properties: Fail for :root {} in CSS Modules production mode should fail to build",
"Custom Properties: Fail for global element in CSS Modules production mode should fail to build" "Custom Properties: Fail for global element in CSS Modules production mode should fail to build"
], ],
"pending": [], "pending": [
"CSS Modules: Import Exports production mode should have compiled successfully",
"CSS Modules: Import Exports production mode should've emitted a single CSS file",
"Custom Properties: Fail for :root {} in CSS Modules production mode should fail to build"
],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
}, },
"test/integration/css-features/test/index.test.js": { "test/integration/css-features/test/index.test.js": {
"passed": [ "passed": [
"Custom Properties: Pass-Through IE11 production mode should have compiled successfully",
"Custom Properties: Pass-Through Modern production mode should have compiled successfully",
"Inline Comments: Minify production mode should have compiled successfully"
],
"failed": [
"Custom Properties: Pass-Through IE11 production mode should've emitted a single CSS file", "Custom Properties: Pass-Through IE11 production mode should've emitted a single CSS file",
"Custom Properties: Pass-Through Modern production mode should've emitted a single CSS file", "Custom Properties: Pass-Through Modern production mode should've emitted a single CSS file"
],
"failed": [],
"pending": [
"Inline Comments: Minify production mode should've emitted a single CSS file" "Inline Comments: Minify production mode should've emitted a single CSS file"
], ],
"pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": 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