Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 22d48e4

Browse files
committedJun 15, 2020
docs: improve
1 parent 3500f14 commit 22d48e4

File tree

1 file changed

+87
-43
lines changed

1 file changed

+87
-43
lines changed
 

‎README.md

+87-43
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ And run `webpack` via your preferred method.
7171
Type: `Boolean`
7272
Default: `false`
7373

74-
Require a fallback for non-worker supporting environments
74+
Require a fallback for non-worker supporting environments.
7575

7676
**webpack.config.js**
7777

@@ -94,7 +94,7 @@ module.exports = {
9494
Type: `Boolean`
9595
Default: `false`
9696

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.
9898

9999
**webpack.config.js**
100100

@@ -114,53 +114,77 @@ module.exports = {
114114

115115
_Note: Inline mode will also create chunks for browsers without support for inline workers, to disable this behavior just set `fallback` parameter as `false`._
116116

117+
**webpack.config.js**
118+
117119
```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+
};
123130
```
124131

125132
### `name`
126133

127134
Type: `String`
128135
Default: `[hash].worker.js`
129136

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**
133142

134143
```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+
};
140154
```
141155

142156
### publicPath
143157

144158
Type: `String`
145159
Default: `null`
146160

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**
149165

150166
```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+
};
156177
```
157178

158179
## Examples
159180

181+
### Basic
182+
160183
The worker file can import dependencies just like any other file:
161184

185+
**Worker.js**
186+
162187
```js
163-
// Worker.js
164188
const _ = require('lodash');
165189

166190
const obj = { foo: 'foo' };
@@ -174,12 +198,13 @@ self.postMessage({ foo: 'foo' });
174198
self.addEventListener('message', (event) => console.log(event));
175199
```
176200

177-
### Integrating with ES2015 Modules
201+
### Integrating with ES Modules
178202

179203
_Note: You can even use ES2015 Modules if you have the [`babel-loader`](https://github.com/babel/babel-loader) configured._
180204

205+
**Worker.js**
206+
181207
```js
182-
// Worker.js
183208
import _ from 'lodash';
184209

185210
const obj = { foo: 'foo' };
@@ -197,8 +222,9 @@ self.addEventListener('message', (event) => console.log(event));
197222

198223
To integrate with TypeScript, you will need to define a custom module for the exports of your worker
199224

225+
**typings/worker-loader.d.ts**
226+
200227
```typescript
201-
// typings/custom.d.ts
202228
declare module 'worker-loader!*' {
203229
class WebpackWorker extends Worker {
204230
constructor();
@@ -208,8 +234,9 @@ declare module 'worker-loader!*' {
208234
}
209235
```
210236

237+
**Worker.ts**
238+
211239
```typescript
212-
// Worker.ts
213240
const ctx: Worker = self as any;
214241

215242
// Post data to parent thread
@@ -219,8 +246,9 @@ ctx.postMessage({ foo: 'foo' });
219246
ctx.addEventListener('message', (event) => console.log(event));
220247
```
221248

249+
**App.ts**
250+
222251
```typescript
223-
// App.ts
224252
import Worker from 'worker-loader!./Worker';
225253

226254
const worker = new Worker();
@@ -235,38 +263,54 @@ worker.addEventListener('message', (event) => {});
235263

236264
[`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.
237265
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:
239268

240269
Firstly, you can inline the worker as a blob instead of downloading it as an external script via the [`inline`](#inline) parameter
241270

271+
**App.js**
272+
242273
```js
243-
// App.js
244274
import Worker from './file.worker.js';
245275
```
246276

277+
**webpack.config.js**
278+
247279
```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+
};
253290
```
254291

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**
257295

258296
```js
259-
// App.js
260297
// This will cause the worker to be downloaded from `/workers/file.worker.js`
261298
import Worker from './file.worker.js';
262299
```
263300

301+
**webpack.config.js**
302+
264303
```js
265-
// webpack.config.js
266-
{
267-
loader: 'worker-loader',
268-
options: { publicPath: '/workers/' }
269-
}
304+
module.exports = {
305+
module: {
306+
rules: [
307+
{
308+
loader: 'worker-loader',
309+
options: { publicPath: '/workers/' },
310+
},
311+
],
312+
},
313+
};
270314
```
271315

272316
## Contributing

0 commit comments

Comments
 (0)
This repository has been archived.