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/guides/shimming.md
+20-19Lines changed: 20 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@ contributors:
7
7
- simon04
8
8
---
9
9
10
-
`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__.
## Prefer unminified CommonJS/AMD files over bundled `dist`versions.
12
+
## 未压缩的 CommonJS/AMD 文件优先于 `dist`打包版本。
13
13
14
-
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. However in most cases `dist`works fine as well.
The [`provide-plugin`](/plugins/provide-plugin)makes a module available as a variable in every other module required by `webpack`. The module is required only if you use the variable.
31
-
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”)` every time it encounters the global `$` identifier.
30
+
The [`provide-plugin`](/plugins/provide-plugin)可以将模块作为一个变量,被 `webpack` 在其他每个模块中引用。只有你需要使用此变量的时候,这个模块才会被 require 进来。
[`imports-loader`](/loaders/imports-loader/)inserts necessary globals into the required legacy module.
47
-
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`.
There are modules that support different [module styles](/concepts/modules), like AMD, CommonJS and legacy. However, most of the time they first check for `define` and then use some quirky code to export properties. In these cases, it could help to force the CommonJS path by setting `define = false`:
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`](/loaders/exports-loader/), to export that global variable in CommonJS format. For instance, in order to export `file`as`file`and`helpers.parse`as`parse`:
The [script-loader](/loaders/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.
97
96
98
-
W> 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.
When there is no AMD/CommonJS version of the module and you want to include the `dist`, you can flag this module as [`noParse`](/configuration/module/#module-noparse). Then `webpack`will just include the module without parsing it, which can be used to improve the build time.
0 commit comments