Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
/ laravel-soppy Public archive

Keep your app hydrated-without APIs and state-management.

License

Notifications You must be signed in to change notification settings

truefrontier/laravel-soppy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 

Repository files navigation

laravel-soppy

This is an opinionated Laravel frontend setup for using vue-cli with vue-soppy in a Laravel project. You can even run multiple vue-cli projects (eg. app and admin). You can even share tailwindcss config between the two!

What and Why?

Check out this article on Medium to get a better understanding why vue-soppy exists.

How to setup

Prerequisites

Setup

1. Create your laravel project

$ laravel new my-project && cd my-project


2. Create the directory to hold your vue-cli app

$ mkdir resources/vue && cd resources/vue/


3. Create your vue-cli app

$ vue create app - Be sure to add vue-router in history mode and vuex


4. Add vue.config.js

See vue.config.js from this repo. Save it to /resources/vue/app/vue.config.js


5. Create soppy:make-routes command

See SoppyMakeRoutes.php from this repo. Save it to /app/Console/Commands/SoppyMakeRoutes.php


6. Add the following scripts to your package.json

package.json

{
  ...
  "scripts": {
    ...
    "routes": "php artisan soppy:make-routes"
    "preserve": "npm run routes",
    "serve": "cd resources/vue/app && yarn serve",
    "prebuild": "npm run routes",
    "build": "cd resources/vue/app && yarn build"
  },
}

7. Add AppController

See AppController.php from this repo. Save it to /app/Http/Controllers/AppController.php

NOTE: The important part is to return JSON when the request wants JSON and the view when it doesn't, like this:

public function someRouteAction(Request $request) {
  $data = [
    // Whatever date you want to be either injected on page load or return as JSON
  ];
  return $request->wantsJson() ? $data : view('app', ['data' => $data]);
}

8. Configure your routes

routes/web.php

Route::get('/', 'AppController@welcome')->name('app.welcome');

NOTE: The ->name('app.welcome') is important here. The php artisan soppy:make-routes command looks for all the routes with a name that starts with app. so that vue-soppy can use it for your routes in your vue app.

404 Not Found Route

routes/web.php

Route::get('{any?}', 'AppController@maybeJsonResponse')->where('any', '.*')->name('app.notFound');

resources/vue/app/src/router/index.js

// ...
const routes = [
  {
    name: 'app.notFound',
    path: '*',
  },
]

9. Inject data for initial pageload

Add the following to <head> in /resources/vue/app/public/index.html

index.html

<script>
  window.SoppyState = <% if (NODE_ENV === 'production') { %>@json(array_merge($data, []))<% } else { %>{}<% } %>;
</script>

10. Setup vue-soppy

Complete the setup instructions for vue-soppy


Multiple vue-cli projects

Let's say you have an app vue-cli project and you also want an admin vue-cli project. It's really simple:

Start with Step 3 above, but now use admin (or whatever you prefer) instead of app

Step 3

  • $ cd my-project/resources/vue/
  • $ vue create admin - Again, with vue-router in history mode and vuex

Step 4

  • Change AREA to admin and save to /resources/vue/admin/vue.config.js

Step 5 The soppy:make-routes command can take two params. By default, it runs:

soppy:make-routes --prefix=app --dest=resources/vue/app/src/router/routes.json

Now, see Step 6 👇🏾

Step 6

"routes:admin": "php artisan soppy:make-routes --prefix=admin"
"preserve:admin": "npm run routes:admin",
"serve:admin": "cd resources/vue/admin && yarn serve",
"prebuild:admin": "npm run routes:admin",
"build:admin": "cd resources/vue/admin && yarn build",

Step 7 Copy AppController.php and rename it AdminController.php

Step 8 Use admin. instead of app.; For example, ->name('admin.dashboard')

Step 9 Add it to resources/vue/admin/public/index.html instead

Step 10 You may consider installing packages that will be used in both projects in resources/vue/.

$ cd my-project/resources/vue/
$ echo "{}" >> package.json
$ npm i --save vue-soggy

Sharing components and assets between the two

1. Create a shared directory

$ mkdir my-project/resources/vue/shared && cd my-project/resources/vue/

2. Add alias in both vue.config.js files

resources/vue/[app/admin]/vue.config.js

modules.export = {
  // ...
  configureWebpack: {
    resolve: {
      alias: {
        '@@': path.join(__dirname, '../'),
      },
    },
  },
  // ...
}

3. Import shared files from @@/shared/your-file


Share Tailwindcss assets too

resources/vue/tailwind.config.js

const path = require('path');

module.exports = {
  purge: [
    path.join(__dirname, './*/src/**/*.html'),
    path.join(__dirname, './*/src/**/*.js'),
    path.join(__dirname, './*/src/**/*.vue'),
    path.join(__dirname, './*/src/**/*.scss'),
    path.join(__dirname, './shared/**/*.html'),
    path.join(__dirname, './shared/**/*.js'),
    path.join(__dirname, './shared/**/*.vue'),
    path.join(__dirname, './shared/**/*.scss'),
  ],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
};

resources/vue/postcss.config.js

const path = require('path');

module.exports = {
  plugins: [
    require('tailwindcss')(path.join(__dirname, './tailwind.config.js')),
    require('autoprefixer')(),
  ],
};

Set custom config path for postcss in vue.config.js

resources/vue/[app/admin]/vue.config.js

modules.export = {
  // ...
  css: {
    loaderOptions: {
      postcss: {
        config: {
          path: '../postcss.config.js',
        },
      },
    },
  },
  // ...
}

In each vue-cli project, you'll need to add to the main.js file:

resources/vue/[app/admin]/src/main.js

require('@@/shared/assets/scss/main.scss');

resources/vue/shared/assets/scss/main.scss

/* purgecss start ignore */
@tailwind base;
@tailwind components;
/* purgecss end ignore */
@tailwind utilities;