Skip to content

Commit 03ee521

Browse files
committed
finishing docs
1 parent 08ca67c commit 03ee521

File tree

4 files changed

+158
-15
lines changed

4 files changed

+158
-15
lines changed

docs/bootstrap.rst

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,116 @@
11
Using Bootstrap CSS & JS
22
========================
33

4-
TODO - show how to bring in bootstrap JS and bootstrap-sass.
4+
Want to use Bootstrap (or something similar) in your project? No problem!
5+
First, install it. To be able to customize things further, we'll install
6+
``bootstrap-sass``:
57

6-
Here is an example from before to include... because I think it's really nice :).
8+
.. code-block:: terminal
79
8-
// when no file path is defined (i.e. no file extension) Webpack loads the
9-
// given JavaScript module installed in node_modules/ dir (Webpack knows all
10-
// the specific files that must be loaded and in which order)
11-
require('bootstrap-star-rating');
10+
$ yarn add bootstrap-sass --dev
11+
12+
Importing Bootstrap SASS
13+
------------------------
14+
15+
Now that ``bootstrap-sass`` lives in your ``node_modules`` directory, you can
16+
import it from any SASS or JavaScript file. For example, if you're already have
17+
a ``global.scss`` file, import it from there:
18+
19+
.. code-block:: css
20+
21+
// assets/css/global.scss
22+
23+
// customize some Bootstrap variables
24+
$brand-primary: darken(#428bca, 20%);
25+
26+
// the ~ allows you to reference things in node_modules
27+
@import '~bootstrap-sass/assets/stylesheets/bootstrap';
28+
29+
That's it! This imports the ``node_modules/bootstrap-sass/assets/stylesheets/_bootstrap.scss``
30+
file into ``global.scss``. You can even customize the Bootstrap variables first!
31+
32+
.. tip::
33+
34+
If you don't need *all* of Bootstrap's features, you can include specific files
35+
in the ``bootstrap`` directory instead - e.g. ``~bootstrap-sass/assets/stylesheets/bootstrap/alerts``.
36+
37+
After including ``bootstrap-sass``, your webpack builds might become slow. To fix
38+
this, you can use the ``resolve_url_loader`` option:
39+
40+
.. code-block:: diff
41+
42+
// webpack.config.js
43+
Encore
44+
+ enableSassLoader({
45+
+ 'resolve_url_loader' => false
46+
+ })
47+
;
48+
49+
This disables the ``resolve-url-loader`` in Webpack, which means that any
50+
``url()`` paths in your SASS files must now be relative to the original source
51+
entry file instead of whatever file you're inside of (see `Problems with url()`_).
52+
To load Bootstrap, you'll need to override the path to its icons:
53+
54+
.. code-block:: diff
55+
56+
// assets/css/global.scss
57+
58+
+ $icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
59+
60+
+ // set if you're also including font-awesome
61+
+ // $fa-font-path: "~font-awesome/fonts";
1262
13-
// when a file path is given, but it doesn't start with '/' or './', the file
14-
// path is considered relative to node_modules/ dir
63+
@import '~bootstrap-sass/assets/stylesheets/bootstrap';
64+
65+
Importing Bootstrap JavaScript
66+
------------------------------
67+
68+
Bootstrap JavaScript requires jQuery, so make sure you have this installed:
69+
70+
.. code-block:: terminal
71+
72+
$ yarn add jquery --dev
73+
74+
Next, make sure call ``.autoProvidejQuery()`` in your ``webpack.config.js`` file:
75+
76+
.. code-block:: diff
77+
78+
// webpack.config.js
79+
Encore
80+
// ...
81+
+ .autoProvidejQuery()
82+
83+
This is needed because Bootstrap expects jQuery to be available as a global
84+
variable. Now, require bootstrap from any of your JavaScript files:
85+
86+
.. code-block:: javascript
87+
88+
// main.js
89+
90+
var $ = require('jquery');
91+
// JS is equivalent to the normal "bootstrap" package
92+
// no need to set this to a variable, just require it
93+
require('bootstrap-sass');
94+
95+
// or you can include specific pieces
96+
// require('bootstrap-sass/javascripts/bootstrap/tooltip');
97+
// require('bootstrap-sass/javascripts/bootstrap/popover');
98+
99+
$(document).ready(function() {
100+
$('[data-toggle="popover"]').popover();
101+
});
102+
103+
Thanks to ``autoProvidejQuery()``, you can require any other jQuery
104+
plugins in a similar way:
105+
106+
.. code-block:: javascript
107+
108+
// ...
109+
110+
// require the JavaScript
111+
require('bootstrap-star-rating');
112+
// require 2 CSS files needed
15113
require('bootstrap-star-rating/css/star-rating.css');
114+
require('bootstrap-star-rating/themes/krajee-svg/theme.css');
16115
17-
// when a file path is given and it starts with '/', './', or '../', it's considered
18-
// as the full file path for the asset (it can live outside the node_modules/ dir)
19-
require('../../../../../node_modules/bootstrap-star-rating/themes/krajee-svg/theme.css');
116+
.. _`Problems with url()`: https://github.com/webpack-contrib/sass-loader#problems-with-url

docs/page-specific-assets.rst

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
Creating Page-Specific CSS/JS
22
=============================
33

4-
TODO
4+
If you're creating a single page app (SPA), then you probably only need to define
5+
*one* entry in ``webpack.config.js``. But if you have multiple pages, you might
6+
want page-specific CSS and JavaScript.
7+
8+
For example, suppose you have a checkout page that has its own JavaScript. Create
9+
a new ``checkout`` entry:
10+
11+
.. code-block:: diff
12+
13+
// webpack.config.js
14+
15+
Encore
16+
// an existing entry
17+
.addEntry('app', './assets/js/main.js')
18+
// a global styles entry
19+
.addStyleEntry('global', './assets/css/global.scss')
20+
21+
+ .addEntry('checkout', './assets/js/checkout.js')
22+
;
23+
24+
Inside ``checkout.js``, add or require the JavaScript and CSS you need.
25+
Then, just include a ``script`` tag for ``checkout.js`` on the checkout
26+
page (and a ``link`` tag for ``checkout.css`` if you import any CSS).
27+
28+
Multiple Entries Per Page?
29+
--------------------------
30+
31+
Typically, you should include only *one* entry JavaScript per page. This means
32+
the checkout page will include ``checkout.js``, but will *not* include the
33+
``app.js`` that's used on the other pages. Think of the checkout page as its
34+
own "app", where ``checkout.js`` includes all the functionality you need.
35+
36+
However, if there is some global JavaScript that you want included on *every*
37+
page, you *can* create an entry that contains that code and include both that
38+
entry *and* your page-specific entry. For example, suppose that the ``app``
39+
entry above contains JavaScript you want on every page. In that case, include
40+
both ``app.js`` and ``checkout.js`` on the checkout page.
41+
42+
.. tip::
43+
44+
Be sure to create a :doc:`shared entry </shared-entry>` to avoid duplicating
45+
the Webpack bootstrap logic and any shared modules.

docs/shared-entry.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Creating a Shared Entry
2-
=======================
1+
Creating a Shared Commons Entry
2+
===============================
33

44
Suppose you have multiple entry files and *each* requires ``jquery``. In this
55
case, *each* output file will contain jQuery, slowing down your user's experience.

docs/simple-example.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,9 @@ In this case, ``main.js`` is being added to an entry called ``app`` in ``webpack
159159
160160
As soon as you require a CSS file, both an ``app.js`` **and** an ``app.css`` file
161161
will be created. You'll need to add a link tag to the ``app.css`` file in your
162-
templates.
162+
templates:
163+
164+
.. code-block:: diff
165+
166+
<link rel="stylesheet" href="{{ asset('build/global.css') }}">
167+
+ <link rel="stylesheet" href="{{ asset('build/app.css') }}">

0 commit comments

Comments
 (0)