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
20 changes: 17 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,21 @@ let target = 'browserslist';
if ( ! browserslist.findConfig( '.' ) ) {
target += ':' + fromConfigRoot( '.browserslistrc' );
}
let entry = {};
if ( process.env.WP_ENTRY ) {
entry = JSON.parse( process.env.WP_ENTRY );
} else {
[ '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 +104,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
47 changes: 29 additions & 18 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,47 @@ const getWebpackArgs = () => {
hasArgInCLI( '-o' ) || hasArgInCLI( '--output' );
if ( hasFileArgInCLI() && ! hasWebpackOutputOption ) {
/**
* 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