webpack/examples/web-worker/README.md

13 KiB

example.js

var Worker = require("worker!./worker");
var worker = new Worker;
worker.postMessage("b");
worker.onmessage = function(event) {
	var templateB = event.data; // "This text was generated by template B"
}

worker.js

onmessage = function(event) {
	var template = event.data;
	require(["../require.context/templates/" + event.data], function(tmpl) {
		postMessage(tmpl());
	});
}

js/output.js

`/******/ (function(modules) { /* webpackBootstrap */ })` ``` javascript /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {};

// // The require function // function webpack_require(moduleId) {

// // Check if module is in cache // if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports;

// // Create a new module (and put it into the cache) // var module = installedModules[moduleId] = { // i: moduleId, // l: false, // exports: {} // };

// // Execute the module function // modules[moduleId].call(module.exports, module, module.exports, webpack_require);

// // Flag the module as loaded // module.l = true;

// // Return the exports of the module // return module.exports; /******/ }

// // expose the modules object (webpack_modules) // webpack_require.m = modules;

// // expose the module cache // webpack_require.c = installedModules;

// // identity function for calling harmory imports with the correct context // webpack_require.i = function(value) { return value; };

// // define getter function for harmory exports // 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); };

// // webpack_public_path // webpack_require.p = "js/";

// // Load entry module and return exports // return webpack_require(webpack_require.s = 1); // }) // // ([ / 0 / / unknown exports provided / / all exports used / /!!
!
(webpack)/~/worker-loader!./worker.js ! *
****************************/ // function(module, exports, webpack_require) {

module.exports = function() { return new Worker(webpack_require.p + "hash.worker.js"); };

// }, / 1 / / unknown exports provided / / all exports used / /!****************!
!
./example.js ! **************/ // function(module, exports, webpack_require) {

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" }

// } /***/ ]);


# js/[hash].worker.js

``` javascript
/******/ (function(modules) { // webpackBootstrap
/******/ 	this["webpackChunk"] = function webpackChunkCallback(chunkIds, moreModules) {
/******/ 		for(var moduleId in moreModules) {
/******/ 			modules[moduleId] = moreModules[moduleId];
/******/ 		}
/******/ 		while(chunkIds.length)
/******/ 			installedChunks[chunkIds.pop()] = 1;
/******/ 	};

/******/ 	// The module cache
/******/ 	var installedModules = {};

/******/ 	// object to store loaded chunks
/******/ 	// "1" means "already loaded"
/******/ 	var installedChunks = {
/******/ 		1: 1
/******/ 	};

/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {

/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId])
/******/ 			return installedModules[moduleId].exports;

/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};

/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ 		// Flag the module as loaded
/******/ 		module.l = true;

/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}

/******/ 	// This file contains only the entry chunk.
/******/ 	// The chunk loading function for additional chunks
/******/ 	__webpack_require__.e = function requireEnsure(chunkId) {
/******/ 		// "1" is the signal for "already loaded"
/******/ 		if(!installedChunks[chunkId]) {
/******/ 			importScripts("" + chunkId + ".hash.worker.js");
/******/ 		}
/******/ 		return Promise.resolve();
/******/ 	};

/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;

/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;

/******/ 	// identity function for calling harmory imports with the correct context
/******/ 	__webpack_require__.i = function(value) { return value; };

/******/ 	// define getter function for harmory exports
/******/ 	__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); };

/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "js/";

/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
``` javascript /******/ ([ /* 0 */, /* 1 */ /* unknown exports provided */ /* all exports used */ /*!*******************!*\ !*** ./worker.js ***! \*******************/ /***/ function(module, exports, __webpack_require__) {

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));}); }

// } /***/ ]);


# js/0.[hash].worker.js

``` javascript
webpackChunk([0],[
/* 0 */
/* unknown exports provided */
/* all exports used */
/*!*********************************************!*\
  !*** ../require.context/templates ^\.\/.*$ ***!
  \*********************************************/
/***/ function(module, exports, __webpack_require__) {

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;


/***/ },
/* 1 */,
/* 2 */
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
  !*** ../require.context/templates/a.js ***!
  \*****************************************/
/***/ function(module, exports) {

module.exports = function() {
	return "This text was generated by template A";
}

/***/ },
/* 3 */
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
  !*** ../require.context/templates/b.js ***!
  \*****************************************/
/***/ function(module, exports) {

module.exports = function() {
	return "This text was generated by template B";
}

/***/ },
/* 4 */
/* unknown exports provided */
/* all exports used */
/*!*****************************************!*\
  !*** ../require.context/templates/c.js ***!
  \*****************************************/
/***/ function(module, exports) {

module.exports = function() {
	return "This text was generated by template C";
}

/***/ }
]);

Info

Uncompressed

Hash: e29ee95ab00f5a45ee69
Version: webpack 2.1.0-beta.25
Time: 191ms
           Asset     Size  Chunks             Chunk Names
0.hash.worker.js  1.83 kB          [emitted]  
  hash.worker.js  3.83 kB          [emitted]  
       output.js  3.18 kB       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 297 bytes [entry] [rendered]
    > main [1] ./example.js 
    [0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [not cacheable] [built]
        cjs require worker!./worker [1] ./example.js 1:13-39
    [1] ./example.js 201 bytes {0} [built]
Child worker:
               Asset     Size  Chunks             Chunk Names
    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]
        > [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]
            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]
            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]
            context element ./c [0] ../require.context/templates ^\.\/.*$
            context element ./c.js [0] ../require.context/templates ^\.\/.*$
    chunk    {1} hash.worker.js (main) 162 bytes [entry] [rendered]
        > main [1] ./worker.js 
        [1] ./worker.js 162 bytes {1} [built]

Minimized (uglify-js, no zip)

Hash: e29ee95ab00f5a45ee69
Version: webpack 2.1.0-beta.25
Time: 329ms
           Asset       Size  Chunks             Chunk Names
0.hash.worker.js  544 bytes          [emitted]  
  hash.worker.js  842 bytes          [emitted]  
       output.js  655 bytes       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 297 bytes [entry] [rendered]
    > main [1] ./example.js 
    [0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [not cacheable] [built]
        cjs require worker!./worker [1] ./example.js 1:13-39
    [1] ./example.js 201 bytes {0} [built]
Child worker:
               Asset       Size  Chunks             Chunk Names
    0.hash.worker.js  544 bytes       0  [emitted]  
      hash.worker.js  842 bytes       1  [emitted]  main
    Entrypoint main = hash.worker.js
    chunk    {0} 0.hash.worker.js 457 bytes {1} [rendered]
        > [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]
            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]
            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]
            context element ./c [0] ../require.context/templates ^\.\/.*$
            context element ./c.js [0] ../require.context/templates ^\.\/.*$
    chunk    {1} hash.worker.js (main) 162 bytes [entry] [rendered]
        > main [1] ./worker.js 
        [1] ./worker.js 162 bytes {1} [built]