Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![deps][deps]][deps-url]
  4. [![test][test]][test-url]
  5. [![coverage][cover]][cover-url]
  6. [![chat][chat]][chat-url]
  7. <div align="center">
  8. <a href="https://github.com/webpack/webpack">
  9. <img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
  10. </a>
  11. <h1>Worker Loader</h1>
  12. <p>This loader registers the script as <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Worker</a><p>
  13. </div>
  14. <h2 align="center">Install</h2>
  15. ```bash
  16. npm i -D worker-loader
  17. ```
  18. <h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2>
  19. ### `Inlined`
  20. **App.js**
  21. ```js
  22. import Worker from 'worker-loader!./Worker.js';
  23. ```
  24. ### `Config`
  25. **webpack.config.js**
  26. ```js
  27. {
  28. module: {
  29. rules: [
  30. {
  31. test: /\.worker\.js$/,
  32. use: { loader: 'worker-loader' }
  33. }
  34. ]
  35. }
  36. }
  37. ```
  38. **App.js**
  39. ```js
  40. import Worker from './file.worker.js';
  41. const worker = new Worker();
  42. worker.postMessage({ a: 1 });
  43. worker.onmessage = function (event) {};
  44. worker.addEventListener("message", function (event) {});
  45. ```
  46. <h2 align="center">Options</h2>
  47. |Name|Type|Default|Description|
  48. |:--:|:--:|:-----:|:----------|
  49. |[**`name`**](#name)|`{String}`|`[hash].worker.js`|Set a custom name for the output script|
  50. |[**`inline`**](#inline)|`{Boolean}`|`false`|Inline the worker as a BLOB|
  51. |[**`fallback`**](#fallback)|`{Boolean}`|`false`|Require a fallback for non-worker supporting environments|
  52. |[**`publicPath`**](#publicPath)|`{String}`|`null`|Override the path from which worker scripts are downloaded|
  53. ### `name`
  54. To set a custom name for the output script, use the `name` parameter. The name may contain the string `[hash]`, which will be replaced with a content dependent hash for caching purposes. When using `name` alone `[hash]` is omitted.
  55. *webpack.config.js**
  56. ```js
  57. {
  58. loader: 'worker-loader',
  59. options: { name: 'WorkerName.[hash].js' }
  60. }
  61. ```
  62. ### `inline`
  63. You can also inline the worker as a BLOB with the `inline` parameter
  64. **webpack.config.js**
  65. ```js
  66. {
  67. loader: 'worker-loader',
  68. options: { inline: true }
  69. }
  70. ```
  71. > ℹ️ Inline mode will also create chunks for browsers without support for inline workers, to disable this behavior just set `fallback` parameter as `false`
  72. **webpack.config.js**
  73. ```js
  74. {
  75. loader: 'worker-loader'
  76. options: { inline: true, fallback: false }
  77. }
  78. ```
  79. ### `fallback`
  80. Require a fallback for non-worker supporting environments
  81. **webpack.config.js**
  82. ```js
  83. {
  84. loader: 'worker-loader'
  85. options: { fallback: false }
  86. }
  87. ```
  88. ### `publicPath`
  89. Overrides the path from which worker scripts are downloaded. If not specified, the same public path used for other
  90. webpack assets is used
  91. **webpack.config.js**
  92. ```js
  93. {
  94. loader: 'worker-loader'
  95. options: { publicPath: '/scripts/workers/' }
  96. }
  97. ```
  98. <h2 align="center">Examples</h2>
  99. The worker file can import dependencies just like any other file
  100. **Worker.js**
  101. ```js
  102. const _ = require('lodash')
  103. const obj = { foo: 'foo' }
  104. _.has(obj, 'foo')
  105. // Post data to parent thread
  106. self.postMessage({ foo: 'foo' })
  107. // Respond to message from parent thread
  108. self.addEventListener('message', (event) => console.log(event))
  109. ```
  110. ### `Integrating with ES2015 Modules`
  111. > ℹ️ You can even use ES2015 Modules if you have the [`babel-loader`](https://github.com/babel/babel-loader) configured.
  112. **Worker.js**
  113. ```js
  114. import _ from 'lodash'
  115. const obj = { foo: 'foo' }
  116. _.has(obj, 'foo')
  117. // Post data to parent thread
  118. self.postMessage({ foo: 'foo' })
  119. // Respond to message from parent thread
  120. self.addEventListener('message', (event) => console.log(event))
  121. ```
  122. ### `Integrating with TypeScript`
  123. To integrate with TypeScript, you will need to define a custom module for the exports of your worker
  124. **typings/custom.d.ts**
  125. ```typescript
  126. declare module "worker-loader!*" {
  127. class WebpackWorker extends Worker {
  128. constructor();
  129. }
  130. export default WebpackWorker;
  131. }
  132. ```
  133. **Worker.ts**
  134. ```typescript
  135. const ctx: Worker = self as any;
  136. // Post data to parent thread
  137. ctx.postMessage({ foo: "foo" });
  138. // Respond to message from parent thread
  139. ctx.addEventListener("message", (event) => console.log(event));
  140. ```
  141. **App.ts**
  142. ```typescript
  143. import Worker from "worker-loader!./Worker";
  144. const worker = new Worker();
  145. worker.postMessage({ a: 1 });
  146. worker.onmessage = (event) => {};
  147. worker.addEventListener("message", (event) => {});
  148. ```
  149. ### `Cross-Origin Policy`
  150. [`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. This scenario can commonly occur if you are hosting your assets under a CDN domain. Even downloads from the `webpack-dev-server` could be blocked. There are two workarounds
  151. Firstly, you can inline the worker as a blob instead of downloading it as an external script via the [`inline`](#inline) parameter
  152. **App.js**
  153. ```js
  154. import Worker from './file.worker.js';
  155. ```
  156. **webpack.config.js**
  157. ```js
  158. {
  159. loader: 'worker-loader'
  160. options: { inline: true }
  161. }
  162. ```
  163. Secondly, you may override the base download URL for your worker script via the [`publicPath`](#publicpath) option
  164. **App.js**
  165. ```js
  166. // This will cause the worker to be downloaded from `/workers/file.worker.js`
  167. import Worker from './file.worker.js';
  168. ```
  169. **webpack.config.js**
  170. ```js
  171. {
  172. loader: 'worker-loader'
  173. options: { publicPath: '/workers/' }
  174. }
  175. ```
  176. <h2 align="center">Maintainers</h2>
  177. <table>
  178. <tbody>
  179. <tr>
  180. <td align="center">
  181. <a href="https://github.com/TrySound">
  182. <img width="150" height="150" src="https://avatars3.githubusercontent.com/u/5635476?v=3&s=150">
  183. </a>
  184. <br />
  185. <a href="https://github.com/TrySound">Bogdan Chadkin</a>
  186. </td>
  187. <td align="center">
  188. <a href="https://github.com/bebraw">
  189. <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
  190. </br>
  191. Juho Vepsäläinen
  192. </a>
  193. </td>
  194. <td align="center">
  195. <a href="https://github.com/d3viant0ne">
  196. <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
  197. </br>
  198. Joshua Wiens
  199. </a>
  200. </td>
  201. <td align="center">
  202. <a href="https://github.com/michael-ciniawsky">
  203. <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
  204. </br>
  205. Michael Ciniawsky
  206. </a>
  207. </td>
  208. <td align="center">
  209. <a href="https://github.com/evilebottnawi">
  210. <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
  211. </br>
  212. Alexander Krasnoyarov
  213. </a>
  214. </td>
  215. </tr>
  216. <tbody>
  217. </table>
  218. [npm]: https://img.shields.io/npm/v/worker-loader.svg
  219. [npm-url]: https://npmjs.com/package/worker-loader
  220. [node]: https://img.shields.io/node/v/cache-loader.svg
  221. [node-url]: https://nodejs.org
  222. [deps]: https://david-dm.org/webpack-contrib/worker-loader.svg
  223. [deps-url]: https://david-dm.org/webpack-contrib/worker-loader
  224. [test]: http://img.shields.io/travis/webpack-contrib/worker-loader.svg
  225. [test-url]: https://travis-ci.org/webpack-contrib/worker-loader
  226. [cover]: https://codecov.io/gh/webpack-contrib/cache-loader/branch/master/graph/badge.svg
  227. [cover-url]: https://codecov.io/gh/webpack-contrib/cache-loader
  228. [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
  229. [chat-url]: https://gitter.im/webpack/webpack