Skip to content

Commit

Permalink
Scripts: Improve the handling for build entry points (#38584)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo authored Feb 8, 2022
1 parent 4a4e32d commit 29d123b
Showing 1 changed file with 39 additions and 7 deletions.
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

3 comments on commit 29d123b

@loxK
Copy link
Contributor

@loxK loxK commented on 29d123b Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gziolo We @wordpress/scripts this for our theme plugins together with gulp. This commit broke it and isn't mentioned ion the changelog.
Any idea how to fix it ? It looks to prevent using the webpack.config.js and setting a custom entry file.

import config from "../../config.js"

/** Gulp and utils **/
import gulp from 'gulp'

const {src, dest} = gulp

import named from 'vinyl-named'

/** Webpack **/
import webpack from 'webpack-stream'
import webpackConfig from "@wordpress/scripts/config/webpack.config.js"
webpackConfig.entry = null

/**
 * Scripts
 */
export const process_js = () => src( config.paths.scripts_sources.src )
        .pipe( named() )
        .pipe( webpack( webpackConfig ) )
        .pipe( dest( config.paths.scripts_sources.dst ) )

Error

No entry files discovered in the "src" folder.

@gziolo
Copy link
Member Author

@gziolo gziolo commented on 29d123b Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit broke it and isn't mentioned ion the changelog.

Sorry about both. Errors happen, but I definitely should include an entry in the changelog.

@ocean90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the same, created #38739 so we can improve this.

Please sign in to comment.