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: Add new flag to allow customization of the src directory. #39618

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased

- Add `--webpack-src-dir` CLI flag to allow customizing the source code directory. (`src` by default) ([#39618](https://github.com/WordPress/gutenberg/pull/39618)).
gziolo marked this conversation as resolved.
Show resolved Hide resolved
## 22.2.0 (2022-03-11)

### Enhancement
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ const config = {
patterns: [
{
from: copyWebPackPattens,
context: 'src',
context: process.env.WP_SRC_DIRECTORY,
noErrorOnMissing: true,
},
],
Expand Down
6 changes: 5 additions & 1 deletion packages/scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { sync: resolveBin } = require( 'resolve-bin' );
/**
* Internal dependencies
*/
const { getWebpackArgs, hasArgInCLI } = require( '../utils' );
const { getWebpackArgs, hasArgInCLI, getArgFromCLI } = require( '../utils' );

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

Expand All @@ -23,6 +23,10 @@ if ( hasArgInCLI( '--webpack-copy-php' ) ) {
process.env.WP_COPY_PHP_FILES_TO_DIST = true;
}

process.env.WP_SRC_DIRECTORY = hasArgInCLI( '--webpack-src-dir' )
? getArgFromCLI( '--webpack-src-dir' )
: 'src';

const { status } = spawn( resolveBin( 'webpack' ), getWebpackArgs(), {
stdio: 'inherit',
} );
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ if ( hasArgInCLI( '--webpack-copy-php' ) ) {
process.env.WP_COPY_PHP_FILES_TO_DIST = true;
}

process.env.WP_SRC_DIRECTORY = hasArgInCLI( '--webpack-src-dir' )
? getArgFromCLI( '--webpack-src-dir' )
: 'src';

const { status } = spawn(
resolveBin( 'webpack' ),
[ hasArgInCLI( '--hot' ) ? 'serve' : 'watch', ...getWebpackArgs() ],
Expand Down
53 changes: 36 additions & 17 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,29 @@ 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(
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

If a function is passed, then it will be invoked on every make event.

Note that the making event triggers when webpack starts and for every invalidation when watching for file changes.

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.

Copy link
Member

Choose a reason for hiding this comment

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

Related comment I mentioned: #38739 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

@gziolo @ryanwelcher Hope this PR resolves the confusing behaviour for #38739 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

I added commit 37ec581 to see if that resolves the issue.

Copy link
Member

Choose a reason for hiding this comment

The 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 block.json changes.

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.`
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved
)
);
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 {
Expand All @@ -213,7 +223,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(
Expand All @@ -223,7 +233,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;
Expand All @@ -233,9 +245,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,
}
Expand All @@ -250,7 +262,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;
Expand All @@ -267,14 +281,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 {};
}
Expand Down