Skip to content

Commit

Permalink
Merge pull request #320 from sfger/feature/babel7
Browse files Browse the repository at this point in the history
feature: Add support for react using babel 7
  • Loading branch information
niftylettuce authored Mar 14, 2022
2 parents 29d5ac5 + e918850 commit ec2e1ae
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1413,9 +1413,13 @@ exports.htmling.render = function(str, options, cb) {
* Rendering function
*/
function requireReact(module, filename) {
var babel = requires.babel || (requires.babel = require('babel-core'));

var compiled = babel.transformFileSync(filename, { presets: [ 'react' ] }).code;
try {
var babel = requires.babel || (requires.babel = require('@babel/core'));
var compiled = babel.transformFileSync(filename, {presets: ['@babel/preset-react']}).code;
} catch (err) {
babel = requires.babel || (requires.babel = require('babel-core'));
compiled = babel.transformFileSync(filename, {presets: ['react']}).code;
}

return module._compile(compiled, filename);
}
Expand All @@ -1426,14 +1430,18 @@ exports.requireReact = requireReact;
* Converting a string into a node module.
*/
function requireReactString(src, filename) {
var babel = requires.babel || (requires.babel = require('babel-core'));

if (!filename) filename = '';
var m = new module.constructor();
filename = filename || '';

// Compile Using React
var compiled = babel.transform(src, { presets: [ 'react' ] }).code;
try {
var babel = requires.babel || (requires.babel = require('@babel/core'));
var compiled = babel.transform(src, { presets: ['@babel/preset-react'] }).code;
} catch (err) {
babel = requires.babel || (requires.babel = require('babel-core'));
compiled = babel.transform(src, { presets: [ 'react' ] }).code;
}

// Compile as a module
m.paths = module.paths;
Expand Down

0 comments on commit ec2e1ae

Please sign in to comment.