1
0

More than 3 years have passed since last update.

Rails6でBootstrapとFontAwesomeをwebpack経由で使う

Last updated at Posted at 2021-02-09

BootstrapとFontAwesomeのwebpackでの導入方法

Rails6からはwebpackが標準で搭載され、javascriptとstylesheetのファイルの位置も変わっており、意外に苦戦したので記載。

まずはターミナルからyarnでインストール

$ yarn add bootstrap@4.3.1 jquery popper.js
$ yarn add @fortawesome/fontawesome-free

下記のコードを追記

私はjavascriptディレクトリの下にsrcディレクトリを配置しているので注意。

app/javascript/src/application.scss
@import "~bootstrap/scss/bootstrap";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
app/javascript/packs/application.js
import "bootstrap";
import "@fortawesome/fontawesome-free/js/all"
config/webpack/environment.js

const webpack = require('webpack')
environment.plugins.prepend(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    Popper: 'popper.js'
  })
)
app/views/layouts/application.html.erb
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 
//↑↑削除

<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>

コピペ可能です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0