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

Packages: Publish all modules as independent npm packages #3955

Closed
19 tasks done
Shelob9 opened this issue Dec 12, 2017 · 29 comments
Closed
19 tasks done

Packages: Publish all modules as independent npm packages #3955

Shelob9 opened this issue Dec 12, 2017 · 29 comments
Assignees
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Enhancement A suggestion for improvement.

Comments

@Shelob9
Copy link
Contributor

Shelob9 commented Dec 12, 2017

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

@youknowriad
Copy link
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)

@Shelob9
Copy link
Contributor Author

Shelob9 commented Dec 14, 2017

@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 :)

@Shelob9 Shelob9 closed this as completed Dec 14, 2017
@Shelob9 Shelob9 reopened this Dec 14, 2017
@youknowriad
Copy link
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.

@andreiglingeanu
Copy link
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?

@youknowriad
Copy link
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

@andreiglingeanu
Copy link
Contributor

andreiglingeanu commented Dec 22, 2017

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.

@youknowriad
Copy link
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.

@danielbachhuber danielbachhuber changed the title [Enhancement] Move components, blocks and i18n to npm package(s) Move components, blocks and i18n to npm package(s) Jan 23, 2018
@danielbachhuber danielbachhuber added [Type] Enhancement A suggestion for improvement. Framework Issues related to broader framework topics, especially as it relates to javascript labels Jan 23, 2018
@gziolo
Copy link
Member

gziolo commented Feb 1, 2018

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?

@gziolo gziolo added the [Feature] Extensibility The ability to extend blocks or the editing experience label Feb 1, 2018
Shelob9 pushed a commit to CalderaWP/gutenberg-modules that referenced this issue May 6, 2018
@Shelob9
Copy link
Contributor Author

Shelob9 commented May 6, 2018

I made a PHP app that copies all of Gutenberg's built JS/CSS files and publishes them as an npm package

Not sure if this approach makes sense yet or not, but if I can figure out how to get it work with webpack, I can use Travis to run the app's build command every-time a new tag is added to Gutenberg and then publish the same tag to npm.

@gziolo
Copy link
Member

gziolo commented May 8, 2018

We should publish all Gutenberg modules (except edit-post) as individual packages to npm later this month. I’ll be working on it starting next week the latest. I’m not sure if using built files is the way to go in general but might be beneficial in your case. My thinking is that we should publish sources as they are after each new version is tagged.

@gziolo gziolo added the [Status] In Progress Tracking issues with work in progress label May 14, 2018
@gziolo gziolo self-assigned this May 14, 2018
@c-shultz
Copy link

c-shultz commented May 14, 2018

We should publish all Gutenberg modules (except edit-post) as individual packages to npm later this month.

That would be great! That would definitely solve the dependency issues I was running into with unit testing a Jetpack module that depend on Gutenberg's data API.

@Shelob9
Copy link
Contributor Author

Shelob9 commented Jun 8, 2018

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 :)

@gziolo
Copy link
Member

gziolo commented Jun 8, 2018

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';

@gziolo
Copy link
Member

gziolo commented Jul 10, 2018

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

@gziolo
Copy link
Member

gziolo commented Aug 13, 2018

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
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

7 participants