How To Get Phoenix & VueJS working Together?

FYI, just got Vue running on edge(ish) Phoenix, which now defaults to Webpack. Here’s the diff between the default assets/webpack.config.js and the version running Vue. (I put the view app in ./js along with the other Javascripts.

const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
+ const VueLoaderPlugin = require('vue-loader/lib/plugin');


module.exports = (env, options) => ({
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  entry: {
    './js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js'))
  },
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../priv/static/js')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
+      {
+       test: /\.vue$/,
+        loader: 'vue-loader'
+      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
+  resolve: {
+    alias: {
+      'vue$': 'vue/dist/vue.esm.js'
+    },
+    extensions: ['*', '.js', '.vue', '.json']
+  },
  plugins: [
    new MiniCssExtractPlugin({ filename: '../css/app.css' }),
    new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
+    new VueLoaderPlugin(),
  ]
});

Putting this here in case it saves someone a bit of time when 1.4 comes out.

6 Likes

building on @paulanthonywilson 's post, in case you weren’t aware you will need to do a couple other things to get Vue.js “working”

This is my naive implementation, there may be more steps for other features and nuances but this will get something showing up in a browser. Using Phoenix 1.4 RC0:

edit package.json:

{
  "repository": {},
  "license": "MIT",
  "scripts": {
    "deploy": "webpack --mode production",
    "watch": "webpack --mode development --watch"
  },
  "dependencies": {
    "phoenix": "file:../deps/phoenix",
    "phoenix_html": "file:../deps/phoenix_html"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "babel-loader": "^8.0.0",
    "copy-webpack-plugin": "^4.5.0",
    "css-loader": "^0.28.10",
    "mini-css-extract-plugin": "^0.4.0",
    "optimize-css-assets-webpack-plugin": "^4.0.0",
    "uglifyjs-webpack-plugin": "^1.2.4",
+    "vue": "^2.5.17",
+    "vue-loader": "^15.4.2",
    "webpack": "4.4.0",
    "webpack-cli": "^2.0.10"
  }
}

cd <project root>
cd assets && npm install && node node_modules/webpack/bin/webpack.js --mode development

^^ this should have npm add Vue as a dependency

in <project root>/assets/js/: add and save new file vue-hello-world.js:

import Vue from 'vue'

const helloWorldContainer = document.querySelector("#hello-world-container")

if (helloWorldContainer) {
  new Vue({
    el: "#hello-world-container",
    data() {
      return {
        msg: "Hello World from Vue!"
      }
    }
  });
}  

^^ we will eventually add a hello-world-container div tag

in <project root>/assets/js/: edit file app.js and import/glue your vue-hello-world.js to the global app imports at the bottom:

// We need to import the CSS so that webpack will load it.
// The MiniCssExtractPlugin is used to separate it out into
// its own CSS file.
import css from "../css/app.css"

// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import dependencies
//
import "phoenix_html"

// Import local files
//
// Local files can be imported directly using relative paths, for example:
// import socket from "./socket"

+ import "./vue-hello-world"

Open <project root>/templates/page/index.html.eex and add your div:

+ <div id="hello-world-container" >
+       <h1>{{ msg }}</h1>
+ </div>
<section class="phx-hero">
  <h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
  <p>A productive web framework that<br/>does not compromise speed and maintainability.</p>
</section>

<section class="row">
  <article class="column">
    <h2>Resources</h2>
    <ul>
      <li>
        <a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
      </li>
      <li>
        <a href="https://github.com/phoenixframework/phoenix">Source</a>
      </li>
      <li>
        <a href="https://github.com/phoenixframework/phoenix/blob/v1.4/CHANGELOG.md">v1.4 Changelog</a>
      </li>
    </ul>
  </article>
  <article class="column">
    <h2>Help</h2>
    <ul>
      <li>
        <a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
      </li>
      <li>
        <a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
      </li>
      <li>
        <a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
      </li>
    </ul>
  </article>
</section>

From the project root run the app: mix phx.server

You should see a webpack log similar to:
[./js/vue-hello-world.js] 275 bytes {./js/app.js} [built]

Open localhost:4000. You should see the “Hello World from Vue!” message.

Finally, get the Chrome Vue plugin:

9 Likes

An up to date (September 2019) post about integrating VueJS step by step into a Phoenix application is here

4 Likes