Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a099bff

Browse filesBrowse files
committed
Documenting (better) how to expose global variables - see #8071
1 parent 8b0a22a commit a099bff
Copy full SHA for a099bff

File tree

1 file changed

+38
-6
lines changed
Filter options

1 file changed

+38
-6
lines changed

‎frontend/encore/legacy-apps.rst

Copy file name to clipboardExpand all lines: frontend/encore/legacy-apps.rst
+38-6Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
jQuery and Legacy Applications
22
==============================
33

4+
Inside Webpack, when you require a module, it does *not* (usually) set a global variable.
5+
Instead, it just returns a value:
6+
7+
.. code-block:: javascript
8+
9+
// this loads jquery, but does *not* set a global $ or jQuery variable
10+
const $ = require('jquery');
11+
12+
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+
419
Some legacy JavaScript applications use programming practices that don't play
520
well with the new practices promoted by Webpack. The most common of these
621
problems is using code (e.g. jQuery plugins) that assume that jQuery is already
@@ -27,7 +42,7 @@ So, when working with legacy applications, you may need to add the following to
2742
+ .autoProvidejQuery()
2843
;
2944
30-
Internally, this ``autoProvidejQuery()`` method uses the ``autoProvideVariables()``
45+
Internally, this ``autoProvidejQuery()`` method calls the ``autoProvideVariables()``
3146
method from Encore. In practice, it's equivalent to doing:
3247

3348
.. code-block:: javascript
@@ -38,16 +53,33 @@ method from Encore. In practice, it's equivalent to doing:
3853
.autoProvideVariables({
3954
$: 'jquery',
4055
jQuery: 'jquery'
56+
'window.jQuery': 'jquery',
4157
})
4258
// ...
4359
;
4460
61+
Accessing jQuery from outside of Webpack JavaScript Files
62+
---------------------------------------------------------
63+
4564
If you also need to provide access to ``$`` and ``jQuery`` variables outside of
46-
JavaScript files processed by Webpack, you must create the global variables
47-
yourself in some file loaded before the legacy JavaScript code. For example, you
48-
can define a ``common.js`` file processed by Webpack and loaded in every page
49-
with the following content:
65+
JavaScript files processed by Webpack (e.g. JavaScript that still lives in your
66+
templates), you need to manually set these as global variables in some JavaScript
67+
file that is loaded before your legacy code.
68+
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:
5071

5172
.. code-block:: javascript
5273
53-
window.$ = window.jQuery = require('jquery');
74+
// require jQuery normally
75+
const $ = require('jquery');
76+
77+
// create global $ and jQuery variables
78+
global.$ = global.jQuery = $;
79+
80+
.. tip::
81+
82+
The ``global`` variable is a special way of setting things pn 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``.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.