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

Babel macro: Add new Babel macro which handles block.json file transformation #16088

Open
wants to merge 4 commits into
base: master
from

Conversation

@gziolo
Copy link
Member

commented Jun 11, 2019

Description

Input

{
	"name": "my-plugin/notice",
	"title": "Notice",
	"category": "common",
	"description": "Shows warning, error or success notices  ...",
	"keywords": [ "alert", "message" ],
	"textDomain": "default",
	"attributes": {},
	"styleVariations": [
		{ "name": "default", "label": "Default", "isDefault": true },
		{ "name": "other", "label": "Other" }
	]
}
import getBlockData from '@wordpress/blocks/macro';
const metadata = getBlockData( './fixtures/block-i18n.json' );

Output

\\"use strict\\";

var _i18n = require(\\"@wordpress/i18n\\");

const metadata = {
  name: \\"my-plugin/notice\\",
  title: (0, _i18n._x)(\\"Notice\\", \\"block title\\", \\"my-plugin\\"),
  category: \\"common\\",
  description: (0, _i18n._x)(\\"Shows warning, error or success notices  ...\\", \\"block description\\", \\"my-plugin\\"),
  keywords: [(0, _i18n._x)(\\"alert\\", \\"block keywords\\", \\"my-plugin\\"), (0, _i18n._x)(\\"message\\", \\"block keywords\\", \\"my-plugin\\")],
  attributes: {},
  styles: [{
    name: \\"default\\",
    label: (0, _i18n._x)(\\"Default\\", \\"block styles\\", \\"my-plugin\\"),
    isDefault: true
  }, {
    name: \\"other\\",
    label: (0, _i18n._x)(\\"Other\\", \\"block styles\\", \\"my-plugin\\")
  }]
};

TODO

  • Use Babel to inline block.json file, accept only whitelist properties and wrap those translatable with i18n calls
  • Make it work with at least one existing block
  • Decide whether we should Babel macro or regular plugin
  • Add cache invalidation for webpack and Jest when using a macro
  • Add documentation

Related resources:

Examples:

import preval from 'preval.macro'
import idx from 'idx.macro'

// preval macro is evaluated first, then idx
@talldan

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

Hi @gziolo - something I noticed recently is that changes to the block.json files aren't built during watch. I haven't looked into how the build for json files works. Is that something that this PR will fix or should it be tackled separately?

@gziolo

This comment has been minimized.

Copy link
Member Author

commented Jun 12, 2019

something I noticed recently is that changes to the block.json files aren't built during watch. I haven't looked into how the build for json files works. Is that something that this PR will fix or should it be tackled separately?

I noticed it as well. This PR doesn’t address it in the current shape but there are some existing solutions in creste-react-app showing how to enforce re-building files which use macros. I think the same issue applies to Jest. We can look into it separately.

@talldan

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

👍 I've created an issue - #16104

@gziolo gziolo referenced this pull request Aug 5, 2019
5 of 5 tasks complete

@gziolo gziolo force-pushed the add/babel-block-macro branch from dc4afea to 464b0bd Sep 9, 2019

@gziolo gziolo marked this pull request as ready for review Sep 11, 2019

@gziolo gziolo force-pushed the add/babel-block-macro branch from 690ff0a to 498d730 Sep 11, 2019

@gziolo gziolo force-pushed the add/babel-block-macro branch from 498d730 to 2054fb5 Sep 15, 2019

@gziolo gziolo requested a review from ellatrix as a code owner Sep 15, 2019

@gziolo

This comment has been minimized.

Copy link
Member Author

commented Sep 15, 2019

I updated PR to allow the following usage in the block's file:

import getBlockData from '@wordpress/blocks/macro';
const metadata = getBlockData( './fixtures/block-i18n.json' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.