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

@wordpress/scripts: Support path-based chunk names in FixStyleWebpackPlugin #32834

Conversation

bosconian-dynamics
Copy link
Contributor

@bosconian-dynamics bosconian-dynamics commented Jun 19, 2021

Closes #32791.

Description

Changes FixStyleWebpackPlugin's chunk name to styleFile name resolution to account for chunk names which might contain forward slashes. This gives users overriding webpack.config.js the option of using path-based entry names by setting optimization.splitChunks.cacheGroups.style.name.

It's possible that the optimization.splitChunks.cacheGroups.style.name function could be set in @wordpress/scripts' webpack.config.js file as well, but I am not familiar enough with splitChunks or the style cacheGroup that I am comfortable asserting that my simple implementation (see the test configuration in the next section) will cover any range of edge-cases.

How has this been tested?

  • npm run lint-js
  • Created, built, and verified functional bundles in a WordPress plugin overriding @wordpress/scripts' webpack.config.js as follows:
const path = require( 'path' );

const config = require( '@wordpress/scripts/config/webpack.config.js' );

config.entry = {
 'blocks/foo/index': path.resolve( process.cwd(), 'src', 'blocks', 'foo', 'index.js' ),
 'frontend/accordion': path.resolve( process.cwd(), 'src', 'frontend', 'accordion.js' ),
};

config.optimization.splitChunks.cacheGroups.style.name = ( module, chunks, group_key ) => {
 const delimeter = config.optimization.splitChunks.cacheGroups.style.automaticNameDelimiter;
   
 return chunks[0].name.replace(
   /(\/?)([^/]+?)$/,
   `$1${group_key}${delimeter}$2`
 );
};

module.exports = config;

Screenshots

image

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

…th-based chunk names

Changes FixStyleWebpackPlugin's chunk name to file name resolution to
account for entrypoints containing forward slashes in their names,
resulting in the related `style-{chunk name}.css` files being output
within the path described by the chunk name rather than rooted to the
output directory.

Closes WordPress#32791.
…ix/wp-scripts-fixstylewebpackplugin-path-based-entries
@gziolo
Copy link
Member

gziolo commented Jun 22, 2021

@bosconian-dynamics - thank you for the fix 👍🏻

It seems to work well in my testing for the use case you shared. It shouldn't have any impact on the default behavior.

We plan to migrate to webpack 5 in the upcoming week. wepack 5 no longer needs FixStyleWebpackPlugin plugin at all.

@gziolo gziolo added [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended labels Jun 22, 2021
@gziolo gziolo merged commit df10a6d into WordPress:trunk Jun 22, 2021
@github-actions github-actions bot added this to the Gutenberg 11.0 milestone Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@wordpress/scripts: path-based webpack entry points produce inconsist output file structure
2 participants