Skip to content

Instantly share code, notes, and snippets.

@jakepusateri
Last active April 13, 2017 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakepusateri/584baa0d5fc5025a4784b667de231256 to your computer and use it in GitHub Desktop.
Save jakepusateri/584baa0d5fc5025a4784b667de231256 to your computer and use it in GitHub Desktop.
webpack 1 working behavior
node_modules/
npm install
npm run build
npm run serve
console.log('a-out.js executed');
webpackJsonp([0],[
/* 0 */
/***/ function(module, exports, __webpack_require__) {
const share = __webpack_require__(1);
share();
console.log('a init');
/***/ }
]);
console.log('b-out.js executed');
webpackJsonp([1],[
/* 0 */
/***/ function(module, exports, __webpack_require__) {
const share = __webpack_require__(1);
share();
console.log('b init');
/***/ }
]);
console.log('commons.js executed');
/******/ (function(modules) { // webpackBootstrap
/******/ // install a JSONP callback for chunk loading
/******/ var parentJsonpFunction = window["webpackJsonp"];
/******/ window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) {
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
/******/ var moduleId, chunkId, i = 0, callbacks = [];
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
/******/ if(installedChunks[chunkId])
/******/ callbacks.push.apply(callbacks, installedChunks[chunkId]);
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
/******/ modules[moduleId] = moreModules[moduleId];
/******/ }
/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules);
/******/ while(callbacks.length)
/******/ callbacks.shift().call(null, __webpack_require__);
/******/ if(moreModules[0]) {
/******/ installedModules[0] = 0;
/******/ return __webpack_require__(0);
/******/ }
/******/ };
/******/ // The module cache
/******/ var installedModules = {};
/******/ // object to store loaded and loading chunks
/******/ // "0" means "already loaded"
/******/ // Array means "loading", array contains callbacks
/******/ var installedChunks = {
/******/ 2:0
/******/ };
/******/ // 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] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = 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, callback) {
/******/ // "0" is the signal for "already loaded"
/******/ if(installedChunks[chunkId] === 0)
/******/ return callback.call(null, __webpack_require__);
/******/ // an array means "currently loading".
/******/ if(installedChunks[chunkId] !== undefined) {
/******/ installedChunks[chunkId].push(callback);
/******/ } else {
/******/ // start chunk loading
/******/ installedChunks[chunkId] = [callback];
/******/ var head = document.getElementsByTagName('head')[0];
/******/ var script = document.createElement('script');
/******/ script.type = 'text/javascript';
/******/ script.charset = 'utf-8';
/******/ script.async = true;
/******/ script.src = __webpack_require__.p + "" + chunkId + "." + ({"0":"a","1":"b"}[chunkId]||chunkId) + "-out.js";
/******/ head.appendChild(script);
/******/ }
/******/ };
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ })
/************************************************************************/
/******/ ([
/* 0 */,
/* 1 */
/***/ function(module, exports) {
function share() {
return 1;
}
module.exports = share;
/***/ }
/******/ ]);
const share = require('./shared');
share();
console.log('a init');
const share = require('./shared');
share();
console.log('b init');
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Demo</title>
</head>
<body>
<h1>Demo</h1>
<script>
function insertScript(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.head.appendChild(script);
}
function loadA() {
loadCommons();
insertScript('/a-out.js');
}
function loadB() {
loadCommons();
insertScript('/b-out.js');
}
var isCommonsLoaded;
function loadCommons() {
if (isCommonsLoaded) {
return;
}
insertScript('/commons.js');
isCommonsLoaded = true;
}
</script>
<button onClick="loadA()">Load A</button>
<button onClick="loadB()">Load B</button>
<p>
Check out the console. Each time the buttons are clicked, another script tag gets added.
This runs the code inside the script. As entry modules, each time A or B are loaded,
they entry modules do re-run, and log their init.
</p>
</body>
</html>
{
"name": "webpack2-execute-modules-bug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"serve": "http-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"http-server": "^0.9.0",
"webpack": "^1.14.0",
"webpack-sources": "^0.2.3"
}
}
function share() {
return 1;
}
module.exports = share;
const webpack = require('webpack');
const ConcatSource = require("webpack-sources").ConcatSource;
// Make each file console.log its own name when executed
class NameAnnouncerPlugin {
apply(compiler) {
compiler.plugin("compilation", (compilation) => {
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
chunks.forEach((chunk) => {
chunk.files
.forEach((file) =>
compilation.assets[file] = new ConcatSource(
`console.log('${file} executed');`, "\n", compilation.assets[file]
)
);
});
callback();
});
});
}
}
module.exports = {
output: {
filename: "[name]-out.js"
},
entry: {
a: './entry-a.js',
b: './entry-b.js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons.js',
chunks: ['a', 'b']
}),
new NameAnnouncerPlugin()
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment