Front-end JavaScipt and npm (ESNext+JSX)
-
Hi,
I am developing a Gutenberg block (plug-in) that shall use some JavaScript in the front end, implemented in ESNext+JSX and compiled usingnpm. Currently, the front-end JavaScript is enqueued from a js file (ES5) that is not compiled. It works, but I was wondering if there is a better way to do (that would typically allow for a safe use of ESNext).Some details about my current implementation:
The project was initiated usingcreate-guten-blockand follows the obtained structured:project │ package.json │ package-lock.json │ plugin.php │ readme.md │ └── dist │ │ blocks.build.js │ │ blocks.editor.build.css │ │ blocks.style.build.css │ └── node_modules │ │ ... │ └── src │ blocks.js │ common.scss │ init.php │ └── block │ block.js │ editor.scss │ style.scssnpmcompiles js and scss files fromsrc/todist/.plugin.phpincludesinit.php, that refers to the js and css files indist/.
I added my ES5 javascript asfrontend.jsinsrc/(src/frontend.js), and it is enqueued to my WordPress pages using:function myblock_frontend_script() { wp_enqueue_script( 'myblock-frontend', plugins_url( 'src/frontend.js', dirname( __FILE__ ) ), array( 'jquery' ), null ); } add_action( 'wp_enqueue_scripts', 'myblock_frontend_script' );As I said before, it works. But is there any way to have my
src/frontend.jscompiled bynpmtowarddist/(assrc/blocks.jsand hence the includedsrc/block/block.jsis), so that I can use ESNext+JSX?
The topic ‘Front-end JavaScipt and npm (ESNext+JSX)’ is closed to new replies.