jsx-node (2024)

Use JSX as a template engine in Node.

This module enables requiring .jsx files in Node. It does this by usingbabel andbabel-plugin-transform-react-jsxto first transform jsx intohyperscript, and then transformall h() calls in the resulting hyperscript into more efficient stringconcatenations.

Warning! This is a Proof of Concept

This module is still in a very early phase. Any production use shouldbe approached with caution.

Basic Use

To be able to simply require .jsx files, you needto tell Node what to do with them. Running the following code makes youable to require('./SomeFile.jsx'):

require('jsx-node').install({

replace:{

preact:'jsx-node',

}

});

As you can see, you are able to pass some options. The default options are:

constdefaultOptions={

extension:'.jsx',

presets:['es2015-node6'],

plugins:[

'add-module-exports',

],

};

options passed to the install method are assigned/extended withdefaultOptions, not (deeply) merged. If you want to add a new plugin orpreset, but still use the default ones you have to explicitly send all defaultsin your array.

Stateless & ES6 Components recommended

To make the the most efficient and simplest use of this library, make sure youonly use stateless or ES6 class components.

For ES6 Class Modules, make sure the initial state is set in the constructor andnowhere else.

Compatible With Preact

This module is designed to be compatible with Preact.

  • className prop is aliased to class, and class can be a plain object.any keys with truthy values will be used as a class.

Usage With Express

server.js

'usestrict';

//makenodeunderstand`*.jsx`files

require('jsx-node/node-require').install();

constchalk=require('chalk');

constexpress=require('express');

constserver=express();

//overridedefaultresponserendermethod

server.response.render=require('./render.jsx');

//loadroutes

server.use(require('./router'));

//getportfromenvorusedefault

constport=process.env.PORT||1337;

//startserver,savereferencetoHTTPServerinstancereturned

server.http=server.listen(port,()=>{

console.info(`[${chalk.cyan('INIT')}]HTTPServerlisteningonport${chalk.magenta(port)}(${chalk.yellow(server.get('env'))})`);

});

//exportinstanceofserverifneededbyothermodules

module.exports=server;

render.jsx

If all our jsx files adhere to stateless and ES6 Classes, the following code worksgreat to override the default express rendering logic.

'usestrict';

module.exports=function(Component,Master){

if(typeofComponent!=='function')thrownewError('NotaComponent');

constlocals=Object.assign({},this.app.locals,this.locals);

if(typeofMaster==='function'){

this.send('<!doctypehtml>'+(

<Master{...locals}>

<Component{...locals}/>

</Master>

));

}elseif(Component.prototype&&Component.prototype.render){

consti=newComponent(locals);

this.send(i.render(i.props,i.state));

}else{

this.send(Component(this.props));

}

};

router.js

'usestrict';

constContact=require('./Contact.jsx');

constIndex=require('./Index.jsx');

constMaster=require('./Master.jsx');

constrouter=new(require('express')).Router();

router.get('/',(req,res,next)=>{

res.render(Index,Master);

});

router.get('/contact',(req,res,next)=>{

res.render(Contact,Master);

});

module.exports=router;

TODO

  • Write tests

babel plugin

  • Escape unsafe code insert, but enable injecting of html from variables likeregions
  • Use custom Component class (needs to be done in babel plugin)
  • Remove all methods on class components except constructor and render, butkeep all methods used in those
  • Remove all dependencies not used in methods from above point
jsx-node (2024)
Top Articles
Finger weg! Die schlechtesten ETFs 2019
The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6342

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.