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
How will solar energy be used 20 years from now?
Parent reviews for Line
Roblox Roguelike
Best Big Jumpshot 2K23
Uihc Family Medicine
Asian Feels Login
St Petersburg Craigslist Pets
Undergraduate Programs | Webster Vienna
Roblox Developers’ Journal
Optimal Perks Rs3
More Apt To Complain Crossword
Kentucky Downs Entries Today
Music Archives | Hotel Grand Bach - Hotel GrandBach
Tv Schedule Today No Cable
Nieuwe en jong gebruikte campers
Pollen Count Central Islip
Culos Grandes Ricos
Charmeck Arrest Inquiry
FAQ: Pressure-Treated Wood
The fabulous trio of the Miller sisters
Shreveport Active 911
boohoo group plc Stock (BOO) - Quote London S.E.- MarketScreener
Commodore Beach Club Live Cam
Fraction Button On Ti-84 Plus Ce
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Accuweather Mold Count
Leccion 4 Lesson Test
Is Windbound Multiplayer
Why Are Fuel Leaks A Problem Aceable
Walgreens On Bingle And Long Point
Evil Dead Rise Showtimes Near Sierra Vista Cinemas 16
Paradise Point Animal Hospital With Veterinarians On-The-Go
Helpers Needed At Once Bug Fables
Craigslist Texas Killeen
Martin Village Stm 16 & Imax
Panchang 2022 Usa
Newcardapply Com 21961
A Man Called Otto Showtimes Near Carolina Mall Cinema
Help with your flower delivery - Don's Florist & Gift Inc.
American Bully Xxl Black Panther
Natashas Bedroom - Slave Commands
Ticketmaster Lion King Chicago
2020 Can-Am DS 90 X Vs 2020 Honda TRX90X: By the Numbers
Great Clips Virginia Center Commons
Pa Legion Baseball
Unitedhealthcare Community Plan Eye Doctors
Toomics - Die unendliche Welt der Comics online
Enter The Gungeon Gunther
Espn Top 300 Non Ppr
Maurices Thanks Crossword Clue
Craigslist Pets Lewiston Idaho
Read Love in Orbit - Chapter 2 - Page 974 | MangaBuddy
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.