1- jQuery and Legacy Applications
2- ==============================
1+ jQuery Plugins and Legacy Applications
2+ ======================================
33
44Inside Webpack, when you require a module, it does *not * (usually) set a global variable.
55Instead, 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+
4544Internally, this ``autoProvidejQuery() `` method calls the ``autoProvideVariables() ``
4645method 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
6568JavaScript files processed by Webpack (e.g. JavaScript that still lives in your
6669templates), you need to manually set these as global variables in some JavaScript
6770file 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 ``.
0 commit comments