Unverified Commit 9f68435d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: implement optimized css production chunking (#77284)

### What?

Port the webpack css chunking implementation for app dir to Turbopack.

It also computes ordered entries in module batches

Pages router also uses the app dir chunking, but that's incorrect and leads to some breaking test cases.
parent 13ad4661
...@@ -471,7 +471,7 @@ pub async fn get_client_chunking_context( ...@@ -471,7 +471,7 @@ pub async fn get_client_chunking_context(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
......
...@@ -256,7 +256,7 @@ pub async fn get_edge_chunking_context_with_client_assets( ...@@ -256,7 +256,7 @@ pub async fn get_edge_chunking_context_with_client_assets(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
...@@ -319,7 +319,7 @@ pub async fn get_edge_chunking_context( ...@@ -319,7 +319,7 @@ pub async fn get_edge_chunking_context(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
......
...@@ -1041,7 +1041,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1041,7 +1041,7 @@ pub async fn get_server_chunking_context_with_client_assets(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
...@@ -1106,7 +1106,7 @@ pub async fn get_server_chunking_context( ...@@ -1106,7 +1106,7 @@ pub async fn get_server_chunking_context(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
......
...@@ -192,11 +192,7 @@ describe('app dir - css', () => { ...@@ -192,11 +192,7 @@ describe('app dir - css', () => {
const $ = await next.render$('/loading-bug/hi') const $ = await next.render$('/loading-bug/hi')
// The link tag should be hoist into head with precedence properties // The link tag should be hoist into head with precedence properties
const styles = $('head link[data-precedence]').length const styles = $('head link[data-precedence]').length
if (process.env.TURBOPACK && !isNextDev) { expect(styles).toBe(2)
expect(styles).toBe(3)
} else {
expect(styles).toBe(2)
}
expect($('body h2').text()).toBe('Loading...') expect($('body h2').text()).toBe('Loading...')
}) })
...@@ -313,11 +309,7 @@ describe('app dir - css', () => { ...@@ -313,11 +309,7 @@ describe('app dir - css', () => {
/<link rel="stylesheet" href="[^<]+\.css(\?v=\d+)?"/g /<link rel="stylesheet" href="[^<]+\.css(\?v=\d+)?"/g
), ),
].length ].length
if (process.env.TURBOPACK && !isNextDev) { expect(stylesheets).toBe(3)
expect(stylesheets).toBe(5)
} else {
expect(stylesheets).toBe(3)
}
}) })
}) })
......
...@@ -7,15 +7,19 @@ describe('css-chunking', () => { ...@@ -7,15 +7,19 @@ describe('css-chunking', () => {
// do not contain styles for the `/other` page, which can happen // do not contain styles for the `/other` page, which can happen
// when the CSSChunkingPlugin is enabled and styles are shared across // when the CSSChunkingPlugin is enabled and styles are shared across
// both routes. // both routes.
it('should be possible to disable the chunking plugin', async () => { ;(process.env.TURBOPACK ? it.skip : it)(
const $ = await next.render$('/') 'should be possible to disable the chunking plugin',
const stylesheets = $('link[rel="stylesheet"]') async () => {
stylesheets.each(async (_, element) => { const $ = await next.render$('/')
const href = element.attribs.href const stylesheets = $('link[rel="stylesheet"]')
const result = await next.fetch(href) stylesheets.each(async (_, element) => {
const css = await result.text() const href = element.attribs.href
const result = await next.fetch(href)
const css = await result.text()
expect(css).not.toContain('.otherPage') // eslint-disable-next-line jest/no-standalone-expect
}) expect(css).not.toContain('.otherPage')
}) })
}
)
}) })
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -2,7 +2,7 @@ import { jsx as _jsx } from 'react/jsx-runtime' ...@@ -2,7 +2,7 @@ import { jsx as _jsx } from 'react/jsx-runtime'
import styles from './index.module.css' import styles from './index.module.css'
export function MyContainer({ children, className, ...props }) { export function MyContainer({ children, className, ...props }) {
return /*#__PURE__*/ _jsx('dev', { return /*#__PURE__*/ _jsx('p', {
...props, ...props,
className: `${styles.button_my_container} ${className ?? ''}`, className: `${styles.button_my_container} ${className ?? ''}`,
children: children, children: children,
......
...@@ -27,7 +27,6 @@ const PAGES: Record< ...@@ -27,7 +27,6 @@ const PAGES: Record<
conflict?: boolean conflict?: boolean
brokenLoading?: boolean brokenLoading?: boolean
brokenLoadingDev?: boolean brokenLoadingDev?: boolean
brokenLoadingTurbo?: boolean
} }
> = { > = {
first: { first: {
...@@ -62,16 +61,12 @@ const PAGES: Record< ...@@ -62,16 +61,12 @@ const PAGES: Record<
}, },
'interleaved-a': { 'interleaved-a': {
group: 'interleaved', group: 'interleaved',
// TODO fix this case
brokenLoadingTurbo: true,
url: '/interleaved/a', url: '/interleaved/a',
selector: '#helloia', selector: '#helloia',
color: 'rgb(0, 255, 0)', color: 'rgb(0, 255, 0)',
}, },
'interleaved-b': { 'interleaved-b': {
group: 'interleaved', group: 'interleaved',
// TODO fix this case
brokenLoadingTurbo: true,
url: '/interleaved/b', url: '/interleaved/b',
selector: '#helloib', selector: '#helloib',
color: 'rgb(255, 0, 255)', color: 'rgb(255, 0, 255)',
...@@ -244,10 +239,6 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', true])( ...@@ -244,10 +239,6 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', true])(
// TODO fix this case // TODO fix this case
let broken = let broken =
isNextDev || ordering.some((page) => PAGES[page].brokenLoading) isNextDev || ordering.some((page) => PAGES[page].brokenLoading)
if (mode === 'turbo') {
// TODO fix this case
broken ||= ordering.some((page) => PAGES[page].brokenLoadingTurbo)
}
if (broken) { if (broken) {
it.todo(name) it.todo(name)
continue continue
...@@ -304,10 +295,6 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', 'loose'])( ...@@ -304,10 +295,6 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', 'loose'])(
PAGES[page].brokenLoading || PAGES[page].brokenLoading ||
(isNextDev && PAGES[page].brokenLoadingDev) (isNextDev && PAGES[page].brokenLoadingDev)
) )
if (mode === 'turbo') {
// TODO fix this case
broken ||= ordering.some((page) => PAGES[page].brokenLoadingTurbo)
}
if (broken) { if (broken) {
it.todo(name) it.todo(name)
continue continue
...@@ -341,7 +328,7 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', 'loose'])( ...@@ -341,7 +328,7 @@ describe.each(process.env.TURBOPACK ? ['turbo'] : ['strict', 'loose'])(
const { next } = nextTestSetup(options(mode)) const { next } = nextTestSetup(options(mode))
for (const [page, pageInfo] of Object.entries(PAGES)) { for (const [page, pageInfo] of Object.entries(PAGES)) {
const name = `should load correct styles on ${page}` const name = `should load correct styles on ${page}`
if (mode === 'loose' && pageInfo.conflict) { if (mode !== 'strict' && pageInfo.conflict) {
// Conflict scenarios won't support that case // Conflict scenarios won't support that case
continue continue
} }
......
.red-text { .red-text {
color: red; color: red;
background-image: url('./dark.svg') url(dark2.svg); background-image: url('./dark.svg'), url(dark2.svg);
} }
...@@ -576,9 +576,10 @@ module.exports = { ...@@ -576,9 +576,10 @@ module.exports = {
if (process.env.TURBOPACK && useLightningcss) { if (process.env.TURBOPACK && useLightningcss) {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/chunks/test_integration_css-fixtures_url-global_styles_global1_69590c47.css: "/_next/static/chunks/test_integration_css-fixtures_url-global_styles_46281715._.css:
.red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}", .red-text{color:red;background-image:url(../media/dark.993bedd3.svg),url(../media/dark2.993bedd3.svg)}
"/_next/static/chunks/test_integration_css-fixtures_url-global_styles_3a258468._.css:
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder} .blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
...@@ -588,9 +589,10 @@ module.exports = { ...@@ -588,9 +589,10 @@ module.exports = {
} else if (process.env.TURBOPACK && !useLightningcss) { } else if (process.env.TURBOPACK && !useLightningcss) {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/chunks/test_integration_css-fixtures_url-global_styles_global1_69590c47.css: "/_next/static/chunks/test_integration_css-fixtures_url-global_styles_46281715._.css:
.red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}", .red-text{color:red;background-image:url(../media/dark.993bedd3.svg),url(../media/dark2.993bedd3.svg)}
"/_next/static/chunks/test_integration_css-fixtures_url-global_styles_3a258468._.css:
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder} .blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
...@@ -600,15 +602,15 @@ module.exports = { ...@@ -600,15 +602,15 @@ module.exports = {
} else if (useLightningcss) { } else if (useLightningcss) {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/css/b52d68d7050c7314.css: "/_next/static/css/02830b0dd136adeb.css:
.red-text{color:red;background-image:url(/_next/static/media/dark.6b01655b.svg) url(/_next/static/media/dark2.6b01655b.svg)}.blue-text{color:orange;background-image:url(/_next/static/media/light.2da1d3d6.svg);font-weight:bolder;color:#00f}", .red-text{color:red;background-image:url(/_next/static/media/dark.6b01655b.svg),url(/_next/static/media/dark2.6b01655b.svg)}.blue-text{color:orange;background-image:url(/_next/static/media/light.2da1d3d6.svg);font-weight:bolder;color:#00f}",
] ]
`) `)
} else { } else {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/css/766d42b9536ee6bb.css: "/_next/static/css/bf86b6e30872ca37.css:
.red-text{color:red;background-image:url(/_next/static/media/dark.6b01655b.svg) url(/_next/static/media/dark2.6b01655b.svg)}.blue-text{color:orange;font-weight:bolder;background-image:url(/_next/static/media/light.2da1d3d6.svg);color:blue}", .red-text{color:red;background-image:url(/_next/static/media/dark.6b01655b.svg),url(/_next/static/media/dark2.6b01655b.svg)}.blue-text{color:orange;font-weight:bolder;background-image:url(/_next/static/media/light.2da1d3d6.svg);color:blue}",
] ]
`) `)
} }
...@@ -651,9 +653,10 @@ describe('CSS URL via `file-loader` and asset prefix (1)', () => { ...@@ -651,9 +653,10 @@ describe('CSS URL via `file-loader` and asset prefix (1)', () => {
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-1_styles_global1_64764808.css: "/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-1_styles_e99cb7f8._.css:
.red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}", .red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}
"/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-1_styles_0a127993._.css:
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder} .blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
...@@ -706,9 +709,10 @@ describe('CSS URL via `file-loader` and asset prefix (2)', () => { ...@@ -706,9 +709,10 @@ describe('CSS URL via `file-loader` and asset prefix (2)', () => {
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(cssContent).toMatchInlineSnapshot(` expect(cssContent).toMatchInlineSnapshot(`
[ [
"/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-2_styles_global1_cfbf8de3.css: "/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-2_styles_7b29c72b._.css:
.red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}", .red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}
"/_next/static/chunks/test_integration_css-fixtures_url-global-asset-prefix-2_styles_764eafae._.css:
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder} .blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
......
...@@ -362,7 +362,8 @@ module.exports = { ...@@ -362,7 +362,8 @@ module.exports = {
}) })
} }
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( // This test case will fail in Turbopack when both pages share some css chunks. Deleting them will cause the / page to fail too.
;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'production mode',
() => { () => {
beforeAll(async () => { beforeAll(async () => {
......
...@@ -1565,6 +1565,7 @@ ...@@ -1565,6 +1565,7 @@
"css-order turbo should load correct styles navigating back again pages-first -> pages-second -> pages-first -> pages-second", "css-order turbo should load correct styles navigating back again pages-first -> pages-second -> pages-first -> pages-second",
"css-order turbo should load correct styles navigating back again pages-first -> pages-third -> pages-first -> pages-third", "css-order turbo should load correct styles navigating back again pages-first -> pages-third -> pages-first -> pages-third",
"css-order turbo should load correct styles navigating back again pages-interleaved-a -> pages-interleaved-b -> pages-interleaved-a -> pages-interleaved-b", "css-order turbo should load correct styles navigating back again pages-interleaved-a -> pages-interleaved-b -> pages-interleaved-a -> pages-interleaved-b",
"css-order turbo should load correct styles navigating back again pages-interleaved-b -> pages-interleaved-a -> pages-interleaved-b -> pages-interleaved-a",
"css-order turbo should load correct styles navigating back again pages-second -> pages-first -> pages-second -> pages-first", "css-order turbo should load correct styles navigating back again pages-second -> pages-first -> pages-second -> pages-first",
"css-order turbo should load correct styles navigating back again pages-second -> pages-third -> pages-second -> pages-third", "css-order turbo should load correct styles navigating back again pages-second -> pages-third -> pages-second -> pages-third",
"css-order turbo should load correct styles navigating back again pages-third -> pages-first -> pages-third -> pages-first", "css-order turbo should load correct styles navigating back again pages-third -> pages-first -> pages-third -> pages-first",
...@@ -1592,6 +1593,7 @@ ...@@ -1592,6 +1593,7 @@
"css-order turbo should load correct styles navigating pages-first -> pages-second", "css-order turbo should load correct styles navigating pages-first -> pages-second",
"css-order turbo should load correct styles navigating pages-first -> pages-third", "css-order turbo should load correct styles navigating pages-first -> pages-third",
"css-order turbo should load correct styles navigating pages-interleaved-a -> pages-interleaved-b", "css-order turbo should load correct styles navigating pages-interleaved-a -> pages-interleaved-b",
"css-order turbo should load correct styles navigating pages-interleaved-b -> pages-interleaved-a",
"css-order turbo should load correct styles navigating pages-second -> pages-first", "css-order turbo should load correct styles navigating pages-second -> pages-first",
"css-order turbo should load correct styles navigating pages-second -> pages-third", "css-order turbo should load correct styles navigating pages-second -> pages-third",
"css-order turbo should load correct styles navigating pages-third -> pages-first", "css-order turbo should load correct styles navigating pages-third -> pages-first",
...@@ -1620,31 +1622,25 @@ ...@@ -1620,31 +1622,25 @@
"css-order turbo should load correct styles on pages-interleaved-a", "css-order turbo should load correct styles on pages-interleaved-a",
"css-order turbo should load correct styles on pages-interleaved-b", "css-order turbo should load correct styles on pages-interleaved-b",
"css-order turbo should load correct styles on pages-partial-reversed-a", "css-order turbo should load correct styles on pages-partial-reversed-a",
"css-order turbo should load correct styles on pages-partial-reversed-b",
"css-order turbo should load correct styles on pages-reversed-a", "css-order turbo should load correct styles on pages-reversed-a",
"css-order turbo should load correct styles on pages-reversed-b",
"css-order turbo should load correct styles on pages-second", "css-order turbo should load correct styles on pages-second",
"css-order turbo should load correct styles on pages-third", "css-order turbo should load correct styles on pages-third",
"css-order turbo should load correct styles on partial-reversed-a",
"css-order turbo should load correct styles on partial-reversed-b",
"css-order turbo should load correct styles on reversed-a",
"css-order turbo should load correct styles on reversed-b",
"css-order turbo should load correct styles on second", "css-order turbo should load correct styles on second",
"css-order turbo should load correct styles on second-client", "css-order turbo should load correct styles on second-client",
"css-order turbo should load correct styles on third", "css-order turbo should load correct styles on third",
"css-order turbo should load correct styles on vendor" "css-order turbo should load correct styles on vendor"
], ],
"failed": [ "failed": [
"css-order turbo should load correct styles navigating back again pages-interleaved-b -> pages-interleaved-a -> pages-interleaved-b -> pages-interleaved-a",
"css-order turbo should load correct styles navigating back again pages-partial-reversed-a -> pages-partial-reversed-b -> pages-partial-reversed-a -> pages-partial-reversed-b", "css-order turbo should load correct styles navigating back again pages-partial-reversed-a -> pages-partial-reversed-b -> pages-partial-reversed-a -> pages-partial-reversed-b",
"css-order turbo should load correct styles navigating back again pages-partial-reversed-b -> pages-partial-reversed-a -> pages-partial-reversed-b -> pages-partial-reversed-a", "css-order turbo should load correct styles navigating back again pages-partial-reversed-b -> pages-partial-reversed-a -> pages-partial-reversed-b -> pages-partial-reversed-a",
"css-order turbo should load correct styles navigating back again pages-reversed-a -> pages-reversed-b -> pages-reversed-a -> pages-reversed-b", "css-order turbo should load correct styles navigating back again pages-reversed-a -> pages-reversed-b -> pages-reversed-a -> pages-reversed-b",
"css-order turbo should load correct styles navigating back again pages-reversed-b -> pages-reversed-a -> pages-reversed-b -> pages-reversed-a", "css-order turbo should load correct styles navigating back again pages-reversed-b -> pages-reversed-a -> pages-reversed-b -> pages-reversed-a",
"css-order turbo should load correct styles navigating pages-interleaved-b -> pages-interleaved-a",
"css-order turbo should load correct styles navigating pages-partial-reversed-a -> pages-partial-reversed-b", "css-order turbo should load correct styles navigating pages-partial-reversed-a -> pages-partial-reversed-b",
"css-order turbo should load correct styles navigating pages-partial-reversed-b -> pages-partial-reversed-a", "css-order turbo should load correct styles navigating pages-partial-reversed-b -> pages-partial-reversed-a",
"css-order turbo should load correct styles navigating pages-reversed-a -> pages-reversed-b", "css-order turbo should load correct styles navigating pages-reversed-a -> pages-reversed-b",
"css-order turbo should load correct styles navigating pages-reversed-b -> pages-reversed-a" "css-order turbo should load correct styles navigating pages-reversed-b -> pages-reversed-a",
"css-order turbo should load correct styles on pages-partial-reversed-b",
"css-order turbo should load correct styles on pages-reversed-b"
], ],
"pending": [ "pending": [
"css-order turbo should load correct styles navigating back again big-interleaved-a -> big-interleaved-b -> big-interleaved-a -> big-interleaved-b", "css-order turbo should load correct styles navigating back again big-interleaved-a -> big-interleaved-b -> big-interleaved-a -> big-interleaved-b",
...@@ -7274,15 +7270,16 @@ ...@@ -7274,15 +7270,16 @@
"next/font import values Variable font without weight range", "next/font import values Variable font without weight range",
"next/font import values page with font", "next/font import values page with font",
"next/font import values page with local fonts", "next/font import values page with local fonts",
"next/font preload page with fonts",
"next/font should not have deprecation warning"
],
"failed": [
"next/font preload font without preloadable subsets", "next/font preload font without preloadable subsets",
"next/font preload font without size adjust", "next/font preload font without size adjust",
"next/font preload google fonts with multiple weights/styles", "next/font preload google fonts with multiple weights/styles",
"next/font preload page with fonts",
"next/font preload page with local fonts", "next/font preload page with local fonts",
"next/font preload page without fonts", "next/font preload page without fonts"
"next/font should not have deprecation warning"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
...@@ -9705,13 +9702,13 @@ ...@@ -9705,13 +9702,13 @@
"CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should hydrate black without dependencies manifest", "CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should hydrate black without dependencies manifest",
"CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should hydrate red without dependencies manifest", "CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should hydrate red without dependencies manifest",
"CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should route from black to red without dependencies", "CSS Support Page hydrates with CSS and not waiting on dependencies useLightnincsss(true) production mode should route from black to red without dependencies",
"CSS Support Page reload on CSS missing useLightnincsss(false) production mode should fall back to server-side transition on missing CSS",
"CSS Support Page reload on CSS missing useLightnincsss(true) production mode should fall back to server-side transition on missing CSS",
"CSS Support production mode CSS Import from node_modules useLightnincsss(false) should fail the build", "CSS Support production mode CSS Import from node_modules useLightnincsss(false) should fail the build",
"CSS Support production mode CSS Import from node_modules useLightnincsss(true) should fail the build" "CSS Support production mode CSS Import from node_modules useLightnincsss(true) should fail the build"
], ],
"failed": [], "failed": [],
"pending": [ "pending": [
"CSS Support Page reload on CSS missing useLightnincsss(false) production mode should fall back to server-side transition on missing CSS",
"CSS Support Page reload on CSS missing useLightnincsss(true) production mode should fall back to server-side transition on missing CSS",
"CSS Support CSS Cleanup on Render Failure useLightnincsss(false) production mode not have intermediary page styles on error rendering", "CSS Support CSS Cleanup on Render Failure useLightnincsss(false) production mode not have intermediary page styles on error rendering",
"CSS Support CSS Cleanup on Render Failure useLightnincsss(true) production mode not have intermediary page styles on error rendering" "CSS Support CSS Cleanup on Render Failure useLightnincsss(true) production mode not have intermediary page styles on error rendering"
], ],
...@@ -350,7 +350,7 @@ async fn build_internal( ...@@ -350,7 +350,7 @@ async fn build_internal(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
...@@ -393,7 +393,7 @@ async fn build_internal( ...@@ -393,7 +393,7 @@ async fn build_internal(
builder = builder.chunking_config( builder = builder.chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
); );
......
...@@ -20,7 +20,7 @@ use crate::{ ...@@ -20,7 +20,7 @@ use crate::{
}, },
}; };
async fn attach_async_info_to_chunkable_module( pub async fn attach_async_info_to_chunkable_module(
module: ResolvedVc<Box<dyn ChunkableModule>>, module: ResolvedVc<Box<dyn ChunkableModule>>,
async_module_info: &ReadRef<AsyncModulesInfo>, async_module_info: &ReadRef<AsyncModulesInfo>,
module_graph: Vc<ModuleGraph>, module_graph: Vc<ModuleGraph>,
...@@ -105,6 +105,7 @@ impl ChunkItemOrBatchWithAsyncModuleInfo { ...@@ -105,6 +105,7 @@ impl ChunkItemOrBatchWithAsyncModuleInfo {
} }
#[turbo_tasks::value] #[turbo_tasks::value]
#[derive(Debug, Clone, Hash, TaskInput)]
pub struct ChunkItemBatchWithAsyncModuleInfo { pub struct ChunkItemBatchWithAsyncModuleInfo {
pub chunk_items: Vec<ChunkItemWithAsyncModuleInfo>, pub chunk_items: Vec<ChunkItemWithAsyncModuleInfo>,
pub chunk_groups: Option<RoaringBitmapWrapper>, pub chunk_groups: Option<RoaringBitmapWrapper>,
...@@ -112,6 +113,15 @@ pub struct ChunkItemBatchWithAsyncModuleInfo { ...@@ -112,6 +113,15 @@ pub struct ChunkItemBatchWithAsyncModuleInfo {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ChunkItemBatchWithAsyncModuleInfo { impl ChunkItemBatchWithAsyncModuleInfo {
#[turbo_tasks::function]
pub fn new(chunk_items: Vec<ChunkItemWithAsyncModuleInfo>) -> Vc<Self> {
Self {
chunk_items,
chunk_groups: None,
}
.cell()
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn from_module_batch( pub async fn from_module_batch(
batch: Vc<ModuleBatch>, batch: Vc<ModuleBatch>,
......
...@@ -21,8 +21,8 @@ use crate::{ ...@@ -21,8 +21,8 @@ use crate::{
}, },
chunking::{ chunking::{
dev::{app_vendors_split, expand_batches}, dev::{app_vendors_split, expand_batches},
ordered_production::make_production_chunks_keeping_order,
production::make_production_chunks, production::make_production_chunks,
style_production::make_style_production_chunks,
}, },
}, },
module_graph::ModuleGraph, module_graph::ModuleGraph,
...@@ -30,8 +30,8 @@ use crate::{ ...@@ -30,8 +30,8 @@ use crate::{
}; };
mod dev; mod dev;
mod ordered_production;
mod production; mod production;
mod style_production;
#[turbo_tasks::value] #[turbo_tasks::value]
struct ChunkItemsWithInfo { struct ChunkItemsWithInfo {
...@@ -336,17 +336,18 @@ pub async fn make_chunks( ...@@ -336,17 +336,18 @@ pub async fn make_chunks(
if let Some(chunking_config) = chunking_configs.get(&ty) { if let Some(chunking_config) = chunking_configs.get(&ty) {
// Production chunking // Production chunking
if !*ty.must_keep_item_order().await? { if *ty.is_style().await? {
make_production_chunks( make_style_production_chunks(
chunk_items, chunk_items,
batch_groups.into_iter().collect(), batch_groups.into_iter().collect(),
module_graph, module_graph,
chunking_context,
chunking_config, chunking_config,
split_context, split_context,
) )
.await?; .await?;
} else { } else {
make_production_chunks_keeping_order( make_production_chunks(
chunk_items, chunk_items,
batch_groups.into_iter().collect(), batch_groups.into_iter().collect(),
module_graph, module_graph,
...@@ -357,20 +358,20 @@ pub async fn make_chunks( ...@@ -357,20 +358,20 @@ pub async fn make_chunks(
} }
} else { } else {
// Development chunking // Development chunking
if !*ty.must_keep_item_order().await? { if *ty.is_style().await? {
let chunk_items = expand_batches(chunk_items, ty, chunking_context).await?; make_chunk(
let chunk_items = chunk_items.iter().collect();
app_vendors_split(
chunk_items, chunk_items,
format!("{key_prefix}{ty_name}"), Vec::new(),
&mut format!("{key_prefix}{ty_name}"),
&mut split_context, &mut split_context,
) )
.await?; .await?;
} else { } else {
make_chunk( let chunk_items = expand_batches(chunk_items, ty, chunking_context).await?;
let chunk_items = chunk_items.iter().collect();
app_vendors_split(
chunk_items, chunk_items,
Vec::new(), format!("{key_prefix}{ty_name}"),
&mut format!("{key_prefix}{ty_name}"),
&mut split_context, &mut split_context,
) )
.await?; .await?;
......
use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{ResolvedVc, Vc};
use crate::{
chunk::{
chunking::{make_chunk, ChunkItemOrBatchWithInfo, SplitContext},
ChunkItemBatchGroup, ChunkingConfig,
},
module_graph::ModuleGraph,
};
pub async fn make_production_chunks_keeping_order(
chunk_items: Vec<&ChunkItemOrBatchWithInfo>,
_batch_groups: Vec<ResolvedVc<ChunkItemBatchGroup>>,
_module_graph: Vc<ModuleGraph>,
_chunking_config: &ChunkingConfig,
mut split_context: SplitContext<'_>,
) -> Result<()> {
let span_outer = tracing::info_span!(
"make production chunks keeping order",
chunk_items = chunk_items.len(),
);
async move {
for chunk_item in chunk_items {
make_chunk(
vec![chunk_item],
vec![],
&mut String::new(),
&mut split_context,
)
.await?;
}
Ok(())
}
.instrument(span_outer)
.await
}
use anyhow::Result;
use rustc_hash::FxHashSet;
use tracing::Instrument;
use turbo_tasks::{ResolvedVc, Vc};
use crate::{
chunk::{
chunking::{make_chunk, ChunkItemOrBatchWithInfo, SplitContext},
ChunkItemBatchGroup, ChunkItemWithAsyncModuleInfo, ChunkingConfig, ChunkingContext,
},
module_graph::{style_groups::StyleGroupsConfig, ModuleGraph},
};
pub async fn make_style_production_chunks(
chunk_items: Vec<&ChunkItemOrBatchWithInfo>,
_batch_groups: Vec<ResolvedVc<ChunkItemBatchGroup>>,
module_graph: Vc<ModuleGraph>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
chunking_config: &ChunkingConfig,
mut split_context: SplitContext<'_>,
) -> Result<()> {
let span_outer = tracing::info_span!(
"make production chunks keeping order",
chunk_items = chunk_items.len(),
);
async move {
let style_groups = module_graph
.style_groups(
chunking_context,
StyleGroupsConfig {
max_chunk_size: chunking_config.max_merge_chunk_size,
},
)
.await?;
let mut handled = FxHashSet::default();
let mut handle_chunk_item = async |chunk_item: &ChunkItemWithAsyncModuleInfo| {
if let Some(&batch) = style_groups.shared_chunk_items.get(chunk_item) {
if handled.insert(batch) {
make_chunk(
vec![&ChunkItemOrBatchWithInfo::Batch { batch, size: 0 }],
vec![],
&mut String::new(),
&mut split_context,
)
.await?;
}
} else {
make_chunk(
vec![&ChunkItemOrBatchWithInfo::ChunkItem {
chunk_item: chunk_item.clone(),
size: 0,
asset_ident: "".into(),
}],
vec![],
&mut String::new(),
&mut split_context,
)
.await?;
}
anyhow::Ok(())
};
for chunk_item in chunk_items {
match chunk_item {
ChunkItemOrBatchWithInfo::ChunkItem { chunk_item, .. } => {
handle_chunk_item(chunk_item).await?;
}
ChunkItemOrBatchWithInfo::Batch { batch, .. } => {
for chunk_item in &batch.await?.chunk_items {
handle_chunk_item(chunk_item).await?;
}
}
};
}
Ok(())
}
.instrument(span_outer)
.await
}
...@@ -310,7 +310,7 @@ pub trait ChunkItem { ...@@ -310,7 +310,7 @@ pub trait ChunkItem {
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait ChunkType: ValueToString { pub trait ChunkType: ValueToString {
/// Whether the source (reference) order of items needs to be retained during chunking. /// Whether the source (reference) order of items needs to be retained during chunking.
fn must_keep_item_order(self: Vc<Self>) -> Vc<bool>; fn is_style(self: Vc<Self>) -> Vc<bool>;
/// Create a new chunk for the given chunk items /// Create a new chunk for the given chunk items
fn chunk( fn chunk(
...@@ -417,6 +417,8 @@ mod tests { ...@@ -417,6 +417,8 @@ mod tests {
assert_eq!(round_chunk_item_size(6), 6); assert_eq!(round_chunk_item_size(6), 6);
assert_eq!(round_chunk_item_size(7), 6); assert_eq!(round_chunk_item_size(7), 6);
assert_eq!(round_chunk_item_size(8), 8); assert_eq!(round_chunk_item_size(8), 8);
assert_eq!(round_chunk_item_size(49000), 32_768);
assert_eq!(round_chunk_item_size(50000), 49_152);
assert_eq!(changes_in_range(0..1000), 19); assert_eq!(changes_in_range(0..1000), 19);
assert_eq!(changes_in_range(1000..2000), 2); assert_eq!(changes_in_range(1000..2000), 2);
......
use turbo_tasks::{ResolvedVc, Vc}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, NonLocalValue, ResolvedVc, Vc};
use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences}; use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences};
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs, NonLocalValue)]
pub enum StyleType {
IsolatedStyle,
GlobalStyle,
}
#[turbo_tasks::value(transparent)]
pub struct OptionStyleType(Option<StyleType>);
/// A module. This usually represents parsed source code, which has references /// A module. This usually represents parsed source code, which has references
/// to other modules. /// to other modules.
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
...@@ -20,6 +30,11 @@ pub trait Module: Asset { ...@@ -20,6 +30,11 @@ pub trait Module: Asset {
fn is_self_async(self: Vc<Self>) -> Vc<bool> { fn is_self_async(self: Vc<Self>) -> Vc<bool> {
Vc::cell(false) Vc::cell(false)
} }
/// The style type of the module.
fn style_type(self: Vc<Self>) -> Vc<OptionStyleType> {
Vc::cell(None)
}
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
......
...@@ -20,13 +20,14 @@ use turbo_tasks::{ ...@@ -20,13 +20,14 @@ use turbo_tasks::{
}; };
use crate::{ use crate::{
chunk::{AsyncModuleInfo, ChunkingType}, chunk::{AsyncModuleInfo, ChunkingContext, ChunkingType},
issue::Issue, issue::Issue,
module::Module, module::Module,
module_graph::{ module_graph::{
async_module_info::{compute_async_module_info, AsyncModulesInfo}, async_module_info::{compute_async_module_info, AsyncModulesInfo},
chunk_group_info::{compute_chunk_group_info, ChunkGroupEntry, ChunkGroupInfo}, chunk_group_info::{compute_chunk_group_info, ChunkGroupEntry, ChunkGroupInfo},
module_batches::{compute_module_batches, ModuleBatchesGraph}, module_batches::{compute_module_batches, ModuleBatchesGraph},
style_groups::{compute_style_groups, StyleGroups, StyleGroupsConfig},
traced_di_graph::{iter_neighbors_rev, TracedDiGraph}, traced_di_graph::{iter_neighbors_rev, TracedDiGraph},
}, },
reference::primary_chunkable_referenced_modules, reference::primary_chunkable_referenced_modules,
...@@ -36,6 +37,7 @@ pub mod async_module_info; ...@@ -36,6 +37,7 @@ pub mod async_module_info;
pub mod chunk_group_info; pub mod chunk_group_info;
pub mod module_batch; pub mod module_batch;
pub(crate) mod module_batches; pub(crate) mod module_batches;
pub(crate) mod style_groups;
mod traced_di_graph; mod traced_di_graph;
pub use self::module_batches::BatchingConfig; pub use self::module_batches::BatchingConfig;
...@@ -730,10 +732,18 @@ impl ModuleGraph { ...@@ -730,10 +732,18 @@ impl ModuleGraph {
self: Vc<Self>, self: Vc<Self>,
config: Vc<BatchingConfig>, config: Vc<BatchingConfig>,
) -> Result<Vc<ModuleBatchesGraph>> { ) -> Result<Vc<ModuleBatchesGraph>> {
// TODO dev: Return modules only
compute_module_batches(self, &*config.await?).await compute_module_batches(self, &*config.await?).await
} }
#[turbo_tasks::function]
pub async fn style_groups(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
config: StyleGroupsConfig,
) -> Result<Vc<StyleGroups>> {
compute_style_groups(self, chunking_context, &config).await
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn async_module_info(self: Vc<Self>) -> Result<Vc<AsyncModulesInfo>> { pub async fn async_module_info(self: Vc<Self>) -> Result<Vc<AsyncModulesInfo>> {
// `compute_async_module_info` calls `module.is_self_async()`, so we need to again ignore // `compute_async_module_info` calls `module.is_self_async()`, so we need to again ignore
......
...@@ -7,7 +7,7 @@ use turbopack_core::{ ...@@ -7,7 +7,7 @@ use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MinifyType}, chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MinifyType},
context::AssetContext, context::AssetContext,
ident::AssetIdent, ident::AssetIdent,
module::Module, module::{Module, OptionStyleType, StyleType},
module_graph::ModuleGraph, module_graph::ModuleGraph,
output::OutputAssets, output::OutputAssets,
reference::{ModuleReference, ModuleReferences}, reference::{ModuleReference, ModuleReferences},
...@@ -133,6 +133,15 @@ impl Module for CssModuleAsset { ...@@ -133,6 +133,15 @@ impl Module for CssModuleAsset {
ParseCssResult::NotFound => Ok(ModuleReferences::empty()), ParseCssResult::NotFound => Ok(ModuleReferences::empty()),
} }
} }
#[turbo_tasks::function]
fn style_type(&self) -> Vc<OptionStyleType> {
let style_type = match self.ty {
CssModuleAssetType::Default => StyleType::GlobalStyle,
CssModuleAssetType::Module => StyleType::IsolatedStyle,
};
Vc::cell(Some(style_type))
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -498,7 +498,7 @@ impl ValueToString for CssChunkType { ...@@ -498,7 +498,7 @@ impl ValueToString for CssChunkType {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ChunkType for CssChunkType { impl ChunkType for CssChunkType {
#[turbo_tasks::function] #[turbo_tasks::function]
fn must_keep_item_order(self: Vc<Self>) -> Vc<bool> { fn is_style(self: Vc<Self>) -> Vc<bool> {
Vc::cell(true) Vc::cell(true)
} }
......
...@@ -27,7 +27,7 @@ impl ValueToString for EcmascriptChunkType { ...@@ -27,7 +27,7 @@ impl ValueToString for EcmascriptChunkType {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ChunkType for EcmascriptChunkType { impl ChunkType for EcmascriptChunkType {
#[turbo_tasks::function] #[turbo_tasks::function]
fn must_keep_item_order(self: Vc<Self>) -> Vc<bool> { fn is_style(self: Vc<Self>) -> Vc<bool> {
Vc::cell(false) Vc::cell(false)
} }
......
...@@ -427,7 +427,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V ...@@ -427,7 +427,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
.chunking_config( .chunking_config(
Vc::<CssChunkType>::default().to_resolved().await?, Vc::<CssChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
min_chunk_size: 0, max_merge_chunk_size: 100_000,
..Default::default() ..Default::default()
}, },
) )
......
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