{"id":12986,"date":"2016-06-02T12:15:57","date_gmt":"2016-06-02T09:15:57","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=12986"},"modified":"2016-05-31T14:22:35","modified_gmt":"2016-05-31T11:22:35","slug":"lazy-inter-module-communication-require-webpack","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/","title":{"rendered":"Lazy inter-module communication with require() and webpack"},"content":{"rendered":"<p><b>Motivation<\/b><\/p>\n<p>CommonJS&#8217; modules can be loaded on demand with\u00a0<strong>require.ensure(&#8230;)<\/strong>. Example:<\/p>\n<pre class=\"brush:js\">require.ensure([\"module-a\"], function(require) {\r\n    var a = require(\"module-a\");\r\n    \/\/ module-a can be used now ...\r\n});<\/pre>\n<p>The well-known module bundler\u00a0<a href=\"https:\/\/webpack.github.io\/\">Webpack<\/a> supports this syntax for loading modules on demand. Such lazy loaded module(s) define a\u00a0<a href=\"https:\/\/webpack.github.io\/docs\/code-splitting.html\">split point<\/a>. Split points split the codebase into &#8220;chunks&#8221; (separate JavaScript files) which are loaded on demand. This feature allows to keep the initial download small and only download the code when it&#8217;s requested.<\/p>\n<p>Webpack performs a static analysis of the code at the project build time. Dynamic information is only available at runtime and not available at build time. You have to define your modules as constants. This leads to the problem. The following code will not work:<\/p>\n<pre class=\"brush:js\">var modA = \"module-a\";\r\nrequire.ensure([modA], function(require) {\r\n    var a = require(\"module-a\");\r\n    \/\/ module-a can be used now ...\r\n});<\/pre>\n<p>Webpack says something like:\u00a0<strong>Error: Module not found. A dependency to an entry point is not allowed<\/strong>.<\/p>\n<p>Theoretically you can use\u00a0<a href=\"https:\/\/webpack.github.io\/docs\/context.html\">require.context<\/a>, but this needs some parts of module&#8217;s path as constants too. Now, imagine you develop a modular UI library and would like to provide some callbacks for certain events (event handlers). Users of your library use it as a third-party library. They want to register their custom callback functions from their own modules. And they want to be called lazy, on demand, when an event occurs. You have many users and your library doesn&#8217;t know about custom modules of course. That means, you can not hard-code module&#8217;s path and function&#8217;s name a priori. What to do?<\/p>\n<p><b>Solution<\/b><\/p>\n<p><a href=\"https:\/\/github.com\/ova2\/dynamic-require-poc\">This POC on GitHub<\/a> demonstrates how to use truly dynamic require with webpack. It simulates third-party and own modules. The project structure:<\/p>\n<pre class=\"brush:js\">index.html          \/\/ some HTML template from the third-party UI library \r\nexternal-modules    \/\/ third-party library with CommonJS modules\r\nown-modules         \/\/ some library with CommonJS modules which uses the third-party library\r\ncommon-modules      \/\/ simple utility modules<\/pre>\n<p>The modules in external-modules don&#8217;t know about own-modules. Modules in the own-modules import modules from the external-modules. The most important webpack loader we use in this POC is\u00a0<a href=\"https:\/\/github.com\/webpack\/bundle-loader\">bundle loader for webpack<\/a>. This loader creates a special function with required module which can be executed lazily. The required module gets loaded when the function is executed.<\/p>\n<p>The HTML template in the POC, backed by the third-party (external) modules, look like as follows (only short snippet):<\/p>\n<pre class=\"brush:xml\">&lt;button data-init=\"button\" data-onclick='[\"ownModules.button\", \"alertSync\"]'&gt;\r\n    Try sync. call (ok)\r\n&lt;\/button&gt;\r\n&lt;p&gt;&lt;\/p&gt;\r\n&lt;button data-init=\"button\" data-onclick='[\"ownModules.button\", \"alertAsyncValid\"]'&gt;\r\n    Try async. call (ok)\r\n&lt;\/button&gt;\r\n&lt;p&gt;&lt;\/p&gt;\r\n&lt;button data-init=\"button\" data-onclick='[\"ownModules.button\", \"alertAsyncInvalid\"]'&gt;\r\n    Try async. call (error)\r\n&lt;\/button&gt;<\/pre>\n<p>The full code can be found in\u00a0<strong>index.xhtml<\/strong>. The callback functions are defined as values of\u00a0<strong>data-*<\/strong> attributes. Example:\u00a0<strong>data-onclick='[&#8220;ownModules.button&#8221;, &#8220;alertSync&#8221;]&#8217;<\/strong>. Here, the function\u00a0alertSync from the namespace\u00a0<strong>ownModules.button<\/strong> wants to be called when the button is clicked. The namespace is a simple JavaScript object defined in the global window scope.<\/p>\n<p>The module&#8217;s functions in the folder\u00a0<strong>own-modules<\/strong> returns\u00a0<strong>Promises<\/strong>. Promises allow asynchronous callbacks. In the POC, the NPM module\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/promise-light\">promise light<\/a> is used. If you have jQuery in your web app, you can use\u00a0<a href=\"https:\/\/api.jquery.com\/category\/deferred-object\">jQuery Deferred Object<\/a> as well because it provides the same functionality. The sample implementation of the\u00a0<strong>button.js<\/strong> looks like as<\/p>\n<pre class=\"brush:js\">var Promise = require('promise-light');\r\n\r\nmodule.exports = {\r\n    alertSync: function (element) {\r\n        document.getElementById('output').innerHTML = 'Clicked button. Sync. call is OK';\r\n        return Promise.resolve('OK');\r\n    },\r\n    \r\n    alertAsyncValid: function (element) {\r\n        return new Promise(function setup(resolve, reject) {\r\n            setTimeout(function () {\r\n                document.getElementById('output').innerHTML = 'Clicked button. Async. call is OK';\r\n                resolve('OK');\r\n            }, 1000);\r\n        });\r\n    },\r\n    \r\n    alertAsyncInvalid: function (element) {\r\n        return new Promise(function setup(resolve, reject) {\r\n            setTimeout(function () {\r\n                document.getElementById('output').innerHTML = 'Clicked button. Async. call has ERROR';\r\n                reject('ERROR');\r\n            }, 1000);\r\n        });\r\n    }\r\n};<\/pre>\n<p>The external modules should be bootstrapped by the own modules. The bootstrapping occurs in the file\u00a0<strong>entry.js<\/strong> which acts as an entry point in the webpack configuration.<\/p>\n<pre class=\"brush:js\">var objectifier = require(\".\/..\/common-modules\/objectifier\");\r\nvar bootstrap = require(\".\/..\/external-modules\/bootstrap\");\r\n\r\ndocument.addEventListener(\"DOMContentLoaded\", function (event) {    \r\n    \/\/ initialize external modules\r\n    bootstrap.bootstrap();\r\n    \r\n    \/\/ lazy load own modules (s. https:\/\/github.com\/webpack\/bundle-loader)\r\n    var lazyLoadedButton = require(\"bundle?lazy!.\/button\");\r\n    var lazyLoadedCheckbox = require(\"bundle?lazy!.\/checkbox\");\r\n    \r\n    \/\/ and put them under namespaces ownModules.button and ownModules.checkbox resp. \r\n    objectifier.set('ownModules.button', lazyLoadedButton);\r\n    objectifier.set('ownModules.checkbox', lazyLoadedCheckbox);\r\n});<\/pre>\n<p>As you can see, the bundle loader is applied to the own modules. The returned functions are saved under arbitrary namespaces in order to be executed later in external modules. Note: the<br \/>\n<strong>objectifier<\/strong> is just a CommonJS module porting of the beautiful\u00a0<a href=\"https:\/\/davidwalsh.name\/nested-objects\">getter and setter implementation for nested objects<\/a>. The\u00a0<strong>button.js<\/strong> in the\u00a0<strong>external-modules<\/strong> registers an onlick event handler for all HTML elements having the\u00a0<strong>data-init=&#8221;button&#8221;<\/strong> attribute.<\/p>\n<pre class=\"brush:js\">var lazyloader = require(\".\/lazyloader\");\r\n\r\nmodule.exports = {\r\n    init: function () {\r\n        var elements = document.querySelectorAll('[data-init=\"button\"]');\r\n        for (var i = 0; i &lt; elements.length; i++) {\r\n            (function () {\r\n                var element = elements[i];\r\n\r\n                \/\/ onlick event handler\r\n                var onclick = function () {\r\n                    \/\/ so something module specific ...\r\n\r\n                    \/\/ execute function defined via data-onclick attribute if it's defined,\r\n                    \/\/ e.g. data-onclick='[\"ownModules.button\", \"alertSync\"]'\r\n                    lazyloader.execute(element, 'data-onclick',\r\n                        function resolved(value) {\r\n                            alert(value);\r\n                        }, function rejected(error) {\r\n                            alert(error);\r\n                        });\r\n                };\r\n\r\n                element.addEventListener(\"click\", onclick);\r\n            })();\r\n        }\r\n\r\n        return elements;\r\n    },\r\n\r\n    doSomething: function () {\r\n        \/\/ ...\r\n    }\r\n};<\/pre>\n<p>The core logic for the event handling is encapsulated in the\u00a0<strong>lazyloader.js<\/strong>.<\/p>\n<pre class=\"brush:js\">\/**\r\n Loads modules specified via data-* attributes and executes the specified function.\r\n HTML-Example:\r\n\r\n &lt;button data-init=\"button\" data-onclick='[\"ownModules.button\", \"alertSync\"]'&gt;\r\n    ...\r\n &lt;\/button&gt;\r\n\r\n The function ownModules.button.alertSync(button) will be executed.\r\n *\/\r\n\r\nvar objectifier = require(\".\/..\/common-modules\/objectifier\");\r\n\r\nmodule.exports = {\r\n    \/**\r\n     * Executes the specified synchronous or asynchronous function from the specified module\r\n     * and invokes resolved or rejected callbacks respectively.\r\n     * The function should return a promise.\r\n     * \r\n     * @param element HTML element where data-* is defined\r\n     * @param data value of data-* as string\r\n     * @param resolvedCallback callback which gets executed when the returned promise is fullfilled\r\n     * @param rejectedCallback callback which gets executed when the returned promise is rejected\r\n     *\/\r\n    execute: function (element, data, resolvedCallback, rejectedCallback) {\r\n        var strData = element.getAttribute(data);\r\n        if (strData) {\r\n            var objData = JSON.parse(strData);\r\n            var module = objData[0];\r\n            var func = objData[1];\r\n\r\n            if (module &amp;&amp; objectifier.exists(module) &amp;&amp; func) {\r\n                \/\/ module and function exist ==&gt; load the module\r\n                objectifier.get(module)(function (module) {\r\n                    \/\/ execute the specified function from the module.\r\n                    \/\/ return value is a promise (see https:\/\/www.npmjs.com\/package\/promise-light)\r\n                    module[func](element).then(\r\n                        function resolved(value) {\r\n                            resolvedCallback(value);\r\n                        },\r\n                        function rejected(error) {\r\n                            rejectedCallback(error);\r\n                        });\r\n                });\r\n            }\r\n        }\r\n    }\r\n};<\/pre>\n<p>It executes the specified function from the specified module. The caller receives a promise. If the promise gets resolved, the first function in\u00a0<strong>.then(&#8230;)<\/strong> is executed. If the promise gets rejected, the second function in\u00a0.<strong>then(&#8230;)<\/strong> is executed. The synchronous or asynchronous function from the own modules can control whether it resolves or rejects the promise. The caller can decide then what it should do in both cases. For instance, assume you got an accordion widget which belongs to a third-party library. One interesting case could be the validation of the content of an open accordion tab when the tab&#8217;s header is clicked by the user. A custom function could validate the tab content on such click. In case of a successful validation, the clicked tab can be closed. Otherwise, the tab should stay open.<\/p>\n<p><b>Screens<\/b><\/p>\n<p>The initial load doesn&#8217;t fetch chunks from the own modules.<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/init-load.png\"><img decoding=\"async\" class=\"aligncenter wp-image-12992\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/init-load.png\" alt=\"init-load\" width=\"860\" height=\"668\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/init-load.png 977w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/init-load-300x233.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/init-load-768x597.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Click on a button triggers the load of the first chunk.<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-1.png\"><img decoding=\"async\" class=\"aligncenter wp-image-12993\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-1.png\" alt=\"chunk-1\" width=\"860\" height=\"662\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-1.png 979w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-1-300x231.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-1-768x591.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Click on a checkbox triggers the load of the second chunk.<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-2.png\"><img decoding=\"async\" class=\"aligncenter wp-image-12994\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-2.png\" alt=\"chunk-2\" width=\"860\" height=\"664\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-2.png 978w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-2-300x232.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/chunk-2-768x593.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/ovaraksin.blogspot.com\/2016\/05\/lazy-inter-module-communication-with.html\">Lazy inter-module communication with require() and webpack<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\">WCG partner<\/a> Oleg Varaksin at the <a href=\"http:\/\/ovaraksin.blogspot.com\/\">Thoughts on software development<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Motivation CommonJS&#8217; modules can be loaded on demand with\u00a0require.ensure(&#8230;). Example: require.ensure([&#8220;module-a&#8221;], function(require) { var a = require(&#8220;module-a&#8221;); \/\/ module-a can be used now &#8230; }); The well-known module bundler\u00a0Webpack supports this syntax for loading modules on demand. Such lazy loaded module(s) define a\u00a0split point. Split points split the codebase into &#8220;chunks&#8221; (separate JavaScript files) which &hellip;<\/p>\n","protected":false},"author":15,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[374],"class_list":["post-12986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-webpack"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Motivation CommonJS&#039; modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(&quot;module-a&quot;);\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Motivation CommonJS&#039; modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(&quot;module-a&quot;);\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-02T09:15:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Oleg Varaksin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleg Varaksin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\"},\"author\":{\"name\":\"Oleg Varaksin\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712\"},\"headline\":\"Lazy inter-module communication with require() and webpack\",\"datePublished\":\"2016-06-02T09:15:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\"},\"wordCount\":753,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"keywords\":[\"Webpack\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\",\"name\":\"Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2016-06-02T09:15:57+00:00\",\"description\":\"Motivation CommonJS' modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(\\\"module-a\\\");\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Lazy inter-module communication with require() and webpack\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712\",\"name\":\"Oleg Varaksin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g\",\"caption\":\"Oleg Varaksin\"},\"sameAs\":[\"http:\/\/ovaraksin.blogspot.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/oleg-varaksin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026","description":"Motivation CommonJS' modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(\"module-a\");","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/","og_locale":"en_US","og_type":"article","og_title":"Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026","og_description":"Motivation CommonJS' modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(\"module-a\");","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-06-02T09:15:57+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Oleg Varaksin","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Oleg Varaksin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/"},"author":{"name":"Oleg Varaksin","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712"},"headline":"Lazy inter-module communication with require() and webpack","datePublished":"2016-06-02T09:15:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/"},"wordCount":753,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","keywords":["Webpack"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/","name":"Lazy inter-module communication with require() and webpack - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2016-06-02T09:15:57+00:00","description":"Motivation CommonJS' modules can be loaded on demand with\u00a0require.ensure(...). Example: require.ensure(, function(require) { var a = require(\"module-a\");","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/lazy-inter-module-communication-require-webpack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Lazy inter-module communication with require() and webpack"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712","name":"Oleg Varaksin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g","caption":"Oleg Varaksin"},"sameAs":["http:\/\/ovaraksin.blogspot.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/oleg-varaksin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=12986"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12986\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=12986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=12986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=12986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}