Skip to content

Instantly share code, notes, and snippets.

@hzamani
Last active December 11, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hzamani/f5dfa6f5f5d04dda2c6b to your computer and use it in GitHub Desktop.
Save hzamani/f5dfa6f5f5d04dda2c6b to your computer and use it in GitHub Desktop.
Configure phoenix framework to use webpack as assets manager
  1. For new projects run mix phoenix.new with --no-brunch option and for existing projects remove brunch related things.

  2. create package.json file:

echo '{"private": true}' > package.json
  1. install webpack and loaders you want to use:
npm install --save-dev webpack babel-loader babel-core livescrip-loader
  1. Create a webpack config file, I prefere it to be in config/ dir (e.i config/webpack.js)
module.exports = {
  devtool: "source-map",
  entry: "./web/client/app.ls",
  output: {
    path: "./priv/static/js",
    filename: "app.js"
  },
  resolve: {
    // if not using livescript remove ".ls"
    // for using coffeescript add ".coffee"
    extensions: ["", ".js", ".jsx", ".ls"]
  },
  module: {
    loaders: [
      // uncomment to use coffeescript
      // { test: /\.coffee$/, loader: 'coffeescript' }
      { test: /\.ls$/, loader: 'livescrip' },
      { test: /\.jsx?$/, loader: 'babel', exclude: /node_modules/ }
    ]
  }
}
  1. Add webpack to watchers array in config/dev.exs:
use Mix.Config
# ...
config :appname, AppName.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  cache_static_lookup: false,
  check_origin: false,
  watchers: [node: ~w{node_modules/webpack/bin/webpack.js --config config/webpack.js --color --watch-stdin}]
# ...
  1. wite some code in your entry file:
echo "console.log 'Working!'" > web/client/app.ls
  1. start server and enjoy:
mix phoenix.server start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment