Skip to content

Packages: Publish all modules as independent npm packages #3955

Closed

Description

@Shelob9

Issue Overview and Possible Solution

When creating a block, I start with something like this:

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks; 
const BlockControls = wp.blocks.BlockControls;
const el = wp.element.createElement;

Strong coupling to global state is consistent with traditional WordPress anti-patterns. But it presents the following problems:

  1. Gutenberg might not be active (3k active installs) or the site might not be on WordPress 5.0 yet (currently ~77% of WordPress sites are not on the current release series.)
  2. Relying on global state makes testing very hard and/or impossible depending on test type/ technology.
  3. I may wish to use these components in my plugin or other type of code that isn't strongly-coupled to WordPress and including them via npm and an import statement is how that should work.

Expected Behavior

$ npm i @wordpress/element
import { createElement } from '@wordpress/element';

Current Behavior

const { createElement } = wp.element;

Tasks

Activity

  1. youknowriad commented on Dec 12, 2017

    @youknowriad
    Contributor

    There is some work being done making Gutenberg modules available as npm packages. This work has started here https://github.com/WordPress/packages (discussions in the #core-js channel). But all modules should not be used as npm packages because:

    • Some of them require Server Side logic (injected scripts
    • This means each plugin will have its own version, while this acceptable for generic modules like "components" or "i18n" it might not be the case for modules like "blocks" where we need one and only one version (a single block registry)
  2. Shelob9 commented on Dec 14, 2017

    @Shelob9
    ContributorAuthor

    @youknowriad I'm glad to hear this is in progress.

    You said Some of them require Server Side logic (injected scripts which is a valid concern, but shouldn't blocks to be sufficiently decoupled from that server-side logic that we can inject our own for testing or non-traditional uses, for example Gutenberg for Drupal content editing :)

  3. youknowriad commented on Dec 14, 2017

    @youknowriad
    Contributor

    shouldn't blocks to be sufficiently decoupled from that server-side logic that we can inject our own for testing or non-traditional uses, for example Gutenberg for Drupal content editing :)

    That's a valid use-case that I care about as well, making Gutenberg (or the core of Gutenberg) a reusable piece. While I think it's important to follow this direction, it's not a priority for the first release.

  4. andreiglingeanu commented on Dec 22, 2017

    @andreiglingeanu
    Contributor

    One valid reason for this is i18n. I'd really love to be able to pull @wordpress/i18n package as a standalone thing. I'm building my own private solution for translations (with having a custom Babel plugin, too) and I'd love to not be having to maintain the wp.i18n lib by myself.

    Are you guys considering to get at least the small utilities published in NPM, for everyone in the WP community to be able to use & benefit from them?

  5. youknowriad commented on Dec 22, 2017

    @youknowriad
    Contributor

    re you guys considering to get at least the small utilities published in NPM, for everyone in the WP community to be able to use & benefit from them?

    Yes, we are. Expect more in the future but there are some utilities available right now. Others like components, i18n, etc. will follow https://www.npmjs.com/org/wordpress

  6. andreiglingeanu commented on Dec 22, 2017

    @andreiglingeanu
    Contributor

    One more question is, should I count on the technique of putting the lib globally under wp namespace (wp.i18n, wp.blocks, wp.element) as a viable one? Will that be supported with arrival of Gutenberg into the WP core?

    If so, I'd like to be able to remove those dependencies from my bundles in the future and just use the globally available ones.

    wp.i18n and wp.element appeals to me a lot. This will allow me to build UIs using React without having to ship the lib itself with the plugin.

  7. youknowriad commented on Dec 22, 2017

    @youknowriad
    Contributor

    If so, I'd like to be able to remove those dependencies from my bundles in the future and just use the globally available ones.

    Yes, I think so. I think we should think about this module by module because making it available in the global means maintaining backwards compatibility for this global. For example the components module, I'm not sure we'd want to make it global in the future.

  8. changed the title [-][Enhancement] Move components, blocks and i18n to npm package(s)[/-] [+]Move components, blocks and i18n to npm package(s)[/+] on Jan 23, 2018
  9. added
    FrameworkIssues related to broader framework topics, especially as it relates to javascript
    on Jan 23, 2018
  10. gziolo commented on Feb 1, 2018

    @gziolo
    Member

    We probably will integrate Lerna into this repository and will publish i18n, block, components, element as packages from here. We would use next tag to inform that they are experimental and bump version every time we do a release for a plugin. Is someone interested to work on this?

  11. added a commit that references this issue on May 6, 2018
  12. 15 remaining items

  13. nerrad commented on Jun 5, 2018

    @nerrad
    Contributor

    ya looks like @wordpress/dom/build/index.js has an import statement when it shouldn't?

  14. nerrad commented on Jun 5, 2018

    @nerrad
    Contributor

    It looks like it may be a babel-transpile bug related to this in dom/src/index.js:

    export * from './dom';
    
  15. gziolo commented on Jun 5, 2018

    @gziolo
    Member

    ya looks like @wordpress/dom/build/index.js has an import statement when it shouldn't?

    Haha, I see it. It is definitely a bug in Babel. There are two things we still need to attack:

    • Fix this bug, maybe try Babel 7.x which is still beta.
    • Decide what to do about babel-runtime, set it as a dependency would be an easy one, but I'm not sure if this is a common practice in such case. On the other hand, inlining those Babel runtime files would introduce lots of code duplication between packages.

    Any thoughts or ideas how to improve this output? For the Gutenberg itself, we should ideally extract all those babel-runtime imports and server as one module. At the moment we have tons of code duplications. Any thoughts?

  16. nerrad commented on Jun 5, 2018

    @nerrad
    Contributor

    Decide what to do about babel-runtime, set it as a dependency would be an easy one, but I'm not sure if this is a common practice in such case.

    It seems that is the expectation according to the [https://babeljs.io/docs/plugins/transform-runtime/] docs where they say, "Make sure you include babel-runtime as a dependency".

    On the other hand, inlining those Babel runtime files would introduce lots of code duplication between packages.

    I'm not sure I follow, isn't it still necessary to have babel-runtime inlined across the packages (since they could be consumed separately from each other)?

  17. youknowriad commented on Jun 5, 2018

    @youknowriad
    Contributor

    @gziolo The polyfill issue seems like a generic issue that any npm package could face. Curious how popular packages are solving it?

    From what I understand:

    • libraries try to avoid the usage of the features requiring polyfills as much as they can.
    • I they do so, they ask people to include the polyfills in their bundles (see how redux-saga requires the generator runtime in the readme https://github.com/redux-saga/redux-saga)

    Maybe on approach could be to do that as well.
    Another approach would be to avoid thinking too much about those for each package and aks to require babel-polyfill or something globally? In general we try to avoid loading the babel-polyfill globally like that but I wonder if it's not better to just do it as a separate script instead of duplicating part of it in several modules?

    Or

    Try the next version of babel-preset-env https://github.com/babel/babel-preset-env/tree/2.0#usebuiltins-usage and specifically the useBuiltIns: 'usage' option

  18. added
    [Status] BlockedUsed to indicate that a current effort isn't able to move forward
    and removed on Jun 7, 2018
  19. gziolo commented on Jun 7, 2018

    @gziolo
    Member

    Migration to Babel 7 is in progress, but we need to fix some configuration issues and fix failing tests. Marking this one as blocked until WordPress/packages#136 gets resolved and Gutenberg upgraded to work with Babel 7.

  20. Shelob9 commented on Jun 8, 2018

    @Shelob9
    ContributorAuthor

    I'm testing with

    import {registerStore} from "@wordpress/data";
    
        describe( 'wp.data import', () => {
            it( 'registerStore is available',  () => {
                expect(typeof registerStore ).toEqual('function');
            });
    
            it( 'registerStore works',  () => {
                const initalState = {
                    hi: 'Roy'
                };
                expect( registerStore( 'testStore', {
                    reducer( state = initalState, action ) {
                        //reduce nothing
                        return state;
                    },
                }).getState() ).toEqual(initalState);
            });
    
    
        })
    });
    

    Both tests are passing :)

  21. gziolo commented on Jun 8, 2018

    @gziolo
    Member

    I'm moving plugins module to packages and I noticed the same issue like in dom module:

    export * from './components';
    export * from './api';

    transpiles to commonjs with the unexpected:

    import _Object$defineProperty from 'babel-runtime/core-js/object/define-property';
    import _Object$keys from 'babel-runtime/core-js/object/keys';
  22. gziolo commented on Jul 10, 2018

    @gziolo
    Member

    #7832 upgrades Babel to v7 which will resolve all the issues we have with the transpiled code 🎉

  23. removed
    [Status] BlockedUsed to indicate that a current effort isn't able to move forward
    on Jul 16, 2018
  24. gziolo commented on Aug 13, 2018

    @gziolo
    Member

    I published last of the planned packages with e1811b2

    All packages can be downloaded from npm using the stable version. Let's open a follow up issues when they are discovered and iterate on the current setup independently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

FrameworkIssues related to broader framework topics, especially as it relates to javascript[Feature] ExtensibilityThe ability to extend blocks or the editing experience[Type] EnhancementA suggestion for improvement.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions