{"id":1546,"date":"2020-06-13T21:45:40","date_gmt":"2020-06-13T14:45:40","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1546"},"modified":"2020-08-31T10:28:00","modified_gmt":"2020-08-31T03:28:00","slug":"build-gutenberg-javascript","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/build-gutenberg-javascript\/","title":{"rendered":"How to Compile Gutenberg JavaScript"},"content":{"rendered":"\n<p>Compiling JavaScript used to be a foreign thing for a WordPress developer. Now we need to do it to create a custom Gutenberg block.<\/p>\n\n\n\n<p>Basically, Gutenberg uses <strong>JSX syntax<\/strong> that allows HTML inside JavaScript:<\/p>\n\n\n\n<pre title=\"my-block.js\" class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ JSX syntax\nedit: function( props ) {\n  return (\n    &lt;div className={ props.className }>\n      Hello World!\n    &lt;\/div>\n  );\n}<\/code><\/pre>\n\n\n\n<p>But that code won&#8217;t run, you <strong>need to convert them<\/strong> into normal syntax:<\/p>\n\n\n\n<pre title=\"my-block.js\" class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ Converted to normal syntax\nedit: function( props ) {\n  return element.createElement( 'div', {\n      className: props.className\n    },\n    'Hello World!'\n  );\n}<\/code><\/pre>\n\n\n\n<p>Follow the steps below to learn how to convert them!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Install Node JS<\/h2>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-gray-background-color\" href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\" rel=\"noreferrer noopener\">Download Node JS<\/a><\/div>\n<\/div>\n\n\n\n<p>Select the LTS Version as recommended and install it.<\/p>\n\n\n\n<p>Check if it&#8217;s successfully installed by running this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">node -v<\/code><\/pre>\n\n\n\n<p>If you see the version number, then you&#8217;re good to go. Otherwise, try restarting your computer first.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create Config File<\/h2>\n\n\n\n<p>For this example, we are assuming our theme structure looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">my-theme\/\n\u251c\u2500\u2500 js\/\n\u251c\u2500\u2500 jsx\/\n\u2502   \u2514\u2500\u2500 my-block.js\n\u251c\u2500\u2500 index.php\n\u251c\u2500\u2500 page.php\n\u251c\u2500\u2500 ...<\/code><\/pre>\n\n\n\n<p>The folder <code>\/jsx<\/code> is where our pre-compiled javascript lies. Later it will be compiled into <code>\/js<\/code>.<\/p>\n\n\n\n<p>Now, create a config file named <code>package.json<\/code> in your theme&#8217;s root folder:<\/p>\n\n\n\n<pre title=\"package.json\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"name\": \"my-block\",\n  \"private\": true,\n  \"author\": \"Henner Setyono (https:\/\/pixelstudio.id\/blog)\",\n  \"devDependencies\": {\n    \"@wordpress\/components\": \"^9.5.0\",\n    \"@wordpress\/scripts\": \"^9.0.0\"\n  },\n  \"scripts\": {\n    \"watch\": \"wp-scripts start jsx\/my-block.js --output-path=js\",\n    \"build\": \"wp-scripts build jsx\/my-block.js --output-path=js\"\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Important<\/strong>: Adapt the <code>scripts<\/code> part to fit your project.<\/p>\n\n\n\n<p>You can also define multiple JS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">\"watch\": \"wp-scripts start jsx\/my-block.js jsx\/other-block.js --output-path=js\",<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2b (Optional): Create Sample JavaScript<\/h2>\n\n\n\n<p>If you don&#8217;t have any script to compile, use this sample to try:<\/p>\n\n\n\n<pre title=\"jsx\/my-block.js\" class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">import { registerBlockType } from '@wordpress\/blocks';\nimport { RichText } from '@wordpress\/block-editor';\n\nregisterBlockType( 'my\/custom-block', {\n\ttitle: 'Custom Block',\n\ticon: 'universal-access-alt',\n\tcategory: 'layout',\n\tattributes: {\n\t\tcontent: {\n\t\t\ttype: 'array',\n\t\t\tsource: 'children',\n\t\t\tselector: 'p',\n\t\t},\n\t},\n\texample: {},\n\tedit: ( props ) => {\n    let atts = props.attributes;\n\n\t\treturn (\n\t\t\t&lt;RichText\n\t\t\t\ttagName=\"p\"\n\t\t\t\tclassName={ props.className }\n        value={ atts.content }\n\t\t\t\tonChange={ ( newContent ) => {\n          setAttributes( { content: newContent } );\n        } }\n\t\t\t\/>\n\t\t);\n\t},\n\tsave: ( props ) => {\n    let atts = props.attributes;\n\n\t\treturn (\n\t\t\t&lt;RichText.Content tagName=\"p\" value={ atts.content } \/>\n\t\t);\n\t},\n} );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install Dependencies<\/h2>\n\n\n\n<p>Open command prompt \/ terminal in your theme and run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">npm install<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"404\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-npm-install.jpg\" alt=\"\" class=\"wp-image-1550\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-npm-install.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-npm-install-480x259.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>There are a lot of WARNING but you can safely ignore them<\/figcaption><\/figure>\n\n\n\n<p>After finish, you will see <code>\/node_modules<\/code> folder. If you use Git, make sure to ignore this folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Compile!<\/h2>\n\n\n\n<p>Run this command to compile your JS everytime it changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">npm run watch<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"328\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-watch.jpg\" alt=\"\" class=\"wp-image-1551\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-watch.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/06\/compile-jsx-watch-480x210.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>Compiling the JS everytime it changes<\/figcaption><\/figure>\n\n\n\n<p>After you finished developing your custom block, run this command <strong>to minify<\/strong> it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">npm run build<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The main reason to use JSX is to make your code <strong>cleaner and easier to read<\/strong>. Yes, the setup process can be confusing, but it&#8217;s worth the effort.<\/p>\n\n\n\n<p>To learn how to make custom Gutenberg block, <a rel=\"noreferrer noopener\" href=\"https:\/\/pixelstudio.id\/blog\/custom-block-without-plugin\/\" target=\"_blank\">read our tutorial<\/a>. It uses normal syntax, but you can replace it with our sample JS above.<\/p>\n\n\n\n<p>Actually, I wrote a series of Gutenberg tutorial using normal syntax <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/hrsetyono\/gutenberg-tutorial\" target=\"_blank\">on this Github<\/a>. Feel free to check it out \ud83d\ude42<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Let me know in the comment if you have any question regarding JSX<\/p><\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compiling JavaScript used to be a foreign thing for a WordPress developer. Now we need to do it to create a custom Gutenberg block.<\/p>\n","protected":false},"author":1,"featured_media":1553,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[22,32],"class_list":["post-1546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gutenberg","tag-gutenberg","tag-javascript"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":10,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"predecessor-version":[{"id":1869,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions\/1869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1553"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}