Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts: Improve the handling for build entry points #38584

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
const { basename, dirname, join } = require( 'path' );
const chalk = require( 'chalk' );
const { basename, dirname, extname, join, sep } = require( 'path' );
const { sync: glob } = require( 'fast-glob' );

/**
Expand All @@ -15,6 +16,8 @@ const {
} = require( './cli' );
const { fromConfigRoot, fromProjectRoot, hasProjectFile } = require( './file' );
const { hasPackageProp } = require( './package' );
const { exit } = require( './process' );
const { log } = console;

// See https://babeljs.io/docs/en/config-files#configuration-file-types.
const hasBabelConfig = () =>
Expand Down Expand Up @@ -188,6 +191,7 @@ function getWebpackEntryPoints() {
} );

if ( blockMetadataFiles.length > 0 ) {
const srcDirectory = fromProjectRoot( 'src' + sep );
const entryPoints = blockMetadataFiles.reduce(
( accumulator, blockMetadataFile ) => {
const {
Expand All @@ -203,15 +207,26 @@ function getWebpackEntryPoints() {
const filepath = join(
dirname( blockMetadataFile ),
value.replace( 'file:', '' )
).replace( /\\/g, '/' );
);

// Takes the path without the file extension, and relative to the `src` directory.
const [ , entryName ] = filepath
.split( '.' )[ 0 ]
.split( 'src/' );
if ( ! entryName ) {
if ( ! filepath.startsWith( srcDirectory ) ) {
log(
chalk.yellow(
`Skipping "${ value.replace(
'file:',
''
) }" listed in "${ blockMetadataFile.replace(
fromProjectRoot( sep ),
''
) }". File is located outside of the "src" directory.`
)
);
return;
}
const entryName = filepath
.replace( extname( filepath ), '' )
.replace( srcDirectory, '' );

// Detects the proper file extension used in the `src` directory.
const [ entryFilepath ] = glob(
Expand All @@ -221,6 +236,20 @@ function getWebpackEntryPoints() {
}
);

if ( ! entryFilepath ) {
log(
chalk.yellow(
`Skipping "${ value.replace(
'file:',
''
) }" listed in "${ blockMetadataFile.replace(
fromProjectRoot( sep ),
''
) }". File does not exist in the "src" directory.`
)
);
return;
}
accumulator[ entryName ] = entryFilepath;
} );
return accumulator;
Expand All @@ -239,7 +268,10 @@ function getWebpackEntryPoints() {
absolute: true,
} );
if ( ! entryFile ) {
return {};
log(
chalk.bold.red( 'No entry files discovered in the "src" folder.' )
);
exit( 1 );
}

return {
Expand Down