-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Babel macro: Add new Babel macro which handles block.json file transf…
…ormation
- Loading branch information
Showing
10 changed files
with
391 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.