@@ -1735,10 +1735,27 @@ async function buildRoute( routeName ) {
17351735 *
17361736 * @return {Promise<void> }
17371737 */
1738- async function buildAllRoutes ( ) {
1738+ async function buildAllRoutes ( activePageIds = null ) {
17391739 console . log ( '\n🚦 Phase 3: Building routes...\n' ) ;
17401740
1741- const routes = getAllRoutes ( ROOT_DIR ) ;
1741+ const allRoutes = getAllRoutes ( ROOT_DIR ) ;
1742+
1743+ // When activePageIds is provided, skip routes whose pages are all inactive.
1744+ const routes = activePageIds
1745+ ? allRoutes . filter ( ( routeName ) => {
1746+ const metadata = getRouteMetadata ( ROOT_DIR , routeName ) ;
1747+ if (
1748+ ! metadata ||
1749+ ! metadata . pages ||
1750+ metadata . pages . length === 0
1751+ ) {
1752+ return false ;
1753+ }
1754+ return metadata . pages . some ( ( page ) =>
1755+ activePageIds . has ( page )
1756+ ) ;
1757+ } )
1758+ : allRoutes ;
17421759
17431760 if ( routes . length === 0 ) {
17441761 console . log ( ' No routes found, skipping.\n' ) ;
@@ -1821,8 +1838,31 @@ async function buildAll( baseUrlExpression ) {
18211838 } )
18221839 ) ;
18231840
1824- // Build routes
1825- await buildAllRoutes ( ) ;
1841+ // Normalize PAGES config to support both string and object formats
1842+ const normalizedPages = PAGES . map ( ( page ) => {
1843+ if ( typeof page === 'string' ) {
1844+ return { id : page , init : [ ] , title : undefined } ;
1845+ }
1846+ return {
1847+ id : page . id ,
1848+ init : page . init || [ ] ,
1849+ title : page . title || undefined ,
1850+ experimental : page . experimental || false ,
1851+ } ;
1852+ } ) ;
1853+
1854+ // When building for WordPress Core, exclude experimental pages.
1855+ const isCoreBuild =
1856+ boolConfigVal ( process . env . IS_WORDPRESS_CORE ) ??
1857+ boolConfigVal ( process . env . npm_package_config_IS_WORDPRESS_CORE ) ;
1858+ const activePages = isCoreBuild
1859+ ? normalizedPages . filter ( ( page ) => ! page . experimental )
1860+ : normalizedPages ;
1861+
1862+ const activePageIds = new Set ( activePages . map ( ( p ) => p . id ) ) ;
1863+
1864+ // Build only routes that belong to active pages.
1865+ await buildAllRoutes ( activePageIds ) ;
18261866
18271867 // Collect route and page data for PHP generation
18281868 // Use flatMap to expand routes with multiple pages into separate entries
@@ -1849,28 +1889,6 @@ async function buildAll( baseUrlExpression ) {
18491889 } ) ;
18501890 } ) ;
18511891
1852- // Normalize PAGES config to support both string and object formats
1853- const normalizedPages = PAGES . map ( ( page ) => {
1854- if ( typeof page === 'string' ) {
1855- return { id : page , init : [ ] , title : undefined } ;
1856- }
1857- return {
1858- id : page . id ,
1859- init : page . init || [ ] ,
1860- title : page . title || undefined ,
1861- experimental : page . experimental || false ,
1862- } ;
1863- } ) ;
1864-
1865- // When building for WordPress Core, exclude experimental pages.
1866- const isCoreBuild =
1867- boolConfigVal ( process . env . IS_WORDPRESS_CORE ) ??
1868- boolConfigVal ( process . env . npm_package_config_IS_WORDPRESS_CORE ) ;
1869- const activePages = isCoreBuild
1870- ? normalizedPages . filter ( ( page ) => ! page . experimental )
1871- : normalizedPages ;
1872-
1873- const activePageIds = new Set ( activePages . map ( ( p ) => p . id ) ) ;
18741892 const activeRoutes = routes . filter ( ( r ) => activePageIds . has ( r . page ) ) ;
18751893
18761894 const pageData = activePages . map ( ( page ) => {
0 commit comments