Skip to content

Commit 6833bb6

Browse files
committed
finishing read-through
1 parent c2b36a2 commit 6833bb6

File tree

6 files changed

+50
-56
lines changed

6 files changed

+50
-56
lines changed

frontend/encore/bootstrap.rst

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,17 @@ Bootstrap JavaScript requires jQuery and Popper.js, so make sure you have this i
4141

4242
.. code-block:: terminal
4343
44-
$ yarn add jquery --dev
45-
$ yarn add popper.js --dev
44+
$ yarn add jquery popper.js --dev
4645
47-
Next, make sure to call ``.autoProvidejQuery()`` in your ``webpack.config.js`` file:
48-
49-
.. code-block:: diff
50-
51-
// webpack.config.js
52-
Encore
53-
// ...
54-
+ .autoProvidejQuery()
55-
;
56-
57-
This is needed because Bootstrap expects jQuery to be available as a global
58-
variable. Now, require bootstrap from any of your JavaScript files:
46+
Now, require bootstrap from any of your JavaScript files:
5947

6048
.. code-block:: javascript
6149
6250
// app.js
6351
6452
const $ = require('jquery');
65-
// JS is equivalent to the normal "bootstrap" package
66-
// no need to set this to a variable, just require it
53+
// this "modifies" the jquery module: adding behavior to it
54+
// the bootstrap module doesn't export/return anything
6755
require('bootstrap');
6856
6957
// or you can include specific pieces
@@ -74,8 +62,13 @@ variable. Now, require bootstrap from any of your JavaScript files:
7462
$('[data-toggle="popover"]').popover();
7563
});
7664
77-
Thanks to ``autoProvidejQuery()``, you can require any other jQuery
78-
plugins in a similar way:
65+
Using other Bootstrap / jQuery Plugins
66+
--------------------------------------
67+
68+
If you need to use jQuery plugins that work well with jQuery, you may need to use
69+
Encore's :ref:`autoProvidejQuery() <encore-autoprovide-jquery>` method so that
70+
these plugins know where to find jQuery. Then, you can include the needed JavaScript
71+
and CSS like normal:
7972

8073
.. code-block:: javascript
8174

frontend/encore/dev-server.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Using webpack-dev-server and HMR
22
================================
33

4-
While developing, instead of using ``encore dev --watch``, you can use the
4+
While developing, instead of using ``yarn encore dev --watch``, you can use the
55
`webpack-dev-server`_:
66

77
.. code-block:: terminal
88
9-
$ ./node_modules/.bin/encore dev-server
9+
$ yarn encore dev-server
1010
1111
This serves the built assets from a new server at ``http://localhost:8080`` (it does
1212
not actually write any files to disk). This means your ``script`` and ``link`` tags
@@ -22,7 +22,7 @@ by the normal `webpack-dev-server`_. For example:
2222

2323
.. code-block:: terminal
2424
25-
$ ./node_modules/.bin/encore dev-server --https --port 9000
25+
$ yarn encore dev-server --https --port 9000
2626
2727
This will start a server at ``https://localhost:9000``.
2828

frontend/encore/legacy-apps.rst

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
jQuery and Legacy Applications
2-
==============================
1+
jQuery Plugins and Legacy Applications
2+
======================================
33

44
Inside Webpack, when you require a module, it does *not* (usually) set a global variable.
55
Instead, it just returns a value:
@@ -10,30 +10,25 @@ Instead, it just returns a value:
1010
const $ = require('jquery');
1111
1212
In practice, this will cause problems with some outside libraries that *rely* on
13-
jQuery to be global. It will be a problem if some of *your* JavaScript isn't being
14-
processed through Webpack (e.g. you have some JavaScript in your templates).
15-
16-
Using Libraries that Expect jQuery to be Global
17-
-----------------------------------------------
18-
19-
Some legacy JavaScript applications use programming practices that don't play
20-
well with the new practices promoted by Webpack. The most common of these
21-
problems is using code (e.g. jQuery plugins) that assume that jQuery is already
22-
available via the ``$`` or ``jQuery`` global variables. If those variables
23-
are not defined, you'll get these errors:
13+
jQuery to be global *or* if *your* JavaScript isn't being processed through Webpack
14+
(e.g. you have some JavaScript in your templates) and you need jQuery. Both will
15+
cause similar errors:
2416

2517
.. code-block:: text
2618
2719
Uncaught ReferenceError: $ is not defined at [...]
2820
Uncaught ReferenceError: jQuery is not defined at [...]
2921
30-
Instead of rewriting everything, Encore allows for a different solution. Thanks
31-
to the ``autoProvidejQuery()`` method, whenever a JavaScript file uses the ``$``
32-
or ``jQuery`` variables, Webpack automatically requires ``jquery`` and creates
33-
those variables for you.
22+
The fix depends on what code is causing the problem.
23+
24+
.. _encore-autoprovide-jquery:
3425

35-
So, when working with legacy applications, you may need to add the following to
36-
``webpack.config.js``:
26+
Fixing jQuery Plugins that Expect jQuery to be Global
27+
-----------------------------------------------------
28+
29+
jQuery plugins often expect that jQuery is already available via the ``$`` or
30+
``jQuery`` global variables. To fix this, call ``autoProvidejQuery()`` from your
31+
``webpack.config.js`` file:
3732

3833
.. code-block:: diff
3934
@@ -42,6 +37,10 @@ So, when working with legacy applications, you may need to add the following to
4237
+ .autoProvidejQuery()
4338
;
4439
40+
After restarting Encore, Webpack will look for all uninitialized ``$`` and ``jQuery``
41+
variables and automatically require ``jquery`` and set those variables for you.
42+
It "rewrites" the "bad" code to be correct.
43+
4544
Internally, this ``autoProvidejQuery()`` method calls the ``autoProvideVariables()``
4645
method from Encore. In practice, it's equivalent to doing:
4746

@@ -61,25 +60,27 @@ method from Encore. In practice, it's equivalent to doing:
6160
Accessing jQuery from outside of Webpack JavaScript Files
6261
---------------------------------------------------------
6362

64-
If you also need to provide access to ``$`` and ``jQuery`` variables outside of
63+
If *your* code needs access to ``$`` or ``jQuery`` and you are inside of a file
64+
that's processed by Webpack/Encore, you should remove any "$ is not defined" errors
65+
by requiring jQuery: ``var $ = require('jquery')``.
66+
67+
But if you also need to provide access to ``$`` and ``jQuery`` variables outside of
6568
JavaScript files processed by Webpack (e.g. JavaScript that still lives in your
6669
templates), you need to manually set these as global variables in some JavaScript
6770
file that is loaded before your legacy code.
6871

69-
For example, you could define a ``common.js`` file that's processed by Webpack and
70-
loaded on every page with the following content:
72+
For example, in your ``app.js`` file that's processed by Webpack and loaded on every
73+
page, add:
7174

72-
.. code-block:: javascript
75+
.. code-block:: diff
7376
7477
// require jQuery normally
7578
const $ = require('jquery');
7679
77-
// create global $ and jQuery variables
78-
global.$ = global.jQuery = $;
79-
80-
.. tip::
80+
+ // create global $ and jQuery variables
81+
+ global.$ = global.jQuery = $;
8182
82-
The ``global`` variable is a special way of setting things in the ``window``
83-
variable. In a web context, using ``global`` and ``window`` are equivalent,
84-
except that ``window.jQuery`` won't work when using ``autoProvidejQuery()``.
85-
In other words, use ``global``.
83+
The ``global`` variable is a special way of setting things in the ``window``
84+
variable. In a web context, using ``global`` and ``window`` are equivalent,
85+
except that ``window.jQuery`` won't work when using ``autoProvidejQuery()``.
86+
In other words, use ``global``.

frontend/encore/page-specific-assets.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ page-specific JavaScript and CSS file from a page-specific entry (e.g. ``checkou
2323

2424
.. tip::
2525

26-
Be sure to use split chunks :doc:`shared entry </frontend/encore/split-chunks>`
26+
Be sure to use :doc:`split chunks </frontend/encore/split-chunks>`
2727
to avoid duplicating and shared code between your entry files.

frontend/encore/server-data.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Fetch this in JavaScript:
1919
document.addEventListener('DOMContentLoaded', function() {
2020
var userRating = document.querySelector('.js-user-rating');
2121
var isAuthenticated = userRating.dataset.isAuthenticated;
22+
23+
// or with jQuery
24+
//var isAuthenticated = $('.js-user-rating').data('isAuthenticated');
2225
});
2326
2427
.. note::

frontend/encore/split-chunks.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,4 @@ this plugin with the ``configureSplitChunks()`` function:
6363
+ splitChunks.minSize = 0;
6464
+ }
6565
66-
- talk about SplitChunksPlugin & how you talk to it in Encore
67-
- also look at the runtime chunk stuff
68-
6966
.. _`SplitChunksPlugin from Webpack`: https://webpack.js.org/plugins/split-chunks-plugin/

0 commit comments

Comments
 (0)