11jQuery 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+
419Some legacy JavaScript applications use programming practices that don't play
520well with the new practices promoted by Webpack. The most common of these
621problems 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() ``
3146method 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+
4564If 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