Skip to content

Commit 099d928

Browse files
committed
docs(config): improve formatting and clarify the string usage in externals
Resolves #1726
1 parent f0b140a commit 099d928

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/content/configuration/externals.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,50 @@ __Prevent bundling__ of certain `import`ed packages and instead retrieve these *
2020

2121
For example, to include [jQuery](https://jquery.com/) from a CDN instead of bundling it:
2222

23-
**index.html**
23+
__index.html__
2424

25-
```html
26-
...
27-
<script src="https://code.jquery.com/jquery-3.1.0.js"
25+
``` html
26+
<script
27+
src="https://code.jquery.com/jquery-3.1.0.js"
2828
integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
29-
crossorigin="anonymous"></script>
30-
...
29+
crossorigin="anonymous">
30+
</script>
3131
```
3232

33-
**webpack.config.js**
33+
__webpack.config.js__
3434

35-
```javascript
35+
``` js
3636
externals: {
3737
jquery: 'jQuery'
3838
}
3939
```
4040

4141
This leaves any dependent modules unchanged, i.e. the code shown below will still work:
4242

43-
```javascript
43+
``` js
4444
import $ from 'jquery';
4545

4646
$('.my-element').animate(...);
4747
```
4848

4949
The bundle with external dependencies can be used in various module contexts, such as [CommonJS, AMD, global and ES2015 modules](/concepts/modules). The external library may be available in any of these forms:
5050

51-
* __root__ - An external library can be available as a global variable. The consumer can achieve this by including the external library in a script tag. This is the default setting for externals.
52-
* __commonjs__ - The consumer application may be using a CommonJS module system and hence the external library should be available as a CommonJS module.
53-
* __commonjs2__ - Similar to the above line but where the export is `module.exports.default`.
54-
* __amd__ - Similar to the above line but using AMD module system.
51+
- __root__: The library should be available as a global variable (e.g. via a script tag).
52+
- __commonjs__: The library should be available as a CommonJS module.
53+
- __commonjs2__: Similar to the above but where the export is `module.exports.default`.
54+
- __amd__: Similar to `commonjs` but using AMD module system.
5555

56-
`externals` accepts various syntax and interprets them in different manners.
56+
The following syntaxes are accepted and interepreted in various different ways.
5757

5858

5959
### string
6060

61-
`jQuery` in the externals indicates that your bundle will need `jQuery` variable in the global form.
61+
See the example above. The property name `jquery` indicates that the module `jquery` in `import $ from 'jquery'` should be excluded. In order to replace this module, the value `jQuery` will be used to retrieve a global `jQuery` variable. In other words, when a string is provided it will be treated as `root` (defined above and below).
6262

6363

6464
### array
6565

66-
```javascript
66+
``` js
6767
externals: {
6868
subtract: ['./math', 'subtract']
6969
}
@@ -74,7 +74,7 @@ externals: {
7474

7575
### object
7676

77-
```javascript
77+
``` js
7878
externals : {
7979
react: 'react'
8080
}
@@ -103,11 +103,11 @@ This syntax is used to describe all the possible ways that an external library c
103103

104104
### function
105105

106-
It might be useful to define your own function to control the behavior of what you want to externalize from webpack. [webpack-node-externals](https://www.npmjs.com/package/webpack-node-externals), for example, excludes all modules from the node_modules and provides some options to, for example, whitelist packages.
106+
It might be useful to define your own function to control the behavior of what you want to externalize from webpack. [webpack-node-externals](https://www.npmjs.com/package/webpack-node-externals), for example, excludes all modules from the `node_modules` directory and provides some options to, for example, whitelist packages.
107107

108108
It basically comes down to this:
109109

110-
```javascript
110+
``` js
111111
externals: [
112112
function(context, request, callback) {
113113
if (/^yourregex$/.test(request)){
@@ -125,7 +125,7 @@ The `'commonjs ' + request` defines the type of module that needs to be external
125125

126126
Every dependency that matches the given regular expression will be excluded from the output bundles.
127127

128-
```javascript
128+
``` js
129129
externals: /^(jquery|\$)$/i
130130
```
131131

0 commit comments

Comments
 (0)