Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mintplugins/1533f7c275e540ee458429de5f426737 to your computer and use it in GitHub Desktop.
Save mintplugins/1533f7c275e540ee458429de5f426737 to your computer and use it in GitHub Desktop.
Build JSX Gutenberg Blocks without Webpack
<?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 );
@nabi009
Copy link

nabi009 commented Sep 6, 2020

hi,
error
Uncaught SyntaxError: Unexpected token '<'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment