Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijunaid8989/65d3a493699e9842ee16e68bbd0aa372 to your computer and use it in GitHub Desktop.
Save ijunaid8989/65d3a493699e9842ee16e68bbd0aa372 to your computer and use it in GitHub Desktop.
Installing Bootstrap + Font Awesome from NPM in Phoenix Framework using sass
  • Tested with Phoenix 1.3

1) Installing sass, font-awesome and bootstrap package's

First, install the Sass, Font Awesome, bootstrap(and deps) package:

cd assets

  • npm install --save-dev sass-brunch
  • npm install --save font-awesome
  • npm install --save copycat-brunch
  • npm install --save jquery
  • npm install --save bootstrap@4.0.0-beta.3
  • npm install --save tether (bootstrap dep)
  • npm install --save popper.js (bootstrap dep)

2) Create folder fonts assets (because brunch will search and copy this folder)

  • You must create a folder fonts in path "assets/static" (assets/static/fonts)
  • cd assets/static
  • mkdir fonts
  • cd fonts
  • touch .gitkeep (you must create this to persist in git repo)

In the end you will have a folder like /assets/static/fonts containing the fonts

3) Update brunch-config.js (look the brunch-config.js attached)

  • Add saas config and copycat:

    sass: {
      mode: 'native',
      options: {
        includePaths: ["node_modules/font-awesome/scss", "node_modules/bootstrap/scss"], // Tell sass-brunch where to look for files to @import
        precision: 8 // Minimum precision required by bootstrap-sass
       }
     },
     copycat: {
       "fonts" : ["static/fonts", "node_modules/font-awesome/fonts"],
       verbose : false, //shows each file that is copied to the destination directory
       onlyChanged: true //only copy a file if it's modified time has changed (only effective when using brunch watch)
     }
    
  • Add "scss", "fonts" to your paths/watcher

    paths: {
      // Dependencies and current project directories to watch
      watched: ["static", "css", "js", "vendor","scss", "fonts"],
      // Where to compile files to
      public: "../priv/static"
    }
    
  • Update npm config

    npm: {
      enabled: true,
      globals: { // Bootstrap's JavaScript requires both '$' and 'jQuery' in global scope
        $: 'jquery',
        jQuery: 'jquery',
        Tether: 'tether',
        Popper: 'popper.js',
        bootstrap: 'bootstrap', // Require Bootstrap's JavaScript globally
      }
    }
    

4) Rename web/static/css/app.css to web/static/css/app.sass and add @import (look file attached)

  • add @import 'font-awesome';
  • add @import 'bootstrap';
@import 'font-awesome';
@import 'bootstrap';
exports.config = {
// See http://brunch.io/#documentation for docs.
files: {
javascripts: {
joinTo: "js/app.js"
},
stylesheets: {
joinTo: "css/app.css",
order: {
after: ["css/app.sass"] // concat app.css last
}
},
templates: {
joinTo: "js/app.js"
}
},
conventions: {
// This option sets where we should place non-css and non-js assets in.
// By default, we set this to "/assets/static". Files in this directory
// will be copied to `paths.public`, which is "priv/static" by default.
assets: /^(static)/
},
// Phoenix paths configuration
paths: {
// Dependencies and current project directories to watch
watched: ["static", "css", "js", "vendor", "scss", "fonts"],
// Where to compile files to
public: "../priv/static"
},
// Configure your plugins
plugins: {
babel: {
// Do not use ES6 compiler in vendor code
ignore: [/vendor/]
},
sass: {
mode: 'native',
options: {
includePaths: ["node_modules/bootstrap/scss", "node_modules/font-awesome/scss"], // Tell sass-brunch where to look for files to @import
precision: 8 // Minimum precision required by bootstrap-sass
}
},
copycat: {
"fonts" : ["static/fonts", "node_modules/font-awesome/fonts"],
verbose : false, //shows each file that is copied to the destination directory
onlyChanged: true //only copy a file if it's modified time has changed (only effective when using brunch watch)
}
},
modules: {
autoRequire: {
"js/app.js": ["js/app"]
}
},
npm: {
enabled: true,
globals: { // Bootstrap's JavaScript requires both '$' and 'jQuery' in global scope
$: 'jquery',
jQuery: 'jquery',
Tether: 'tether',
Popper: 'popper.js',
bootstrap: 'bootstrap', // Require Bootstrap's JavaScript globally
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment