webpack/examples/web-worker/README.md

13 KiB

example.js

var Worker = require("worker-loader!./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 harmony imports with the correct context // webpack_require.i = function(value) { return value; };

// // define getter function for harmony exports // webpack_require.d = function(exports, name, getter) { // if(!webpack_require.o(exports, name)) { // 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); // }) /******************************************************************/

</details>
``` javascript
/******/ ([
/* 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-loader!./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

/******/ (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 harmony imports with the correct context
/******/ 	__webpack_require__.i = function(value) { return value; };

/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			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 */,
/* 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).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__));}).catch(__webpack_require__.oe);
}


/***/ }
/******/ ]);

js/0.[hash].worker.js

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: 3f54ef4e4bc5a0766ec6
Version: webpack 2.2.0-rc.2
           Asset     Size  Chunks             Chunk Names
0.hash.worker.js  1.83 kB          [emitted]  
  hash.worker.js  3.88 kB          [emitted]  
       output.js  3.27 kB       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 311 bytes [entry] [rendered]
    > main [1] ./example.js 
    [0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [not cacheable] [built]
        cjs require worker-loader!./worker [1] ./example.js 1:13-46
    [1] ./example.js 215 bytes {0} [built]
Child worker:
               Asset     Size  Chunks             Chunk Names
    0.hash.worker.js  1.83 kB       0  [emitted]  
      hash.worker.js  3.88 kB       1  [emitted]  main
    Entrypoint main = hash.worker.js
    chunk    {0} 0.hash.worker.js 463 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 82 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 82 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 82 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) 168 bytes [entry] [rendered]
        > main [1] ./worker.js 
        [1] ./worker.js 168 bytes {1} [built]

Minimized (uglify-js, no zip)

Hash: 3f54ef4e4bc5a0766ec6
Version: webpack 2.2.0-rc.2
           Asset       Size  Chunks             Chunk Names
0.hash.worker.js  544 bytes          [emitted]  
  hash.worker.js  836 bytes          [emitted]  
       output.js  665 bytes       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 311 bytes [entry] [rendered]
    > main [1] ./example.js 
    [0] (webpack)/~/worker-loader!./worker.js 96 bytes {0} [not cacheable] [built]
        cjs require worker-loader!./worker [1] ./example.js 1:13-46
    [1] ./example.js 215 bytes {0} [built]
Child worker:
               Asset       Size  Chunks             Chunk Names
    0.hash.worker.js  544 bytes       0  [emitted]  
      hash.worker.js  836 bytes       1  [emitted]  main
    Entrypoint main = hash.worker.js
    chunk    {0} 0.hash.worker.js 463 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 82 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 82 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 82 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) 168 bytes [entry] [rendered]
        > main [1] ./worker.js 
        [1] ./worker.js 168 bytes {1} [built]