【发布时间】:2015-09-28 14:53:56
【问题描述】:
我正在使用 GoogleAppsScript。
我正在尝试将我的代码放在本地,以便:
- 使用 github
- 编写 ES6
我正在使用 webpack,生成将在 html 页面中运行的包不是问题。 But I don't know for now how to generate a bundle that I will be able to copy/paste to GoogleAppsScript.
我创建的 main.js 文件是这样的:
import Point from './Point.js'
import Test from './test.js'
import SpreadSheetLogger from './SpreadSheetLogger.js'
当我查看 捆绑包时,我有类似的东西:
/******/ (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 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _PointJs = __webpack_require__(1);
var _PointJs2 = _interopRequireDefault(_PointJs);
var _testJs = __webpack_require__(2);
var _testJs2 = _interopRequireDefault(_testJs);
var _SpreadSheetLoggerJs = __webpack_require__(4);
var _SpreadSheetLoggerJs2 = _interopRequireDefault(_SpreadSheetLoggerJs);
var a = new _PointJs2['default'](1, 2);
/***/ },
/* 1 */
/***/ 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 Point = (function () {
function Point(x, y) {
_classCallCheck(this, Point);
this.x = x;
this.y = y;
}
_createClass(Point, [{
key: "toString",
value: function toString() {
return "(" + this.x + ", " + this.y + ")";
}
}]);
return Point;
})();
exports["default"] = Point;
module.exports = exports["default"];
/***/ },
/* 2 */
/***/ function(module, exports) {
"use strict";
[....]
/***/ }
/******/ ]);
所以如果我总结一下,它是这样的:
(function(modules) {
WebPack code
}([
module1,
module2,
...
])
但如果我将该代码复制/粘贴到浏览器的 javascript 中,我将无法使用不同的模块。
如果我从包中提取 modules 并将 copy/paste 提取到浏览器中的 js 控制台,这一次它可以工作。
我确信 webpack/babel 可以生成我需要的代码,但我找不到如何。
【问题讨论】:
-
模块没有什么是全局性的。我对谷歌应用脚本不熟悉,你只需要在
window上暴露一些功能吗? -
是的,我需要公开一些课程。我的问题来自我的捆绑包暂时没有公开它们。
-
你能在你的
main中像window.Point = Point这样自己做吗? -
window不存在。我刚刚进入了浏览器的解决方案:(function(root) { class MyClass { ... }; root.MyClass = MyClass; })(window),但我在 Google App Script 中找不到与window对应的对象。
标签: javascript webpack babeljs