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: Add new flag to allow customization of the src
directory.
#39618
Changes from 14 commits
c8a9c01
f334906
6465545
6438856
6f24eb8
37ec581
c5a2b68
4e2af84
19b81b0
c980cd2
0f50fcf
0b691cc
1ed44fc
727ba85
188f23f
6d6b943
c7075da
04b0606
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* External dependencies | ||
*/ | ||
const chalk = require( 'chalk' ); | ||
const { readFileSync } = require( 'fs' ); | ||
const { basename, dirname, extname, join, sep } = require( 'path' ); | ||
const { sync: glob } = require( 'fast-glob' ); | ||
|
||
|
@@ -183,26 +184,38 @@ function getWebpackEntryPoints() { | |
return JSON.parse( process.env.WP_ENTRY ); | ||
} | ||
|
||
// Continue only if the `src` directory exists. | ||
if ( ! hasProjectFile( 'src' ) ) { | ||
// Continue only if the source directory exists. | ||
if ( ! hasProjectFile( process.env.WP_SRC_DIRECTORY ) ) { | ||
log( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember some folks reporting a similar message on the terminal was confusing in case they using a custom config that overrides the entry points. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It made me think that we could use a dynamic entry as documented in https://webpack.js.org/configuration/entry-context/#dynamic-entry. We would never run this code when the project overrides entry points.
In fact, this would solve a more pressing issue we recently discovered with @schmitzoide when working on a plugin with multiple blocks. Currently, the logic for finding entry points runs only once in the watch mode. Therefore you need to restart the development build whenever new blocks get introduced or scripts for the block change. Using a callback function should resolve the issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related comment I mentioned: #38739 (comment). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gziolo @ryanwelcher Hope this PR resolves the confusing behaviour for #38739 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added commit 37ec581 to see if that resolves the issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can confirm that the message isn't shown when we use the default handling for entry points. I can also confirm that entry points get dynamically updated in the watch mode when |
||
chalk.yellow( | ||
`Source directory "${ process.env.WP_SRC_DIRECTORY }" was not found. Please confirm there is a "src" directory in the root or the value passed to --webpack-src-dir is correct.` | ||
) | ||
); | ||
return {}; | ||
} | ||
|
||
// 2. Checks whether any block metadata files can be detected in the `src` directory. | ||
// 2. Checks whether any block metadata files can be detected in the defined source directory. | ||
// It scans all discovered files looking for JavaScript assets and converts them to entry points. | ||
const blockMetadataFiles = glob( 'src/**/block.json', { | ||
absolute: true, | ||
} ); | ||
const blockMetadataFiles = glob( | ||
`${ process.env.WP_SRC_DIRECTORY }/**/block.json`, | ||
{ | ||
absolute: true, | ||
} | ||
); | ||
|
||
if ( blockMetadataFiles.length > 0 ) { | ||
const srcDirectory = fromProjectRoot( 'src' + sep ); | ||
const srcDirectory = fromProjectRoot( | ||
process.env.WP_SRC_DIRECTORY + sep | ||
); | ||
const entryPoints = blockMetadataFiles.reduce( | ||
( accumulator, blockMetadataFile ) => { | ||
const { | ||
editorScript, | ||
script, | ||
viewScript, | ||
} = require( blockMetadataFile ); | ||
} = JSON.parse( | ||
readFileSync( blockMetadataFile ) | ||
); | ||
[ editorScript, script, viewScript ] | ||
.flat() | ||
.filter( ( value ) => value && value.startsWith( 'file:' ) ) | ||
|
@@ -213,7 +226,7 @@ function getWebpackEntryPoints() { | |
value.replace( 'file:', '' ) | ||
); | ||
|
||
// Takes the path without the file extension, and relative to the `src` directory. | ||
// Takes the path without the file extension, and relative to the defined source directory. | ||
if ( ! filepath.startsWith( srcDirectory ) ) { | ||
log( | ||
chalk.yellow( | ||
|
@@ -223,7 +236,9 @@ function getWebpackEntryPoints() { | |
) }" listed in "${ blockMetadataFile.replace( | ||
fromProjectRoot( sep ), | ||
'' | ||
) }". File is located outside of the "src" directory.` | ||
) }". File is located outside of the "${ | ||
process.env.WP_SRC_DIRECTORY | ||
}" directory.` | ||
) | ||
); | ||
return; | ||
|
@@ -233,9 +248,9 @@ function getWebpackEntryPoints() { | |
.replace( srcDirectory, '' ) | ||
.replace( /\\/g, '/' ); | ||
|
||
// Detects the proper file extension used in the `src` directory. | ||
// Detects the proper file extension used in the defined source directory. | ||
const [ entryFilepath ] = glob( | ||
`src/${ entryName }.[jt]s?(x)`, | ||
`${ process.env.WP_SRC_DIRECTORY }/${ entryName }.[jt]s?(x)`, | ||
{ | ||
absolute: true, | ||
} | ||
|
@@ -250,7 +265,9 @@ function getWebpackEntryPoints() { | |
) }" listed in "${ blockMetadataFile.replace( | ||
fromProjectRoot( sep ), | ||
'' | ||
) }". File does not exist in the "src" directory.` | ||
) }". File does not exist in the "${ | ||
process.env.WP_SRC_DIRECTORY | ||
}" directory.` | ||
) | ||
); | ||
return; | ||
|
@@ -267,14 +284,19 @@ function getWebpackEntryPoints() { | |
} | ||
} | ||
|
||
// 3. Checks whether a standard file name can be detected in the `src` directory, | ||
// 3. Checks whether a standard file name can be detected in the defined source directory, | ||
// and converts the discovered file to entry point. | ||
const [ entryFile ] = glob( 'src/index.[jt]s?(x)', { | ||
absolute: true, | ||
} ); | ||
const [ entryFile ] = glob( | ||
`${ process.env.WP_SRC_DIRECTORY }/index.[jt]s?(x)`, | ||
{ | ||
absolute: true, | ||
} | ||
); | ||
if ( ! entryFile ) { | ||
log( | ||
chalk.yellow( 'No entry file discovered in the "src" directory.' ) | ||
chalk.yellow( | ||
`No entry file discovered in the "${ process.env.WP_SRC_DIRECTORY }" directory.` | ||
) | ||
); | ||
return {}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIt: it's webpack not WebPack 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦 Nice catch - I've updated the reference.