diff --git a/package.json b/package.json index ca5ee00..9337058 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index a07c253..17b797e 100644 --- a/readme.md +++ b/readme.md @@ -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 `. +You can also run any [Clasp command](https://developers.google.com/apps-script/guides/clasp) with +`npx clasp `. ## Automated GitHub Workflow @@ -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 @@ -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 diff --git a/src/global.d.ts b/src/global.d.ts deleted file mode 100644 index 6cf3d95..0000000 --- a/src/global.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/** Add functions to this object to expose them to Google Apps triggers. */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -declare const global: Record void>; diff --git a/src/index.ts b/src/index.ts index 555ba41..f85cf06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/tsconfig.json b/tsconfig.json index 4cf07d1..e3a683b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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, diff --git a/webpack.config.js b/webpack.config.js index 7c8eb21..de64d0f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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, };