Repository navigation
Closed
Labels
FrameworkIssues related to broader framework topics, especially as it relates to javascriptIssues related to broader framework topics, especially as it relates to javascript[Feature] ExtensibilityThe ability to extend blocks or the editing experienceThe ability to extend blocks or the editing experience[Type] EnhancementA suggestion for improvement.A suggestion for improvement.
Description
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:
- 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.)
- Relying on global state makes testing very hard and/or impossible depending on test type/ technology.
- 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/elementimport { createElement } from '@wordpress/element';Current Behavior
const { createElement } = wp.element;Tasks
-
api-request(Add a new@wordpress/api-requestpackage #7018) -
blob(Blob: Extract newblobpackage out of utils module #6973) - extracted fromutils -
blocks- depends onandwp.shortcodewindow._wpBlocks(Packages: Add the blocks module to the packages folder #8046) -
renamed tocore-blocksblock-library(Extract Core Blocks to the npm packages folder #8287) -
core-data(Move the Core Data Module to the reusable packages #7222) -
components(Packages: Move the components module partially to the packages folder #7640) -
compose(Packages: Add a new compose package #7948) -
editor(Packages: Extract the editor package #8081) -
- we don't plan to publish it, this might changeedit-post -
data(Packages: Move data module to the package maintained by Lerna #6828) -
date(Build: Move date module to packages maintained by Lerna #6658) -
deprecated(Deprecated: Introduce new module with depreaction util #6914) - extracted fromutils -
dom(Packages: Extract dom package and make it maintained with Lerna #6758) - extracted fromutils -
element(Packages: Move element to packages maintained by Lerna #6756) -
library-export-default-webpack-plugin(Build: Add new Webpack plugin to handle library default export #6935) -
nux(Packages: Extract@wordpress/nuxpackage #8016) -
plugins(Packages: Move Plugins module to packages #7235) -
- to be extracted into smaller packages (Editor: Move media upload from utils #7978)utils -
viewport- depends oncomponents(Packages: Extract viewport package #7975)
Metadata
Metadata
Assignees
Labels
FrameworkIssues related to broader framework topics, especially as it relates to javascriptIssues related to broader framework topics, especially as it relates to javascript[Feature] ExtensibilityThe ability to extend blocks or the editing experienceThe ability to extend blocks or the editing experience[Type] EnhancementA suggestion for improvement.A suggestion for improvement.
Activity
youknowriad commentedon Dec 12, 2017
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:
Shelob9 commentedon Dec 14, 2017
@youknowriad I'm glad to hear this is in progress.
You said
Some of them require Server Side logic (injected scriptswhich 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 :)youknowriad commentedon Dec 14, 2017
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 commentedon Dec 22, 2017
One valid reason for this is
i18n. I'd really love to be able to pull@wordpress/i18npackage 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 thewp.i18nlib 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 commentedon Dec 22, 2017
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 commentedon Dec 22, 2017
One more question is, should I count on the technique of putting the lib globally under
wpnamespace (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.i18nandwp.elementappeals to me a lot. This will allow me to build UIs using React without having to ship the lib itself with the plugin.youknowriad commentedon Dec 22, 2017
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
componentsmodule, I'm not sure we'd want to make it global in the future.[-][Enhancement] Move components, blocks and i18n to npm package(s)[/-][+]Move components, blocks and i18n to npm package(s)[/+]gziolo commentedon Feb 1, 2018
We probably will integrate Lerna into this repository and will publish
i18n,block,components,elementas packages from here. We would usenexttag 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?mention WordPress/gutenberg#3955 in readme
15 remaining items
nerrad commentedon Jun 5, 2018
ya looks like
@wordpress/dom/build/index.jshas an import statement when it shouldn't?nerrad commentedon Jun 5, 2018
It looks like it may be a babel-transpile bug related to this in
dom/src/index.js:gziolo commentedon Jun 5, 2018
Haha, I see it. It is definitely a bug in Babel. There are two things we still need to attack:
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-runtimeimports and server as one module. At the moment we have tons of code duplications. Any thoughts?nerrad commentedon Jun 5, 2018
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".
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)?
youknowriad commentedon Jun 5, 2018
@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:
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-polyfillor something globally? In general we try to avoid loading thebabel-polyfillglobally 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'optiongziolo commentedon Jun 7, 2018
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.
Shelob9 commentedon Jun 8, 2018
I'm testing with
Both tests are passing :)
gziolo commentedon Jun 8, 2018
I'm moving
pluginsmodule to packages and I noticed the same issue like indommodule:transpiles to commonjs with the unexpected:
gziolo commentedon Jul 10, 2018
#7832 upgrades Babel to v7 which will resolve all the issues we have with the transpiled code 🎉
gziolo commentedon 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.