webpack/examples/web-worker/README.md

404 lines
13 KiB
Markdown
Raw Normal View History

2012-08-23 08:05:07 +08:00
# example.js
``` javascript
var Worker = require("worker-loader!./worker");
2012-08-23 08:05:07 +08:00
var worker = new Worker;
worker.postMessage("b");
worker.onmessage = function(event) {
var templateB = event.data; // "This text was generated by template B"
}
```
# worker.js
``` javascript
onmessage = function(event) {
var template = event.data;
require(["../require.context/templates/" + event.data], function(tmpl) {
postMessage(tmpl());
});
}
```
# js/output.js
2016-09-07 18:28:56 +08:00
<details><summary>`/******/ (function(modules) { /* webpackBootstrap */ })`</summary>
2012-08-23 08:05:07 +08:00
``` javascript
2013-03-28 17:31:52 +08:00
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // The require function
2014-03-25 17:44:10 +08:00
/******/ function __webpack_require__(moduleId) {
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
2014-03-25 17:44:10 +08:00
/******/ return installedModules[moduleId].exports;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2013-03-28 17:31:52 +08:00
/******/ };
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Execute the module function
2014-03-25 17:44:10 +08:00
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Return the exports of the module
2014-03-25 17:44:10 +08:00
/******/ return module.exports;
2013-03-28 17:31:52 +08:00
/******/ }
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // expose the modules object (__webpack_modules__)
2014-03-25 17:44:10 +08:00
/******/ __webpack_require__.m = modules;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // expose the module cache
2014-03-25 17:44:10 +08:00
/******/ __webpack_require__.c = installedModules;
2015-06-13 23:41:14 +08:00
2016-12-08 02:14:47 +08:00
/******/ // identity function for calling harmony imports with the correct context
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__.i = function(value) { return value; };
2016-12-08 02:14:47 +08:00
/******/ // define getter function for harmony exports
2016-09-07 18:28:56 +08:00
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2013-12-16 06:30:50 +08:00
/******/ // __webpack_public_path__
2014-03-25 17:44:10 +08:00
/******/ __webpack_require__.p = "js/";
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Load entry module and return exports
2016-02-04 20:02:53 +08:00
/******/ return __webpack_require__(__webpack_require__.s = 1);
2013-03-28 17:31:52 +08:00
/******/ })
2013-03-27 01:22:30 +08:00
/************************************************************************/
2013-12-16 06:30:50 +08:00
/******/ ([
/* 0 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2016-02-04 20:02:53 +08:00
/*!*********************************************!*\
!*** (webpack)/~/worker-loader!./worker.js ***!
\*********************************************/
/***/ function(module, exports, __webpack_require__) {
2016-09-07 18:28:56 +08:00
module.exports = function() {
return new Worker(__webpack_require__.p + "hash.worker.js");
};
2016-02-04 20:02:53 +08:00
/***/ },
/* 1 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2013-01-31 01:49:25 +08:00
/*!********************!*\
!*** ./example.js ***!
\********************/
2014-03-25 17:44:10 +08:00
/***/ function(module, exports, __webpack_require__) {
2013-01-31 01:49:25 +08:00
2016-09-07 18:28:56 +08:00
var Worker = __webpack_require__(/*! worker!./worker */ 0);
var worker = new Worker;
worker.postMessage("b");
worker.onmessage = function(event) {
var templateB = event.data; // "This text was generated by template B"
}
2012-08-23 08:05:07 +08:00
2013-01-31 01:49:25 +08:00
/***/ }
2015-06-13 23:41:14 +08:00
/******/ ]);
2012-08-23 08:05:07 +08:00
```
# js/[hash].worker.js
``` javascript
2013-03-28 17:31:52 +08:00
/******/ (function(modules) { // webpackBootstrap
2015-01-18 07:49:58 +08:00
/******/ this["webpackChunk"] = function webpackChunkCallback(chunkIds, moreModules) {
/******/ for(var moduleId in moreModules) {
/******/ modules[moduleId] = moreModules[moduleId];
/******/ }
/******/ while(chunkIds.length)
/******/ installedChunks[chunkIds.pop()] = 1;
/******/ };
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // The module cache
/******/ var installedModules = {};
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // object to store loaded chunks
/******/ // "1" means "already loaded"
2013-12-16 06:30:50 +08:00
/******/ var installedChunks = {
2016-02-04 20:02:53 +08:00
/******/ 1: 1
2013-12-16 06:30:50 +08:00
/******/ };
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // The require function
2014-03-25 17:44:10 +08:00
/******/ function __webpack_require__(moduleId) {
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
2014-03-25 17:44:10 +08:00
/******/ return installedModules[moduleId].exports;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Execute the module function
2014-03-25 17:44:10 +08:00
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Return the exports of the module
2014-03-25 17:44:10 +08:00
/******/ return module.exports;
/******/ }
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // This file contains only the entry chunk.
/******/ // The chunk loading function for additional chunks
2016-02-04 20:02:53 +08:00
/******/ __webpack_require__.e = function requireEnsure(chunkId) {
2013-03-28 17:31:52 +08:00
/******/ // "1" is the signal for "already loaded"
/******/ if(!installedChunks[chunkId]) {
/******/ importScripts("" + chunkId + ".hash.worker.js");
/******/ }
2016-02-04 20:02:53 +08:00
/******/ return Promise.resolve();
/******/ };
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // expose the modules object (__webpack_modules__)
2014-03-25 17:44:10 +08:00
/******/ __webpack_require__.m = modules;
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // expose the module cache
2014-03-25 17:44:10 +08:00
/******/ __webpack_require__.c = installedModules;
2015-06-13 23:41:14 +08:00
2016-12-08 02:14:47 +08:00
/******/ // identity function for calling harmony imports with the correct context
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__.i = function(value) { return value; };
2016-12-08 02:14:47 +08:00
/******/ // define getter function for harmony exports
2016-09-07 18:28:56 +08:00
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2013-12-16 06:30:50 +08:00
/******/ // __webpack_public_path__
2015-01-18 07:49:58 +08:00
/******/ __webpack_require__.p = "js/";
2015-06-13 23:41:14 +08:00
2013-03-28 17:31:52 +08:00
/******/ // Load entry module and return exports
2016-02-04 20:02:53 +08:00
/******/ return __webpack_require__(__webpack_require__.s = 1);
2013-03-28 17:31:52 +08:00
/******/ })
/************************************************************************/
2016-09-07 18:28:56 +08:00
```
</details>
``` javascript
2013-12-16 06:30:50 +08:00
/******/ ([
2016-02-04 20:02:53 +08:00
/* 0 */,
/* 1 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2013-01-31 10:25:20 +08:00
/*!*******************!*\
!*** ./worker.js ***!
\*******************/
2014-03-25 17:44:10 +08:00
/***/ function(module, exports, __webpack_require__) {
2012-08-23 08:05:07 +08:00
2016-09-07 18:28:56 +08:00
onmessage = function(event) {
var template = event.data;
__webpack_require__.e/* require */(0).catch(function(err) { __webpack_require__.oe(err); }).then(function() { var __WEBPACK_AMD_REQUIRE_ARRAY__ = [__webpack_require__(/*! ../require.context/templates */ 0)("./" + event.data)]; (function(tmpl) {
postMessage(tmpl());
}.apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));});
}
2013-10-31 07:49:59 +08:00
2012-08-23 08:05:07 +08:00
2013-03-27 01:22:30 +08:00
/***/ }
2015-06-13 23:41:14 +08:00
/******/ ]);
2012-08-23 08:05:07 +08:00
```
2016-06-29 01:08:13 +08:00
# js/0.[hash].worker.js
``` javascript
2016-02-04 20:02:53 +08:00
webpackChunk([0],[
/* 0 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2015-06-13 23:41:14 +08:00
/*!*********************************************!*\
!*** ../require.context/templates ^\.\/.*$ ***!
\*********************************************/
/***/ function(module, exports, __webpack_require__) {
2016-09-07 18:28:56 +08:00
var map = {
"./a": 2,
"./a.js": 2,
"./b": 3,
"./b.js": 3,
"./c": 4,
"./c.js": 4
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
};
function webpackContextResolve(req) {
var id = map[req];
if(!(id + 1)) // check for number
throw new Error("Cannot find module '" + req + "'.");
return id;
};
webpackContext.keys = function webpackContextKeys() {
return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = 0;
2015-06-13 23:41:14 +08:00
/***/ },
2016-02-04 20:02:53 +08:00
/* 1 */,
2015-06-13 23:41:14 +08:00
/* 2 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
!*** ../require.context/templates/a.js ***!
\*****************************************/
2015-06-13 23:41:14 +08:00
/***/ function(module, exports) {
2016-09-07 18:28:56 +08:00
module.exports = function() {
return "This text was generated by template A";
}
/***/ },
2015-06-13 23:41:14 +08:00
/* 3 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
!*** ../require.context/templates/b.js ***!
\*****************************************/
2015-06-13 23:41:14 +08:00
/***/ function(module, exports) {
2016-09-07 18:28:56 +08:00
module.exports = function() {
return "This text was generated by template B";
}
/***/ },
2015-06-13 23:41:14 +08:00
/* 4 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
!*** ../require.context/templates/c.js ***!
\*****************************************/
2015-06-13 23:41:14 +08:00
/***/ function(module, exports) {
2016-09-07 18:28:56 +08:00
module.exports = function() {
return "This text was generated by template C";
}
/***/ }
2015-06-13 23:41:14 +08:00
]);
```
2012-08-23 08:05:07 +08:00
# Info
## Uncompressed
```
Hash: e29ee95ab00f5a45ee69
Version: webpack 2.1.0-beta.25
Time: 191ms
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2016-09-07 18:28:56 +08:00
0.hash.worker.js 1.83 kB [emitted]
hash.worker.js 3.83 kB [emitted]
output.js 3.18 kB 0 [emitted] main
2016-09-07 18:28:56 +08:00
Entrypoint main = output.js
chunk {0} output.js (main) 302 bytes [entry] [rendered]
> main [1] ./example.js
[0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [built]
2016-02-04 20:02:53 +08:00
cjs require worker!./worker [1] ./example.js 1:13-39
[1] ./example.js 201 bytes {0} [built]
2013-01-31 01:49:25 +08:00
Child worker:
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2016-09-07 18:28:56 +08:00
0.hash.worker.js 1.83 kB 0 [emitted]
hash.worker.js 3.83 kB 1 [emitted] main
Entrypoint main = hash.worker.js
chunk {0} 0.hash.worker.js 457 bytes {1} [rendered]
2016-02-04 20:02:53 +08:00
> [1] ./worker.js 3:1-5:3
[0] ../require.context/templates ^\.\/.*$ 217 bytes {0} [built]
amd require context ../require.context/templates [1] ./worker.js 3:1-5:3
[2] ../require.context/templates/a.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./a [0] ../require.context/templates ^\.\/.*$
context element ./a.js [0] ../require.context/templates ^\.\/.*$
[3] ../require.context/templates/b.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./b [0] ../require.context/templates ^\.\/.*$
context element ./b.js [0] ../require.context/templates ^\.\/.*$
[4] ../require.context/templates/c.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./c [0] ../require.context/templates ^\.\/.*$
context element ./c.js [0] ../require.context/templates ^\.\/.*$
chunk {1} hash.worker.js (main) 168 bytes [entry] [rendered]
> main [1] ./worker.js
[1] ./worker.js 168 bytes {1} [built]
2012-08-23 08:05:07 +08:00
```
## Minimized (uglify-js, no zip)
```
Hash: e29ee95ab00f5a45ee69
Version: webpack 2.1.0-beta.25
Time: 329ms
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2016-02-04 20:02:53 +08:00
0.hash.worker.js 544 bytes [emitted]
hash.worker.js 842 bytes [emitted]
output.js 655 bytes 0 [emitted] main
2016-09-07 18:28:56 +08:00
Entrypoint main = output.js
chunk {0} output.js (main) 302 bytes [entry] [rendered]
> main [1] ./example.js
[0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [built]
2016-02-04 20:02:53 +08:00
cjs require worker!./worker [1] ./example.js 1:13-39
[1] ./example.js 201 bytes {0} [built]
2013-01-31 01:49:25 +08:00
Child worker:
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2016-02-04 20:02:53 +08:00
0.hash.worker.js 544 bytes 0 [emitted]
hash.worker.js 842 bytes 1 [emitted] main
2016-09-07 18:28:56 +08:00
Entrypoint main = hash.worker.js
chunk {0} 0.hash.worker.js 457 bytes {1} [rendered]
2016-02-04 20:02:53 +08:00
> [1] ./worker.js 3:1-5:3
[0] ../require.context/templates ^\.\/.*$ 217 bytes {0} [built]
amd require context ../require.context/templates [1] ./worker.js 3:1-5:3
[2] ../require.context/templates/a.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./a [0] ../require.context/templates ^\.\/.*$
context element ./a.js [0] ../require.context/templates ^\.\/.*$
[3] ../require.context/templates/b.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./b [0] ../require.context/templates ^\.\/.*$
context element ./b.js [0] ../require.context/templates ^\.\/.*$
[4] ../require.context/templates/c.js 80 bytes {0} [optional] [built]
2016-02-04 20:02:53 +08:00
context element ./c [0] ../require.context/templates ^\.\/.*$
context element ./c.js [0] ../require.context/templates ^\.\/.*$
chunk {1} hash.worker.js (main) 168 bytes [entry] [rendered]
> main [1] ./worker.js
[1] ./worker.js 168 bytes {1} [built]
2016-06-29 01:08:13 +08:00
```