Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call webpack bundled function/class from console.log

Is it possible to access the exported modules(ES6->ES5 compiled) from web inspector console? They are bundled together using webpack

I am trying to call Session.setSessionInLocalStorage('test key', 'test object');

/******/ (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] = {
/******/ 			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;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!*********************!*\
  !*** ./app/main.js ***!
  \*********************/
/***/ function(module, exports, __webpack_require__) {

	'use strict';
	
	var _CoreUtils = __webpack_require__(/*! ./Core/utils/session/Core.utils.Session */ 4);
	
	var mySession = new _CoreUtils.Session();
	
	mySession.setSessionInLocalStorage('key', 'my object');

/***/ },
/* 1 */,
/* 2 */,
/* 3 */,
/* 4 */
/*!******************************************************!*\
  !*** ./app/Core/utils/session/Core.utils.Session.js ***!
  \******************************************************/
/***/ function(module, exports) {

	"use strict";
	
	Object.defineProperty(exports, "__esModule", {
	    value: true
	});
	
	var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
	
	function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
	
	var Session = function () {
	    function Session(sessionId) {
	        _classCallCheck(this, Session);
	
	        this.sessionId = sessionId;
	    }
	
	    _createClass(Session, [{
	        key: "getSessionFromLocalStorage",
	        value: function getSessionFromLocalStorage(sessionKey) {
	            return localStorage.getItem(sessionKey);
	        }
	    }, {
	        key: "setSessionInLocalStorage",
	        value: function setSessionInLocalStorage(sessionKey, sessionData) {
	            localStorage.setItem(sessionKey, JSON.stringify(sessionData));
	        }
	    }]);
	
	    return Session;
	}();
	
	exports.Session = Session;

/***/ }
/******/ ]);
//# sourceMappingURL=app.js.map
like image 871
MonteCristo Avatar asked Apr 18 '16 21:04

MonteCristo


2 Answers

Yes.

Add following code to some module in bundle:

require.ensure([], function () {
    window.require = function (module) {
        return require(module);
    };
});

Use require from console:

require("./app").doSomething();

EDIT:

In TypeScript we use:

(require as any).ensure([], () => {
    window["require"] = module => require(module);
});

I'm currently instantiating/getting my services this way:

var contactsService= (new (require("./app").default)).contactsService;
like image 80
Milan Jaros Avatar answered Oct 23 '22 03:10

Milan Jaros


1) Open your devtools, the "Sources" panel.

2) open the webpack:// in the explorer tab, you'll see your original ES6 files thanks to sourcemaps.

You can setup break points, access variables ...

like image 27
topheman Avatar answered Oct 23 '22 04:10

topheman