Coder Social home page Coder Social logo

rui-ferreira / template-play2-react-typescript Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 25 KB

Play 2 / React / Typescript template application

License: Other

Java 60.10% HTML 7.77% Scala 10.15% TypeScript 4.57% JavaScript 17.16% CSS 0.26%
playframework react typescript template

template-play2-react-typescript's Introduction

Play 2 / React / Typescript sample application

This is a sample project to illustrate how to setup Play 2 to work with React and Typescript.

Checkout the project to use it as base for your own clone or rebuild it step by step by following instructions below.

After checkout, type cd frontend && yarn install to install all frontend dependencies (you just need to do this once).

To start the application, just type activator run.

Tools

Project structure

This project has the standard Play 2 folder structure, with an additional frontend folder where all client-side code written with React/Typescript will reside.

Build workflow

You should use the documented Play commands to run your application or to prepare the distributable package for deployment.

Development

While developing the application, use the run command in the SBT console. The SBT console can be started with any of these commands: sbt, activator or play.

In this mode, the server will be launched with the auto-reload feature enabled, meaning that for each request Play will check your project and recompile required sources. If needed the application will restart automatically.

Our SBT build (build.sbt) registers a Play hook to also watch for changes in the frontend code. Take a look at project/FrontendWatch.scala: it starts webpack in watch mode when the Play app starts and stops the process when the app stops. Webpack will bundle all frontend code nicely and put it in public/frontend (btw, you should git-ignore this folder), which is already under Play's standard umbrella.

Simply put, just run your old known activator run, write your code and reload the browser to see it coming to life.

Production

The best and usual approach to create a production distributable in Play is to use the dist task. This task builds a binary version of your application that you can deploy to a server without any dependency on SBT, the only thing the server needs is a Java installation.

To build the frontend assets production-ready, we use webpack -p. Unfortunately, there's no simple way to automate this in the SBT stages in a way that it would run only when sbt dist is called. This means you first need to build the frontend assets and then run the SBT task, as follows:

cd frontend
yarn run dist
cd ..
activator dist

If you use Apache ANT, you can automate this easily by creating a simple target and making your main dist task depend on it:

<target name="frontend-dist" description="Generates frontend dist">
    <exec executable="yarn" dir="frontend">
        <arg value="install" />
    </exec>

    <exec executable="yarn" dir="frontend">
        <arg value="run" />
        <arg value="dist" />
    </exec>
</target>

<target name="zipdist" depends="frontend-dist" description="Generate the distribution package">
    <exec executable="activator">
        <arg value="clean"/>
        <arg value="dist"/>
    </exec>
    <!--...-->
</target>    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.