Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get server side react from webjars #4

Open
pasviegas opened this issue Oct 27, 2014 · 10 comments
Open

Get server side react from webjars #4

pasviegas opened this issue Oct 27, 2014 · 10 comments

Comments

@pasviegas
Copy link

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
Copy link
Owner

yanns commented Oct 27, 2014

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!

@pasviegas
Copy link
Author

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
Copy link
Contributor

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

@huntc
Copy link

huntc commented Oct 29, 2014

@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
Copy link

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

@pasviegas
Copy link
Author

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
Copy link

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
Copy link
Contributor

Cool. Thanks for the tips!

@pasviegas
Copy link
Author

Thanks a lot :)

@yanns
Copy link
Owner

yanns commented May 10, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants