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
{{ message }}
This repository was archived by the owner on Sep 9, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+87-43
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ And run `webpack` via your preferred method.
71
71
Type: `Boolean`
72
72
Default: `false`
73
73
74
-
Require a fallback for non-worker supporting environments
74
+
Require a fallback for non-worker supporting environments.
75
75
76
76
**webpack.config.js**
77
77
@@ -94,7 +94,7 @@ module.exports = {
94
94
Type: `Boolean`
95
95
Default: `false`
96
96
97
-
You can also inline the worker as a BLOB with the `inline` parameter
97
+
You can also inline the worker as a BLOB with the `inline` parameter.
98
98
99
99
**webpack.config.js**
100
100
@@ -114,53 +114,77 @@ module.exports = {
114
114
115
115
_Note: Inline mode will also create chunks for browsers without support for inline workers, to disable this behavior just set `fallback` parameter as `false`._
116
116
117
+
**webpack.config.js**
118
+
117
119
```js
118
-
// webpack.config.js
119
-
{
120
-
loader:'worker-loader',
121
-
options: { inline:true, fallback:false }
122
-
}
120
+
module.exports= {
121
+
module: {
122
+
rules: [
123
+
{
124
+
loader:'worker-loader',
125
+
options: { inline:true, fallback:false },
126
+
},
127
+
],
128
+
},
129
+
};
123
130
```
124
131
125
132
### `name`
126
133
127
134
Type: `String`
128
135
Default: `[hash].worker.js`
129
136
130
-
To set a custom name for the output script, use the `name` parameter. The name
131
-
may contain the string `[hash]`, which will be replaced with a content dependent
132
-
hash for caching purposes. When using `name` alone `[hash]` is omitted.
137
+
To set a custom name for the output script, use the `name` parameter.
138
+
The name may contain the string `[hash]`, which will be replaced with a content dependent hash for caching purposes.
139
+
When using `name` alone `[hash]` is omitted.
140
+
141
+
**webpack.config.js**
133
142
134
143
```js
135
-
// webpack.config.js
136
-
{
137
-
loader:'worker-loader',
138
-
options: { name:'WorkerName.[hash].js' }
139
-
}
144
+
module.exports= {
145
+
module: {
146
+
rules: [
147
+
{
148
+
loader:'worker-loader',
149
+
options: { name:'WorkerName.[hash].js' },
150
+
},
151
+
],
152
+
},
153
+
};
140
154
```
141
155
142
156
### publicPath
143
157
144
158
Type: `String`
145
159
Default: `null`
146
160
147
-
Overrides the path from which worker scripts are downloaded. If not specified,
148
-
the same public path used for other webpack assets is used.
161
+
Overrides the path from which worker scripts are downloaded.
162
+
If not specified, the same public path used for other webpack assets is used.
163
+
164
+
**webpack.config.js**
149
165
150
166
```js
151
-
// webpack.config.js
152
-
{
153
-
loader:'worker-loader',
154
-
options: { publicPath:'/scripts/workers/' }
155
-
}
167
+
module.exports= {
168
+
module: {
169
+
rules: [
170
+
{
171
+
loader:'worker-loader',
172
+
options: { publicPath:'/scripts/workers/' },
173
+
},
174
+
],
175
+
},
176
+
};
156
177
```
157
178
158
179
## Examples
159
180
181
+
### Basic
182
+
160
183
The worker file can import dependencies just like any other file:
[`WebWorkers`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) are restricted by a [same-origin policy](https://en.wikipedia.org/wiki/Same-origin_policy), so if your `webpack` assets are not being served from the same origin as your application, their download may be blocked by your browser.
237
265
This scenario can commonly occur if you are hosting your assets under a CDN domain.
238
-
Even downloads from the `webpack-dev-server` could be blocked. There are two workarounds:
266
+
Even downloads from the `webpack-dev-server` could be blocked.
267
+
There are two workarounds:
239
268
240
269
Firstly, you can inline the worker as a blob instead of downloading it as an external script via the [`inline`](#inline) parameter
241
270
271
+
**App.js**
272
+
242
273
```js
243
-
// App.js
244
274
importWorkerfrom'./file.worker.js';
245
275
```
246
276
277
+
**webpack.config.js**
278
+
247
279
```js
248
-
// webpack.config.js
249
-
{
250
-
loader:'worker-loader',
251
-
options: { inline:true }
252
-
}
280
+
module.exports= {
281
+
module: {
282
+
rules: [
283
+
{
284
+
loader:'worker-loader',
285
+
options: { inline:true },
286
+
},
287
+
],
288
+
},
289
+
};
253
290
```
254
291
255
-
Secondly, you may override the base download URL for your worker script via the
256
-
[`publicPath`](#publicpath) option
292
+
Secondly, you may override the base download URL for your worker script via the [`publicPath`](#publicpath) option
293
+
294
+
**App.js**
257
295
258
296
```js
259
-
// App.js
260
297
// This will cause the worker to be downloaded from `/workers/file.worker.js`
0 commit comments