2

The WordPress Gutenberg block build script uses wordpress/dependency-extraction-webpack-plugin to build the blocks without WordPress dependencies being repeated but what about other dependencies?

If I build a plugin with 10 blocks and they all re-use dependencies, the WordPress build script will embed the dependency in every block bundle.

Is there a good way to handle this without having to re-create webpack build by myself?

2
  • Can you provide an example of the dependencies you're using? If they are not part of the ones that the plugin supports, you may need to add a custom webpack config that extends the one provided by @wordpress/scripts
    – Welcher
    Commented Oct 6, 2020 at 14:30
  • 2
    "without having to re-create webpack build" - if I understand it correctly, no, you don't have to. You could put all your blocks in a single directory and use a single webpack config. As with the code duplication issue, webpack has a guide you can try.
    – Sally CJ
    Commented Oct 7, 2020 at 11:09

1 Answer 1

2
+50

WordPress Dependency extraction plugin @wordpress/dependency-extraction-webpack-plugin basically makes your ES6 dependency imports use WordPress scripts instead of adding them to the bundle over and over.

This makes modules registered and enqueued in WordPress (including but not limited to jquery, moment and react and wp.* modules) to be properly excluded from builds.

You can add additional dependencies to be excluded (make sure you register/enqueue in WordPress) with requestToHandle callback.

For example for excluding say react-sortable component from build registered as react-sortable script in WordPress (PHP),

module.exports = {
  plugins: [
    new DependencyExtractionWebpackPlugin( {
      requestToHandle: function ( module ) {
        if ( module === 'react-sortable' ) {
          return 'react-sortable'; // WordPress script handle
        }
      }
    } ),
  ]
}
3
  • Where do you add this? I am using wp-scripts build to build. The webpack config is hidden.
    – Guerrilla
    Commented Nov 13, 2020 at 3:34
  • In your webpack config file.
    – shramee
    Commented Nov 26, 2020 at 18:56
  • when using wp-scripts build the webpack config file is not accessible. I guess it not possible with that way and have to build custom
    – Guerrilla
    Commented Nov 27, 2020 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.