Git Product home page Git Product logo

rails-vue-cli's Introduction

Rails and Vue CLI

Concept Overview

This project shows how to build an application with both Rails and Vue CLI in the same repo.

It does not use the Rails gem Webpacker to manage JavaScript and CSS. It's fully delegated to Vue CLI.

Why?

Because sometime you want to use the power of Vue CLI and work with the "classic" Vue ecosystem.

How it works

Rails is only responsible for two things.

  1. Serve the first HTML page built with Vue CLI.
  2. Expose an API.

In the Rails routes, you can see:

get '*path', to: 'application#index', format: false

And the Application controller:

class ApplicationController < ActionController::Base
  def index
    render template: 'application'
  end
end

It means that Rails will dispatch any requests to the app/views/application.html.erb view. The Vue router is responsible to handle those requests with the correct components after the first load of the page.

For every API requests, we use the :api namespace to serve JSON in the same application that Vue CLI can use.

In the frontend/vue.config.js:

module.exports = {
  // proxy API requests to Rails during development
  devServer: {
    proxy: "http://localhost:3000"
  },

  // output built static files to Rails's public dir.
  // note the "build" script in package.json needs to be modified as well.
  outputDir: "../public",

  // modify the location of the generated HTML file.
  // make sure to do this only in production.
  indexPath:
    process.env.NODE_ENV === "production"
      ? "../app/views/application.html.erb"
      : "index.html"
};

We build the application in the Rails's public dir so it can be served by your Web server on production.

The indexPath is responsible to generate the app/views/application.html.erb that the ApplicationController will use.

In development, we now have two endpoints, localhost:3000 for the api and localhost:8080 for the Vue App.

In production, it works exactly the same way as if you run rails assets:precompile but CSS and JS are precompiled by Vue CLI instead.

Getting started

Install

Development environment requirements :

$ git clone https://github.com/guillaumebriday/rails-vue-cli.git
$ cd rails-vue-cli

To run the backend

$ bundle
$ bin/setup
$ rm config/credentials.yml.enc && bundle exec rails credentials:edit
$ bundle exec rails s

To run the Frontend:

$ cd frontend
$ yarn
$ yarn serve

Open http://localhost:8080 in your browser.

Development

Lint and fix Vue files:

$ cd frontend
$ yarn lint --fix

Build the image locally :

$ docker build -f .cloud/docker/Dockerfile -t rails-vue-cli .

Deploy in production

You need to build the frontend like you used to do with Webpacker:

$ cd frontend
$ yarn build

Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Credits

Inspired by:

License

This project is released under the MIT license.

rails-vue-cli's People

Contributors

guillaumebriday avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rails-vue-cli's Issues

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.