Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export declarations are not exposed as triggers to GAS #2

Closed
kiddhustle opened this issue May 16, 2024 · 6 comments · Fixed by #3
Closed

Export declarations are not exposed as triggers to GAS #2

kiddhustle opened this issue May 16, 2024 · 6 comments · Fixed by #3

Comments

@kiddhustle
Copy link

I create an app with simple code however I cannot execute any of the transpiled code in the script editor.

Is this down to the project code or setup/configuration?

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ "./build/example.js":
/*!**************************!*\
  !*** ./build/example.js ***!
  \**************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   helloWorld: () => (/* binding */ helloWorld)
/* harmony export */ });
const helloWorld = "Hello, world!";


/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/global */
/******/ 	(() => {
/******/ 		__webpack_require__.g = (function() {
/******/ 			if (typeof globalThis === 'object') return globalThis;
/******/ 			try {
/******/ 				return this || new Function('return this')();
/******/ 			} catch (e) {
/******/ 				if (typeof window === 'object') return window;
/******/ 			}
/******/ 		})();
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/*!************************!*\
  !*** ./build/index.js ***!
  \************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   doGet: () => (/* binding */ doGet),
/* harmony export */   doPost: () => (/* binding */ doPost),
/* harmony export */   onEdit: () => (/* binding */ onEdit),
/* harmony export */   onInstall: () => (/* binding */ onInstall),
/* harmony export */   onOpen: () => (/* binding */ onOpen)
/* harmony export */ });
/* harmony import */ var _example__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./example */ "./build/example.js");
// You can access any of the global GAS objects in this file. You can also
// import local files or external dependencies:

console.log(_example__WEBPACK_IMPORTED_MODULE_0__.helloWorld);
// Simple Triggers: These five export functions are reserved export function names that are
// called by Google Apps when the corresponding event occurs. You can safely
// delete them if you won't be using them, but don't use the same export function names
// for anything else.
// See: https://developers.google.com/apps-script/guides/triggers
function onOpen(e) {
    console.log(e);
}
function onEdit(e) {
    console.log(e);
}
function onInstall(e) {
    console.log(e);
}
function doGet(e) {
    console.log(e);
}
function doPost(e) {
    console.log(e);
}

})();

/******/ })()
;
@iansan5653
Copy link
Owner

Hi, what error are you seeing when you run the code?

Could you create a public GitHub repository with your typescript code so I can try it out?

@kiddhustle
Copy link
Author

Hi thanks for getting back to me.

I created a repo with my code here: https://github.com/kiddhustle/gas-ts-template

There is no error per say, but whilst I do see the code pushed to my project (via clasp), I am unable to execute the code.

I am not sure if this is because the code in the example is made up of triggers.

I created another function helloYou to see if this could be ran/executed but I can't run that either.

Screenshot from 2024-05-18 02-54-58

@kiddhustle
Copy link
Author

Please note that I am unable to select and run a function.

@iansan5653
Copy link
Owner

iansan5653 commented May 20, 2024

Thanks @kiddhustle; I'm able to reproduce this as well. It seems to be a bug in the Webpack plugin we are using to build the Code.gs file. After some experimentation I found out it is only recognizing certain types of exports. This seems to be the same as the issue described in fossamagna/gas-webpack-plugin#879

Simple declaration exports do not work:

// ❌ Does NOT work
export function one() { /* ... */ }

export const two = () => { /* ... */ }

But object exports and re-exports from other files do:

// ✅ Does work:
function three() { /* ... */ }

export {three}

export {four} from "./four.ts"

So as a workaround, you can update your code like so:

function onOpen(
  e:
    | GoogleAppsScript.Events.DocsOnOpen
    | GoogleAppsScript.Events.SlidesOnOpen
    | GoogleAppsScript.Events.SheetsOnOpen
    | GoogleAppsScript.Events.FormsOnOpen,
): void {
  console.log(e);
}

function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit): void {
  console.log(e);
}

function onInstall(e: GoogleAppsScript.Events.AddonOnInstall): void {
  console.log(e);
}

function doGet(e: GoogleAppsScript.Events.DoGet): void {
  console.log(e);
}

function doPost(e: GoogleAppsScript.Events.DoPost): void {
  console.log(e);
}

export {onOpen, onEdit, onInstall, doGet, doPost}

I recognize this is not as readable but unfortunately I think it's the only solution until the bug gets fixed. I will update the example code and documentation to note this.

@iansan5653 iansan5653 changed the title Transpiled Code.js doesn't run in GAS Export declarations are not exposed as triggers to GAS May 20, 2024
@kiddhustle
Copy link
Author

OK thanks for looking into this for me. At least now I know the problem is not with the project or the code.

Thanks!

@iansan5653
Copy link
Owner

No problem, thanks for reporting the bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants