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: Convert legacy entry point arguments for compatibility with webpack 5 #34264

Merged
merged 9 commits into from
Sep 9, 2021
1 change: 1 addition & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- Bring back support for SVG files in CSS ([#34394](https://github.com/WordPress/gutenberg/pull/34394)). It wasn't correctly migrated when integrating webpack v5.
- Convert legacy entry point arguments supported in webpack 4 for compatibility with webpack 5 ([#34264](https://github.com/WordPress/gutenberg/pull/34264)).

## 18.0.0 (2021-08-23)

Expand Down
22 changes: 19 additions & 3 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const { CleanWebpackPlugin } = require( 'clean-webpack-plugin' );
const browserslist = require( 'browserslist' );
const fs = require( 'fs' );
const path = require( 'path' );

/**
Expand All @@ -31,6 +32,23 @@ let target = 'browserslist';
if ( ! browserslist.findConfig( '.' ) ) {
target += ':' + fromConfigRoot( '.browserslistrc' );
}
let entry = {};
if ( process.env.WP_ENTRY ) {
entry = JSON.parse( process.env.WP_ENTRY );
} else {
// By default the script checks if `src/index.js` exists and sets it as an entry point.
// In the future we should add similar handling for `src/script.js` and `src/view.js`.
[ 'index' ].forEach( ( entryName ) => {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
const filepath = path.resolve(
process.cwd(),
'src',
`${ entryName }.js`
);
if ( fs.existsSync( filepath ) ) {
entry[ entryName ] = filepath;
}
} );
}

const cssLoaders = [
{
Expand Down Expand Up @@ -88,9 +106,7 @@ const getLiveReloadPort = ( inputPort ) => {
const config = {
mode,
target,
entry: {
index: path.resolve( process.cwd(), 'src', 'index.js' ),
},
entry,
output: {
filename: '[name].js',
path: path.resolve( process.cwd(), 'build' ),
Expand Down
53 changes: 34 additions & 19 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,38 +108,53 @@ const getWebpackArgs = () => {

const hasWebpackOutputOption =
hasArgInCLI( '-o' ) || hasArgInCLI( '--output' );
if ( hasFileArgInCLI() && ! hasWebpackOutputOption ) {
if (
! hasWebpackOutputOption &&
! hasArgInCLI( '--entry' ) &&
hasFileArgInCLI()
) {
/**
* Converts a path to the entry format supported by webpack, e.g.:
* `./entry-one.js` -> `entry-one=./entry-one.js`
* `entry-two.js` -> `entry-two=./entry-two.js`
* Converts a legacy path to the entry pair supported by webpack, e.g.:
* `./entry-one.js` -> `[ 'entry-one', './entry-one.js] ]`
* `entry-two.js` -> `[ 'entry-two', './entry-two.js' ]`
*
* @param {string} path The path provided.
*
* @return {string} The entry format supported by webpack.
* @return {string[]} The entry pair of its name and the file path.
*/
const pathToEntry = ( path ) => {
const entry = basename( path, '.js' );
const entryName = basename( path, '.js' );

if ( ! path.startsWith( './' ) ) {
path = './' + path;
}

return [ entry, path ].join( '=' );
return [ entryName, path ];
};

// The following handles the support for multiple entry points in webpack, e.g.:
// `wp-scripts build one.js custom=./two.js` -> `webpack one=./one.js custom=./two.js`
webpackArgs = webpackArgs.map( ( cliArg ) => {
if (
getFileArgsFromCLI().includes( cliArg ) &&
! cliArg.includes( '=' )
) {
return pathToEntry( cliArg );
}

return cliArg;
} );
const fileArgs = getFileArgsFromCLI();
if ( fileArgs.length > 0 ) {
// Filters out all CLI arguments that are recognized as file paths.
const fileArgsToRemove = new Set( fileArgs );
webpackArgs = webpackArgs.filter( ( cliArg ) => {
if ( fileArgsToRemove.has( cliArg ) ) {
fileArgsToRemove.delete( cliArg );
return false;
}
return true;
} );

// Converts all CLI arguments that are file paths to the `entry` format supported by webpack.
// It is going to be consumed in the config through the WP_ENTRY global variable.
const entry = {};
fileArgs.forEach( ( fileArg ) => {
const [ entryName, path ] = fileArg.includes( '=' )
? fileArg.split( '=' )
: pathToEntry( fileArg );
entry[ entryName ] = path;
} );
process.env.WP_ENTRY = JSON.stringify( entry );
}
}

if ( ! hasWebpackConfig() ) {
Expand Down