yanns / play-react

Proof of concept of running react.js on the server side with play
42 stars 5 forks source link

Get server side react from webjars #4

Open pasviegas opened 9 years ago

pasviegas commented 9 years ago

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

yanns commented 9 years ago

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:

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

pasviegas commented 9 years ago

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.

benmccann commented 9 years ago

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

huntc commented 9 years ago

@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.

jamesward commented 9 years ago

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

pasviegas commented 9 years ago

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. :)

jamesward commented 9 years ago

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"
benmccann commented 9 years ago

Cool. Thanks for the tips!

pasviegas commented 9 years ago

Thanks a lot :)

yanns commented 9 years ago

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