Skip to content

Commit

Permalink
Simplify by eliminating the global object
Browse files Browse the repository at this point in the history
  • Loading branch information
iansan5653 authored Dec 15, 2023
1 parent 6bf0f46 commit 104d977
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"build": "tsc && webpack",
"build": "rm -rf build && tsc && webpack",
"push": "npm run build && clasp push -f",
"deploy": "npm run push && clasp deploy",
"lint": "eslint",
Expand Down
29 changes: 12 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ have Node & npm installed):
- `npm run deploy` Build and push your code, then create a new versioned
[deployment](https://developers.google.com/apps-script/concepts/deployments).

You can also run any [Clasp command](https://developers.google.com/apps-script/guides/clasp) with `npx clasp <command>`.
You can also run any [Clasp command](https://developers.google.com/apps-script/guides/clasp) with
`npx clasp <command>`.

## Automated GitHub Workflow

Expand Down Expand Up @@ -97,29 +98,24 @@ In order for your Google Apps to run any of your code, you'll need to expose one
to the engine. In the traditional Google Apps Script environment, you'd do this by declaring global
functions, however in this setup there is no concept of 'global' as all files are modules.

Instead, you need to add these to the `global` object. This object type is declared in
`src/global.d.ts` so you can assign to it from any file, although as a matter of best-practice you
may want to confine global assignments to your `index.ts` file. Any function added to the `global`
object will be available under that name to all
Instead, all (and only) functions exported from `index.ts` will be available to Google Apps Script.
Any function added to the `global` object will be available= to all
[triggers](https://developers.google.com/apps-script/guides/triggers) and anywhere else Google Apps
might need to call your function, such as from a
[custom menu](https://developers.google.com/apps-script/guides/menus).

Note that the field in `global` that you assign the function, not the function's name, is treated as
the function name. For example, the following function will be called by the `onOpen` trigger, _not_
the `onEdit` trigger:
Examples for all the simple triggers are given in
[`index.ts`](https://github.com/iansan5653/gas-ts-template/blob/master/src/index.ts#L39-L43).

```ts
function onEdit() {}
For cleaner, more usable code, it may be useful to reference functions by their `name` property instead of hardcoding the name into code.
For example:

global.onOpen = onEdit;
```ts
const createButton = CardService.newTextButton()
.setText("Create")
.setOnClickAction(CardService.newAction().setFunctionName(onClickCreateEvent.name)); // instead of "onClickCreateEvent"
```

Further documentation is provided with the `global` type definition in
[`global.d.ts`](https://github.com/iansan5653/gas-ts-template/blob/master/src/global.d.ts) and
examples for all the simple triggers are given in
[`index.ts`](https://github.com/iansan5653/gas-ts-template/blob/master/src/index.ts#L39-L43).

### Circular Dependencies

Circular dependencies (files that depend on each other in a circular manner) can cause unexpected
Expand All @@ -139,7 +135,6 @@ project and you know they are wrong, try checking for circular dependencies usin
- `example.ts` Gives an example of how to export something from a local file.
- `index.ts` Provides you with the five basic triggers prebuilt, as well as an example of how to
import from a local file.
- `global.d.ts` Provides type-definitions for the global object from which triggers are exported.
- `.clasp.json` Provides the configuration for Clasp, the command-line tool which pushes code to
Google Apps Script.
- `.claspignore` Tells Clasp to ignore every file except for `Code.js`, `appsscript.json`, and
Expand Down
3 changes: 0 additions & 3 deletions src/global.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,3 @@ export function doGet(e: GoogleAppsScript.Events.DoGet): void {
export function doPost(e: GoogleAppsScript.Events.DoPost): void {
console.log(e);
}

global.onOpen = onOpen;
global.onEdit = onEdit;
global.onInstall = onInstall;
global.doPost = doPost;
global.doGet = doGet;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// GAS uses the v8 runtime in sync with Chrome now, so it always has the latest ES features. No need to downcompile.
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"noUnusedLocals": true,
Expand Down
14 changes: 10 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/* eslint-disable no-undef, @typescript-eslint/no-var-requires */
const GasPlugin = require("gas-webpack-plugin");

const entry = "./build/index.js";

module.exports = {
mode: "production",
// we always use dev mode because bundle size is unimportant - code runs server-side
mode: "development",
context: __dirname,
entry: "./build/index.js",
entry,
output: {
path: __dirname,
filename: "Code.js",
},
plugins: [new GasPlugin()],
plugins: [
new GasPlugin({
autoGlobalExportsFiles: [entry],
}),
],
devtool: false,
};

0 comments on commit 104d977

Please sign in to comment.