play-react
play-react copied to clipboard
Get server side react from webjars
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
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!
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.
@jamesward or @huntc might know if there's a better way to read a source from a webjar
@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.
Are you trying to read the WebJar asset's contents in a running Play app or in an sbt plugin?
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. :)
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"
Cool. Thanks for the tips!
Thanks a lot :)
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