hexo-renderer-react icon indicating copy to clipboard operation
hexo-renderer-react copied to clipboard

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

Open erisnuts opened this issue 7 years ago • 2 comments

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.

erisnuts avatar Jan 21 '17 17:01 erisnuts

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.

vanwagonet avatar Jan 21 '17 22:01 vanwagonet

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)

erisnuts avatar Jan 22 '17 11:01 erisnuts