vanwagonet / hexo-renderer-react

React+babel renderer plugin for Hexo
MIT License
40 stars 8 forks source link

How can I use .js files instead of .jsx ? #10

Open erisnuts opened 7 years ago

erisnuts commented 7 years ago

If I making js instead of jsx renderer dosen't work. But if I renaming the file - works perfectly. I am not using JSX so I want make js files. Help, please.

vanwagonet commented 7 years ago

You may just need to add the following to index.js. Could you try it out?

hexo.extend.renderer.register('js', 'html', renderer, true)

I'm a little nervous about assuming every .js file is a react component, though.

erisnuts commented 7 years ago

yes, it works, thank you!

hexo-renderer-react/index.js

hexo.extend.renderer.register('js', 'html', function (data, locals) {
  var js = babel.transform(data.text, { filename: data.path })
  var Component = reval(js.code, data.path, null, true)
  var element = React.createElement(Component.default || Component, locals)
  var markup = ReactDOMServer.renderToStaticMarkup(element);

  if(markup.match(/^<html/)) {
    markup = '<!doctype html>' + markup;
  }

  return markup;
}, true)