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

Block library: Try to use Babel plugins to inline block.json metadata #14551

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = function( api ) {

return {
presets: [ '@wordpress/babel-preset-default' ],
plugins: [ 'babel-plugin-inline-json-import' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that can be useful for our default preset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it would land there eventually once proved that we like this approach 👍

By the way, this plugin still depends on Babel 6. Not sure it's an issue, just noting.

gziolo marked this conversation as resolved.
Show resolved Hide resolved
};
};
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@wordpress/npm-package-json-lint-config": "file:packages/npm-package-json-lint-config",
"@wordpress/postcss-themes": "file:packages/postcss-themes",
"@wordpress/scripts": "file:packages/scripts",
"babel-plugin-inline-json-import": "0.3.2",
"benchmark": "2.1.4",
"browserslist": "4.4.1",
"chalk": "2.4.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
setDefaultBlockName,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
} from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -120,7 +121,10 @@ export const registerCoreBlocks = () => {
if ( ! block ) {
return;
}
const { name, settings } = block;
const { metadata, settings, name } = block;
if ( metadata ) {
unstable__bootstrapServerSideBlockDefinitions( { [ name ]: metadata } ); // eslint-disable-line camelcase
}
registerBlockType( name, settings );
} );

Expand Down
26 changes: 26 additions & 0 deletions packages/block-library/src/text-columns/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "core/text-columns",
"icon": "columns",
"category": "layout",
"attributes": {
"content": {
"type": "array",
"source": "query",
"selector": "p",
"query": {
"children": {
"type": "string",
"source": "html"
}
},
"default": [ {}, {} ]
},
"columns": {
"type": "number",
"default": 2
},
"width": {
"type": "string"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I have a lot to say about these :), can we start with something very small like the "category"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this PR is more about the way javascript consumes this file rather than about the content of the file directly and that's why I suggest to keep it to the minimum for the moment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care what we include, I opted for everything that server is able to expose to the client:

https://github.com/WordPress/wordpress-develop/blob/689ba4eec66b8d735c57940303ea0d49657f4cb3/src/wp-admin/includes/post.php#L2206

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed it's not very important for now (to filter or not) but the shape of some of these properties might need to be changed later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supports removed, should I remove anything else?

}
35 changes: 8 additions & 27 deletions packages/block-library/src/text-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ import {
} from '@wordpress/block-editor';
import deprecated from '@wordpress/deprecated';

export const name = 'core/text-columns';
/**
* Internal dependencies
*/
import metadata from './block.json';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets transpiled into:

Screen Shot 2019-03-21 at 16 47 22


const { name } = metadata;

export { metadata, name };

export const settings = {
// Disable insertion as this block is deprecated and ultimately replaced by the Columns block.
Expand All @@ -30,32 +37,6 @@ export const settings = {

description: __( 'This block is deprecated. Please use the Columns block instead.' ),

icon: 'columns',

category: 'layout',

attributes: {
content: {
type: 'array',
source: 'query',
selector: 'p',
query: {
children: {
type: 'string',
source: 'html',
},
},
default: [ {}, {} ],
},
columns: {
type: 'number',
default: 2,
},
width: {
type: 'string',
},
},

transforms: {
to: [
{
Expand Down
7 changes: 5 additions & 2 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ import { isValidIcon, normalizeIconObject } from './utils';
let serverSideBlockDefinitions = {};

/**
* Set the server side block definition of blocks.
* Sets the server side block definition of blocks.
*
* @param {Object} definitions Server-side block definitions
*/
export function unstable__bootstrapServerSideBlockDefinitions( definitions ) { // eslint-disable-line camelcase
gziolo marked this conversation as resolved.
Show resolved Hide resolved
serverSideBlockDefinitions = definitions;
serverSideBlockDefinitions = {
...serverSideBlockDefinitions,
...definitions,
};
}

/**
Expand Down
5 changes: 3 additions & 2 deletions playground/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"plugins": [
[ "@babel/plugin-transform-react-jsx", {
"pragma": "createElement"
} ]
} ],
"babel-plugin-inline-json-import"
]
}
}