Created
July 29, 2018 19:18
-
-
Save mintplugins/1533f7c275e540ee458429de5f426737 to your computer and use it in GitHub Desktop.
Build JSX Gutenberg Blocks without Webpack
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
<?php | |
function my_custom_gutenberg_block_enqueue_scripts(){ | |
// Required thing to build Gutenberg Blocks | |
$required_js_files = array( | |
'wp-blocks', | |
'wp-i18n', | |
'wp-element', | |
'wp-editor' | |
); | |
// Add React files - you may wish to save and move these directly into your plugin if you'd rather not load from a CDN | |
wp_enqueue_script( 'react', 'https://unpkg.com/react@16/umd/react.development.js', $required_js_files ); | |
$required_js_files[] = 'react'; | |
// Add React DOM | |
wp_enqueue_script( 'react-dom', 'https://unpkg.com/react-dom@16/umd/react-dom.development.js', $required_js_files ); | |
$required_js_files[] = 'react-dom'; | |
// Add Babel file | |
wp_enqueue_script( 'babel', 'https://unpkg.com/babel-standalone@6/babel.min.js', $required_js_files ); | |
$required_js_files[] = 'babel'; | |
// Your Gutenberg Block JSX code | |
wp_enqueue_script( 'my_custom_gutenberg_block_gutenberg_js', plugin_dir_url( __FILE__ ) . 'assets/js/my-custom-gutenberg-jsx.js', $required_js_files ); | |
} | |
add_action( 'admin_enqueue_scripts', 'my_custom_gutenberg_block_enqueue_scripts' ); | |
function my_custom_gutenberg_block_gutenberg_modify_jsx_tag( $tag, $handle, $src ) { | |
// Convert our custom block file to be recognized as a JSX file | |
if ( 'my_custom_gutenberg_block_gutenberg_js' == $handle ) { | |
$tag = str_replace( "<script type='text/javascript'", "<script type='text/babel'", $tag ); | |
} | |
return $tag; | |
} | |
add_filter( 'script_loader_tag', 'my_custom_gutenberg_block_gutenberg_modify_jsx_tag', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi,
error
Uncaught SyntaxError: Unexpected token '<'