play-react icon indicating copy to clipboard operation
play-react copied to clipboard

Get server side react from webjars

Open pasviegas opened this issue 9 years ago • 10 comments

I really don't know how to do that, but it seems odd to have react in two different places.

Do you have any idea on how to do that?

Thanks

pasviegas avatar Oct 27 '14 01:10 pasviegas

You're right, it is not an optimal solution. For a technical proof of concept, it is sufficient.

But for a real world application, a better solution has to be found.

What I would like to achieve:

  • using the react tool from sbt-web
  • using react from webjar
  • managing dependencies with require.js or common.js modules + browersify

If you have any idea, please feel free to suggest them!

yanns avatar Oct 27 '14 08:10 yanns

So, I have figured out how to read the webjar file

Assets.resourceNameAt("/" + WebJarAssetLocator.WEBJARS_PATH_PREFIX, WebJarAssets.locate("react.min.js"))
        .flatMap(AssetInfo.resource)
        .map(url => {
        new String(readStream(url.openStream()))
      })

There should be a better way, but this is actually working.

pasviegas avatar Oct 27 '14 13:10 pasviegas

@jamesward or @huntc might know if there's a better way to read a source from a webjar

benmccann avatar Oct 29 '14 22:10 benmccann

@pasviegas If you're using sbt-web then webModules in Assets will return a Seq[File] of all libraries - including WebJars. Using this task key will eliminate the need to perform another locator lookup. sbt-web already does one for you.

huntc avatar Oct 29 '14 22:10 huntc

Are you trying to read the WebJar asset's contents in a running Play app or in an sbt plugin?

jamesward avatar Oct 30 '14 15:10 jamesward

In a running play app inside a controller. The code in my previous comment actually works, but I am guessing there should a better way. :)

pasviegas avatar Oct 30 '14 16:10 pasviegas

Here is how I'd do it:

val path = new WebJarAssetLocator().getFullPath("react", "react.min.js")
val maybeReactMinJs = Play.current.resourceAsStream(path).map { resource =>
  Source.fromInputStream(resource).mkString
}

This uses webjars-locator. Here are my deps:

"org.webjars" % "webjars-locator" % "0.19",
"org.webjars" % "react" % "0.12.0"

jamesward avatar Oct 30 '14 16:10 jamesward

Cool. Thanks for the tips!

benmccann avatar Oct 30 '14 18:10 benmccann

Thanks a lot :)

pasviegas avatar Oct 30 '14 19:10 pasviegas

To resolve `var React = require('./react'), trireme needs a real file and not a stream from a jar.

I think we can work around this like I did for the public folder: https://github.com/yanns/play-react/blob/master/project/PublicOnFileSystem.scala

yanns avatar May 10 '15 20:05 yanns