Skip to content

Build Tooling: Packages default export should not be assigned as property #6748

Closed

Description

@aduth

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:

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.libraryExport configuration 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.editor or wp.i18n, we want named exports to be respected, and assigned as nested properties within. For dom-ready though, the default export function should be assigned as wp.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's ExportPropertyMainTemplatePlugin.js. Where I started having trouble was determining the exports of a module to decide what to assign as libraryExport.

Alternative: have separate output configurations for specific edge-case modules like dom-ready, where we can assign libraryExport: 'default'. This too is not made very simple in an all-in-one Webpack configuration like we have.

Alternative alternative: Don't allow default exports on packages.

Alternative alternative alternative: Use CommonJS module.exports instead of export default from the packages implementation.

Alternative Alternative Alternative, assign the globals manually in "enqueues" importing things from the regular built modules :)

Activity

  1. added
    [Type] TaskIssues or PRs that have been broken down into an individual action to take
    on May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Type] Build ToolingIssues or PRs related to build tooling[Type] TaskIssues or PRs that have been broken down into an individual action to takenpm PackagesRelated to npm packages

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions