繁体   English   中英

从console.log调用webpack捆绑的函数/类

[英]Call webpack bundled function/class from console.log

是否可以从Web检查器控制台访问导出的模块(ES6-> ES5编译)? 它们使用webpack捆绑在一起

我试图调用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 

1)打开你的devtools,“Sources”面板。

2)打开webpack://在资源管理器选项卡中,由于源图,您将看到原始的ES6文件。

您可以设置断点,访问变量......

是。

将以下代码添加到bundle中的某个模块:

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

从控制台使用require:

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

编辑:

在TypeScript中我们使用:

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

我目前正以这种方式实例化/获取我的服务:

var contactsService= (new (require("./app").default)).contactsService;

如果您不想在应用中添加特殊代码。 您可以按照以下步骤操作:

  • 在您使用稍后要调试的服务/类的位置放置断点
  • 在控制台中,将其保存在窗口中,例如window.myService = this.props.myService
  • 使用窗口中保存的参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM