You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/how-to/shim.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: How to Shim Third Party Libraries?
4
4
5
5
`webpack` as a module bundler can understand modules written as ES2015 modules, CommonJS or AMD. But many times, while using third party libraries, we see that they expect dependencies which are global aka `$` for `jquery`. They might also be creating global variables which need to be exported. Here we will see different ways to help webpack understand these __broken modules__.
6
6
7
-
###Prefer unminified src(CommonJs/AMD) files over bundled `dist` versions.
7
+
## Prefer unminified CommonJs/AMD files over bundled `dist` versions.
8
8
9
9
Most modules link the `dist` version in the `main` field of their `package.json`. While this is useful for most developers, for webpack it is better to alias the src version because this way webpack is able to optimize dependencies better (e.g. when using the [DedupePlugin](/concepts/plugins#DedupePlugin)). However in most cases `dist` works fine as well.
10
10
@@ -21,7 +21,7 @@ module.exports = {
21
21
};
22
22
```
23
23
24
-
###ProvidePlugin
24
+
## ProvidePlugin
25
25
The [ProvidePlugin](/concepts/plugins#ProvidePlugin) makes a module available as a variable in every other module required by `webpack`. The module is required only if you use the variable.
26
26
Most legacy modules rely on the presence of specific globals, like jQuery plugins do on `$` or `jQuery`. In this scenario, you can configure webpack to prepend `var $ = require(“jquery”)` everytime it encounters the global `$` identifier.
27
27
@@ -38,7 +38,7 @@ var webpack = require("webpack");
38
38
]
39
39
```
40
40
41
-
###`imports-loader`
41
+
## `imports-loader`
42
42
43
43
[`imports-loader`](https://github.com/webpack/imports-loader) inserts necessary globals into the required legacy module.
44
44
For example, Some legacy modules rely on `this` being the `window` object. This becomes a problem when the module is executed in a CommonJS context where `this` equals `module.exports`. In this case you can override `this` using the `imports-loader`.
@@ -67,7 +67,7 @@ module: {
67
67
}
68
68
```
69
69
70
-
###`exports-loader`
70
+
## `exports-loader`
71
71
72
72
Let's say a library creates a global variable that it expects it's consumers to use. In this case we can use [`exports-loader`](https://github.com/webpack/exports-loader), to export that global variable in CommonJS format.
73
73
@@ -85,7 +85,7 @@ module: {
85
85
}
86
86
```
87
87
88
-
###`scripts-loader`
88
+
## `scripts-loader`
89
89
90
90
The [scripts-loader](https://github.com/webpack/script-loader) evaluates code in the global context, just like you would add the code into a `script` tag. In this mode every normal library should work. require, module, etc. are undefined.
91
91
Note: The file is added as string to the bundle. It is not minimized by `webpack`, so use a minimized version. There is also no dev tool support for libraries added by this loader.
globalLegacyVariable() // This global variable will be added as a result of the script loader
99
99
```
100
100
101
-
###`noParse` option
101
+
## `noParse` option
102
102
103
103
When there is no AMD/CommonJS version of the module and you want to include the `dist`, you can flag this module as `noParse`. Then `webpack` will just include the module without parsing it, which can be used to improve the build time. This means that any feature requiring the AST, like the `ProvidePlugin`, will not work.
0 commit comments