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: README.md
+27-23Lines changed: 27 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,7 +59,7 @@ All scripts set to `async` except 'first.js' which is sync:
59
59
plugins: [
60
60
newHtmlWebpackPlugin(),
61
61
newScriptExtHtmlWebpackPlugin({
62
-
sync:['first.js'],
62
+
sync:'first.js',
63
63
defaultAttribute:'async'
64
64
})
65
65
]
@@ -70,27 +70,31 @@ Configuration offers much more complex options:
70
70
Configuration
71
71
-------------
72
72
You must pass a hash of configuration options to the plugin to cause the addition of attributes:
73
-
-`inline`: array of `String`'s and/or `RegExp`'s defining scripts that should be inlined in the html (default: `[]`);
74
-
-`sync`: array of `String`'s and/or `RegExp`'s defining script names that should have no attribute (default: `[]`);
75
-
-`async`: array of `String`'s and/or `RegExp`'s defining script names that should have an `async` attribute (default: `[]`);
76
-
-`defer`: array of `String`'s and/or `RegExp`'s defining script names that should have a `defer` attribute (default: `[]`);
73
+
-`inline`: a __script matching pattern__ defining scripts that should be inlined in the html (default: `[]`);
74
+
-`sync`: a __script matching pattern__ defining script names that should have no attribute (default: `[]`);
75
+
-`async`: a __script matching pattern__ defining script names that should have an `async` attribute (default: `[]`);
76
+
-`defer`: a __script matching pattern__ defining script names that should have a `defer` attribute (default: `[]`);
77
77
-`defaultAttribute`: `'sync' | 'async' | 'defer'` The default attribute to set - `'sync'` actually results in no attribute (default: `'sync'`);
78
-
-`module`: array of `String`'s and/or `RegExp`'s defining script names that should have a
78
+
-`module`: a __script matching pattern__ defining script names that should have a
79
79
`type="module"` attribute (default: `[]`);
80
-
-`preload`: array of `String`'s and/or `RegExp`'s defining scripts that should have accompanying preload resource hints (default: `[]`);
81
-
-`prefetch`: array of `String`'s and/or `RegExp`'s defining scripts that should have accompanying prefetch resource hints (default: `[]`);
80
+
-`preload`: a __script matching pattern__ defining scripts that should have accompanying preload resource hints (default: `[]`);
81
+
-`prefetch`: a __script matching pattern__ defining scripts that should have accompanying prefetch resource hints (default: `[]`);
82
82
83
-
In the arrays a `String` value matches if it is a substring of the script name.
83
+
A __script matching pattern__ matches against a script's name. It can be one of:
84
+
- a `String`- matches if it is a substring of the script name;
85
+
- a `RegExp`;
86
+
- an array of `String`'s and/or `RegExp`'s - matches if any one element matches;
87
+
- a hash with property `test` with a value of one of the above.
84
88
85
89
In more complicated use cases it may prove difficult to ensure that the pattern matching for different attributes are mutually exclusive. To prevent confusion, the plugin operates a simple precedence model:
86
90
87
-
1. if a script name matches a `RegEx` or `String` from the`inline`option, it will be inlined;
91
+
1. if a script name matches the`inline`script matching pattern, it will be inlined;
88
92
89
-
2. if a script name matches a `Regex` or `String` from the `sync`option, it will have no attribute, *unless* it matched condition 1;
93
+
2. if a script name matches the `sync`script matching pattern, it will have no attribute, *unless* it matched condition 1;
90
94
91
-
3. if a script name matches a `Regex` or `String` from the `async`option, it will have the `async` attribute, *unless* it matched conditions 1 or 2;
95
+
3. if a script name the `async`script matching pattern, it will have the `async` attribute, *unless* it matched conditions 1 or 2;
92
96
93
-
4. if a script name matches a `Regex` or `String` from the `defer`option, it will have the `defer` attribute, *unless* it matched conditions 1, 2 or 3;
97
+
4. if a script name matches the `defer`script matching pattern, it will have the `defer` attribute, *unless* it matched conditions 1, 2 or 3;
94
98
95
99
5. if a script name does not match any of the previous conditions, it will have the `defaultAttribute' attribute.
96
100
@@ -104,7 +108,7 @@ All scripts with 'important' in their name are sync and all others set to `defer
104
108
plugins: [
105
109
newHtmlWebpackPlugin(),
106
110
newScriptExtHtmlWebpackPlugin({
107
-
sync:['important'],
111
+
sync:'important'
108
112
defaultAttribute:'defer'
109
113
})
110
114
]
@@ -115,7 +119,7 @@ Alternatively, using a regular expression:
115
119
plugins: [
116
120
newHtmlWebpackPlugin(),
117
121
newScriptExtHtmlWebpackPlugin({
118
-
sync:[/important/],
122
+
sync:/important/,
119
123
defaultAttribute:'defer'
120
124
})
121
125
]
@@ -126,8 +130,8 @@ All scripts with 'mod' in their name are async and type 'module', all others are
126
130
plugins: [
127
131
newHtmlWebpackPlugin(),
128
132
newScriptExtHtmlWebpackPlugin({
129
-
async:['mod'],
130
-
module:['mod']
133
+
async:'mod',
134
+
module:'mod'
131
135
})
132
136
]
133
137
```
@@ -137,25 +141,24 @@ Script 'startup.js' is inlined whilst all other scripts are async and preloaded:
137
141
plugins: [
138
142
newHtmlWebpackPlugin(),
139
143
newScriptExtHtmlWebpackPlugin({
140
-
inline:['startup'],
141
-
preload:[/\.js$/],
144
+
inline:'startup',
145
+
preload:/\.js$/,
142
146
defaultAttribute:'async'
143
147
})
144
148
]
145
149
```
146
150
147
151
148
-
149
152
And so on, to craziness:
150
153
```javascript
151
154
plugins: [
152
155
newHtmlWebpackPlugin(),
153
156
newScriptExtHtmlWebpackPlugin({
154
-
inline:['startup'],
157
+
inline:'startup',
155
158
sync: [/imp(1|2){1,3}}/, 'initial'],
156
159
defer: ['slow',/big.*andslow/],
157
160
module: [/^((?!sync).)*/, 'mod'],
158
-
prefetch:['indirectly-referenced.js'],
161
+
prefetch:'indirectly-referenced.js',
159
162
defaultAttribute:'async'
160
163
})
161
164
]
@@ -229,7 +232,8 @@ Change History
229
232
230
233
v1.5.x
231
234
* added resource hints
232
-
* works with webpack 2.2.0
235
+
* works with webpack 2.2.1
236
+
* enhanced API (no need to use array), fully backwardly compatible
233
237
234
238
v1.4.x
235
239
* updated internal mechanism to use new(ish) [HtmlWebpackPlugin event](https://github.com/ampedandwired/html-webpack-plugin#events)
0 commit comments