Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
hasArgInCLI,
hasCssnanoConfig,
hasPostCSSConfig,
getWordPressSrcDirectory,
getWebpackEntryPoints,
getRenderPropPaths,
} = require( '../utils' );
Expand Down Expand Up @@ -233,7 +234,7 @@ const config = {
patterns: [
{
from: '**/block.json',
context: process.env.WP_SRC_DIRECTORY,
context: getWordPressSrcDirectory(),
noErrorOnMissing: true,
transform( content, absoluteFrom ) {
const convertExtension = ( path ) => {
Expand Down Expand Up @@ -267,7 +268,7 @@ const config = {
},
{
from: '**/*.php',
context: process.env.WP_SRC_DIRECTORY,
context: getWordPressSrcDirectory(),
noErrorOnMissing: true,
filter: ( filepath ) => {
return (
Expand Down
43 changes: 24 additions & 19 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ const getWebpackArgs = () => {
return webpackArgs;
};

/**
* Returns the WordPress source directory. It defaults to 'src' if the
* `process.env.WP_SRC_DIRECTORY` variable is not set.
*
* @return {string} The WordPress source directory.
*/
function getWordPressSrcDirectory() {
return process.env.WP_SRC_DIRECTORY || 'src';
}

/**
* Detects the list of entry points to use with webpack. There are three ways to do this:
* 1. Use the legacy webpack 4 format passed as CLI arguments.
Expand All @@ -185,10 +195,10 @@ function getWebpackEntryPoints() {
}

// Continue only if the source directory exists.
if ( ! hasProjectFile( process.env.WP_SRC_DIRECTORY ) ) {
if ( ! hasProjectFile( getWordPressSrcDirectory() ) ) {
log(
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.`
`Source directory "${ getWordPressSrcDirectory() }" was not found. Please confirm there is a "src" directory in the root or the value passed to --webpack-src-dir is correct.`
)
);
return {};
Expand All @@ -197,15 +207,15 @@ function getWebpackEntryPoints() {
// 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(
`${ process.env.WP_SRC_DIRECTORY }/**/block.json`,
`${ getWordPressSrcDirectory() }/**/block.json`,
{
absolute: true,
}
);

if ( blockMetadataFiles.length > 0 ) {
const srcDirectory = fromProjectRoot(
process.env.WP_SRC_DIRECTORY + sep
getWordPressSrcDirectory() + sep
);
const entryPoints = blockMetadataFiles.reduce(
( accumulator, blockMetadataFile ) => {
Expand All @@ -232,9 +242,7 @@ function getWebpackEntryPoints() {
) }" listed in "${ blockMetadataFile.replace(
fromProjectRoot( sep ),
''
) }". File is located outside of the "${
process.env.WP_SRC_DIRECTORY
}" directory.`
) }". File is located outside of the "${ getWordPressSrcDirectory() }" directory.`
)
);
return;
Expand All @@ -246,7 +254,7 @@ function getWebpackEntryPoints() {

// Detects the proper file extension used in the defined source directory.
const [ entryFilepath ] = glob(
`${ process.env.WP_SRC_DIRECTORY }/${ entryName }.[jt]s?(x)`,
`${ getWordPressSrcDirectory() }/${ entryName }.[jt]s?(x)`,
{
absolute: true,
}
Expand All @@ -261,9 +269,7 @@ function getWebpackEntryPoints() {
) }" listed in "${ blockMetadataFile.replace(
fromProjectRoot( sep ),
''
) }". File does not exist in the "${
process.env.WP_SRC_DIRECTORY
}" directory.`
) }". File does not exist in the "${ getWordPressSrcDirectory() }" directory.`
)
);
return;
Expand All @@ -283,15 +289,15 @@ function getWebpackEntryPoints() {
// 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(
`${ process.env.WP_SRC_DIRECTORY }/index.[jt]s?(x)`,
`${ getWordPressSrcDirectory() }/index.[jt]s?(x)`,
{
absolute: true,
}
);
if ( ! entryFile ) {
log(
chalk.yellow(
`No entry file discovered in the "${ process.env.WP_SRC_DIRECTORY }" directory.`
`No entry file discovered in the "${ getWordPressSrcDirectory() }" directory.`
)
);
return {};
Expand All @@ -309,19 +315,19 @@ function getWebpackEntryPoints() {
*/
function getRenderPropPaths() {
// Continue only if the source directory exists.
if ( ! hasProjectFile( process.env.WP_SRC_DIRECTORY ) ) {
if ( ! hasProjectFile( getWordPressSrcDirectory() ) ) {
return [];
}

// Checks whether any block metadata files can be detected in the defined source directory.
const blockMetadataFiles = glob(
`${ process.env.WP_SRC_DIRECTORY }/**/block.json`,
`${ getWordPressSrcDirectory() }/**/block.json`,
{
absolute: true,
}
);

const srcDirectory = fromProjectRoot( process.env.WP_SRC_DIRECTORY + sep );
const srcDirectory = fromProjectRoot( getWordPressSrcDirectory() + sep );

const renderPaths = blockMetadataFiles.map( ( blockMetadataFile ) => {
const { render } = JSON.parse( readFileSync( blockMetadataFile ) );
Expand All @@ -342,9 +348,7 @@ function getRenderPropPaths() {
) }" listed in "${ blockMetadataFile.replace(
fromProjectRoot( sep ),
''
) }". File is located outside of the "${
process.env.WP_SRC_DIRECTORY
}" directory.`
) }". File is located outside of the "${ getWordPressSrcDirectory() }" directory.`
)
);
return false;
Expand All @@ -360,6 +364,7 @@ function getRenderPropPaths() {
module.exports = {
getJestOverrideConfigFile,
getWebpackArgs,
getWordPressSrcDirectory,
getWebpackEntryPoints,
getRenderPropPaths,
hasBabelConfig,
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
const {
getJestOverrideConfigFile,
getWebpackArgs,
getWordPressSrcDirectory,
getWebpackEntryPoints,
getRenderPropPaths,
hasBabelConfig,
Expand All @@ -34,6 +35,7 @@ module.exports = {
getNodeArgsFromCLI,
getPackageProp,
getWebpackArgs,
getWordPressSrcDirectory,
getWebpackEntryPoints,
getRenderPropPaths,
hasArgInCLI,
Expand Down