1
- jQuery and Legacy Applications
2
- ==============================
1
+ jQuery Plugins and Legacy Applications
2
+ ======================================
3
3
4
4
Inside Webpack, when you require a module, it does *not * (usually) set a global variable.
5
5
Instead, it just returns a value:
@@ -10,30 +10,25 @@ Instead, it just returns a value:
10
10
const $ = require (' jquery' );
11
11
12
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
-
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:
24
16
25
17
.. code-block :: text
26
18
27
19
Uncaught ReferenceError: $ is not defined at [...]
28
20
Uncaught ReferenceError: jQuery is not defined at [...]
29
21
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 :
34
25
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:
37
32
38
33
.. code-block :: diff
39
34
@@ -42,6 +37,10 @@ So, when working with legacy applications, you may need to add the following to
42
37
+ .autoProvidejQuery()
43
38
;
44
39
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
+
45
44
Internally, this ``autoProvidejQuery() `` method calls the ``autoProvideVariables() ``
46
45
method from Encore. In practice, it's equivalent to doing:
47
46
@@ -61,25 +60,27 @@ method from Encore. In practice, it's equivalent to doing:
61
60
Accessing jQuery from outside of Webpack JavaScript Files
62
61
---------------------------------------------------------
63
62
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
65
68
JavaScript files processed by Webpack (e.g. JavaScript that still lives in your
66
69
templates), you need to manually set these as global variables in some JavaScript
67
70
file that is loaded before your legacy code.
68
71
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 :
71
74
72
- .. code-block :: javascript
75
+ .. code-block :: diff
73
76
74
77
// require jQuery normally
75
78
const $ = require('jquery');
76
79
77
- // create global $ and jQuery variables
78
- global .$ = global .jQuery = $;
79
-
80
- .. tip ::
80
+ + // create global $ and jQuery variables
81
+ + global.$ = global.jQuery = $;
81
82
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