Skip to content

Commit 0a7e1cc

Browse files
authored
Build V2: Supports packages with styles (#72242)
1 parent fd16d0e commit 0a7e1cc

File tree

74 files changed

+853
-109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+853
-109
lines changed

bin/packages/build-v2.mjs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import watch from 'node-watch';
1313
// See https://github.com/WordPress/gutenberg/issues/72136
1414
// eslint-disable-next-line import/no-unresolved
1515
import browserslistToEsbuild from 'browserslist-to-esbuild';
16+
import { sassPlugin } from 'esbuild-sass-plugin';
17+
import postcss from 'postcss';
18+
import autoprefixer from 'autoprefixer';
19+
import rtlcss from 'rtlcss';
1620

1721
/**
1822
* Internal dependencies
@@ -513,6 +517,33 @@ async function bundlePackage( packageName ) {
513517
}
514518
}
515519

520+
// Copy CSS files from build-style to build directory (for wpScript packages)
521+
if ( packageJson.wpScript ) {
522+
const buildStyleDir = path.join( packageDir, 'build-style' );
523+
const outputDir = path.join( PACKAGES_DIR, '..', 'build', packageName );
524+
525+
try {
526+
// Find CSS files in build-style directory
527+
const cssFiles = await glob(
528+
normalizePath( path.join( buildStyleDir, '*.css' ) )
529+
);
530+
531+
if ( cssFiles.length > 0 ) {
532+
// Ensure output directory exists
533+
await mkdir( outputDir, { recursive: true } );
534+
535+
// Copy each CSS file
536+
for ( const cssFile of cssFiles ) {
537+
const filename = path.basename( cssFile );
538+
const destPath = path.join( outputDir, filename );
539+
builds.push( copyFile( cssFile, destPath ) );
540+
}
541+
}
542+
} catch ( error ) {
543+
// build-style doesn't exist or is empty - that's fine, not all packages have styles
544+
}
545+
}
546+
516547
if ( builds.length === 0 ) {
517548
return false;
518549
}
@@ -625,11 +656,84 @@ async function transpilePackage( packageName ) {
625656
}
626657
}
627658

659+
await compileStyles( packageName );
660+
628661
await Promise.all( builds );
629662

630663
return Date.now() - startTime;
631664
}
632665

666+
/**
667+
* Compile styles for a single package.
668+
*
669+
* @param {string} packageName Package name.
670+
* @return {Promise<number|null>} Build time in milliseconds, or null if no styles.
671+
*/
672+
async function compileStyles( packageName ) {
673+
const packageDir = path.join( PACKAGES_DIR, packageName );
674+
const styleEntryPath = path.join( packageDir, 'src', 'style.scss' );
675+
676+
// Check if style entry point exists
677+
try {
678+
await readFile( styleEntryPath );
679+
} catch {
680+
return null;
681+
}
682+
683+
const startTime = Date.now();
684+
const buildStyleDir = path.join( packageDir, 'build-style' );
685+
686+
// Create build-style directory
687+
await mkdir( buildStyleDir, { recursive: true } );
688+
689+
// Build with Sass plugin
690+
await esbuild.build( {
691+
entryPoints: [ styleEntryPath ],
692+
outdir: buildStyleDir,
693+
bundle: true,
694+
write: false,
695+
loader: {
696+
'.scss': 'css',
697+
},
698+
plugins: [
699+
sassPlugin( {
700+
loadPaths: [
701+
'node_modules',
702+
path.join( PACKAGES_DIR, 'base-styles' ),
703+
],
704+
async transform( source ) {
705+
// Process with autoprefixer for LTR version
706+
const ltrResult = await postcss( [
707+
autoprefixer( { grid: true } ),
708+
] ).process( source, { from: undefined } );
709+
710+
// Process with rtlcss for RTL version
711+
const rtlResult = await postcss( [ rtlcss() ] ).process(
712+
ltrResult.css,
713+
{ from: undefined }
714+
);
715+
716+
// Write both versions
717+
await Promise.all( [
718+
writeFile(
719+
path.join( buildStyleDir, 'style.css' ),
720+
ltrResult.css
721+
),
722+
writeFile(
723+
path.join( buildStyleDir, 'style-rtl.css' ),
724+
rtlResult.css
725+
),
726+
] );
727+
728+
return '';
729+
},
730+
} ),
731+
],
732+
} );
733+
734+
return Date.now() - startTime;
735+
}
736+
633737
/**
634738
* Determine if a file is a source file in a v2 package.
635739
*

bin/packages/v2-packages.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const V2_PACKAGES = [
1313
'blob',
1414
'block-serialization-default-parser',
1515
'blocks',
16+
'components',
1617
'compose',
1718
'data',
1819
'data-controls',
@@ -25,6 +26,7 @@ const V2_PACKAGES = [
2526
'hooks',
2627
'html-entities',
2728
'i18n',
29+
'icons',
2830
'interactivity',
2931
'interactivity-router',
3032
'is-shallow-equal',
@@ -35,6 +37,7 @@ const V2_PACKAGES = [
3537
'media-utils',
3638
'notices',
3739
'preferences-persistence',
40+
'primitives',
3841
'priority-queue',
3942
'private-apis',
4043
'react-i18n',

0 commit comments

Comments
 (0)