1
1
jQuery and Legacy Applications
2
2
==============================
3
3
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
+
4
19
Some legacy JavaScript applications use programming practices that don't play
5
20
well with the new practices promoted by Webpack. The most common of these
6
21
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
27
42
+ .autoProvidejQuery()
28
43
;
29
44
30
- Internally, this ``autoProvidejQuery() `` method uses the ``autoProvideVariables() ``
45
+ Internally, this ``autoProvidejQuery() `` method calls the ``autoProvideVariables() ``
31
46
method from Encore. In practice, it's equivalent to doing:
32
47
33
48
.. code-block :: javascript
@@ -38,16 +53,33 @@ method from Encore. In practice, it's equivalent to doing:
38
53
.autoProvideVariables ({
39
54
$: ' jquery' ,
40
55
jQuery: ' jquery'
56
+ ' window.jQuery' : ' jquery' ,
41
57
})
42
58
// ...
43
59
;
44
60
61
+ Accessing jQuery from outside of Webpack JavaScript Files
62
+ ---------------------------------------------------------
63
+
45
64
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:
50
71
51
72
.. code-block :: javascript
52
73
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