Repository navigation
Description
Previously: #6584 (comment)
The intention with consuming from the WordPress npm organization packages is that each module would have itself exposed as an equivalent camel-case form as a property on window.wp. This already works well for most packages, allowing developers to use e.g. wp.i18n.__ (the __ named export from @wordpress/i18n).
However, for packages which have a default export, the usage is not as expected:
gutenberg/lib/client-assets.php
Line 1032 in 73f2331
| wp.domReady.default( function() { |
Note the .default, which ideally should be omitted, calling instead as wp.domReady( fn ).
See #6584 (comment) for additional context and ideas for solution:
I dug into this a bit, and it's trickier than it appears on the surface.
This is what Webpack's
output.libraryExportconfiguration is meant to support: Setting a specific property as the value assigned on the library target (window).However, we have mixed needs: For something like
wp.editororwp.i18n, we want named exports to be respected, and assigned as nested properties within. Fordom-readythough, the default export function should be assigned aswp.domReady.In my head, it's as though we want
libraryExport: 'default'when there are no other named exports, otherwise using the default_entry_return_.So far as implementation, we might need to get our hands dirty with a custom Webpack plugin to achieve this. It probably needs to extend (whether via classical inheritance or just dynamically instantiating during
apply, I'm uncertain) Webpack'sExportPropertyMainTemplatePlugin.js. Where I started having trouble was determining the exports of a module to decide what to assign aslibraryExport.
Alternative: have separate
outputconfigurations for specific edge-case modules likedom-ready, where we can assignlibraryExport: 'default'. This too is not made very simple in an all-in-one Webpack configuration like we have.Alternative alternative: Don't allow
defaultexports on packages.Alternative alternative alternative: Use CommonJS
module.exportsinstead ofexport defaultfrom thepackagesimplementation.
Alternative Alternative Alternative, assign the globals manually in "enqueues" importing things from the regular built modules :)
Activity