Skip to content

Commit

Permalink
Babel macro: Add new Babel macro which handles block.json file transf…
Browse files Browse the repository at this point in the history
…ormation
  • Loading branch information
gziolo committed Sep 9, 2019
1 parent 421de39 commit 464b0bd
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/manifest-devhub.json
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,12 @@
"markdown_source": "../packages/autop/README.md",
"parent": "packages"
},
{
"title": "@wordpress/babel-block.macro",
"slug": "packages-babel-block.macro",
"markdown_source": "../packages/babel-block.macro/README.md",
"parent": "packages"
},
{
"title": "@wordpress/babel-plugin-import-jsx-pragma",
"slug": "packages-babel-plugin-import-jsx-pragma",
Expand Down
166 changes: 149 additions & 17 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@babel/runtime-corejs3": "7.4.4",
"@babel/traverse": "7.4.4",
"@octokit/rest": "16.26.0",
"@wordpress/babel-block.macro": "file:packages/babel-block.macro",
"@wordpress/babel-plugin-import-jsx-pragma": "file:packages/babel-plugin-import-jsx-pragma",
"@wordpress/babel-plugin-makepot": "file:packages/babel-plugin-makepot",
"@wordpress/babel-preset-default": "file:packages/babel-preset-default",
Expand All @@ -91,6 +92,7 @@
"babel-plugin-inline-json-import": "0.3.2",
"babel-plugin-react-native-classname-to-style": "1.2.2",
"babel-plugin-react-native-platform-specific-extensions": "1.1.1",
"babel-plugin-tester": "7.0.1",
"benchmark": "2.1.4",
"browserslist": "4.6.2",
"chalk": "2.4.1",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-block.macro/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
13 changes: 13 additions & 0 deletions packages/babel-block.macro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Babel Block Macro

Babel block macro.

## Installation

Install the module to your project using [npm](https://www.npmjs.com/).

```bash
npm install @wordpress/babel-block.macro
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
79 changes: 79 additions & 0 deletions packages/babel-block.macro/macro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* External dependencies
*/
const { createMacro } = require( 'babel-plugin-macros' );
const { existsSync, readFileSync } = require( 'fs' );
const { mapKeys, pick } = require( 'lodash' );
const { dirname, join, relative } = require( 'path' );

function getFilename( [ filenamePath ], state ) {
const filename = filenamePath.evaluate().value;

return join(
relative( process.cwd(), dirname( state.file.opts.filename ) ),
filename
);
}

function readMetadata( filename ) {
if ( ! existsSync( filename ) ) {
throw new Error( `Invalid file name provided: ${ filename }.` );
}

const metadataRaw = readFileSync( filename, 'utf8' );

return JSON.parse( metadataRaw );
}

function formatMetadata( metadata, types ) {
const aliases = {
styleVariations: 'styles',
};
const replaceWithAlias = ( _, key ) => {
return aliases[ key ] || key;
};
const whitelistedProperties = [
'name',
'title',
'category',
'parent',
'icon',
'description',
'keywords',
'attributes',
'styles',
];

const metadataFiltered = pick(
mapKeys( metadata, replaceWithAlias ),
whitelistedProperties
);

const metadataNode = types.valueToNode( metadataFiltered );
/*const translatedProperties = [
'title',
'description',
'keywords',
];*/

return metadataNode;
}

function babelBlockMacro( { references, state, babel } ) {
const { types } = babel;
references.default.forEach( ( referencePath ) => {
if ( referencePath.parentPath.type === 'CallExpression' ) {
const metadata = readMetadata(
getFilename( referencePath.parentPath.get( 'arguments' ), state )
);

referencePath.parentPath.replaceWith( formatMetadata( metadata, types ) );
} else {
throw new Error(
`@wordpress/babel-block.macro can only be used as function call. You tried ${ referencePath.parentPath.type }.`,
);
}
} );
}

module.exports = createMacro( babelBlockMacro );
35 changes: 35 additions & 0 deletions packages/babel-block.macro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@wordpress/babel-block.macro",
"version": "1.0.0-alpha.1",
"description": "Babel block macro.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"block",
"babel",
"macro"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/babel-block.macro/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/babel-block.macro"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"engines": {
"node": ">=10"
},
"files": [
"index.js"
],
"main": "index.js",
"dependencies": {
"babel-plugin-macros": "^2.6.1"
},
"publishConfig": {
"access": "public"
}
}
Loading

0 comments on commit 464b0bd

Please sign in to comment.