@b2bc-devkit/gas-class-entrypoints
v1.0.0
Published
Vite/Rollup plugin that exposes static methods of exported classes as top-level Google Apps Script functions
Maintainers
Readme
@b2bc-devkit/gas-class-entrypoints
Vite/Rollup plugin that exposes static methods of exported classes as top-level Google Apps Script functions.
How it works
Problem
Google Apps Script discovers callable functions by scanning for top-level function Name() {} declarations at parse time. When you bundle code with Vite/Rollup the functions end up inside an IIFE or module scope and remain invisible to the GAS scanner.
Solution
This plugin:
- Injects a virtual entry module (
gas-entry) that imports the compiled entry file produced bytscand assigns every configured class static method toglobalThis, making them callable at GAS runtime. - Prepends
function Name() {}stubs for each binding so that GAS discovers the function names at parse time. - Wraps the entire bundle in an IIFE to prevent internal helpers and module-scoped variables from polluting the GAS global scope.
Prerequisites
- Your TypeScript source is compiled with
tscbefore running Vite.
The plugin looks for the entry file atbuild/index.js(flat layout) orbuild/src/index.js(mirrored layout whenrootDirissrc/). - Vite
>= 5.0.0is installed.
A typical build sequence:
tsc && vite buildInstallation
npm install @b2bc-devkit/gas-class-entrypoints --save-devUsage
Set input to the virtual "gas-entry" specifier and add the plugin to your Vite config:
// vite.config.js
import { defineConfig } from "vite";
import gasClassEntrypoints from "@b2bc-devkit/gas-class-entrypoints";
export default defineConfig({
build: {
rollupOptions: {
input: "gas-entry",
},
},
plugins: [
gasClassEntrypoints({
classEntrypoints: {
GasEntrypoints: ["demoCreate", "demoRead"],
},
}),
],
});Options
| Option | Type | Description |
| ------------------ | ---------------------------- | -------------------------------------------------------------------------------------------------- |
| classEntrypoints | Record<string, string[]> | Map of exported class names to arrays of static method names to expose as top-level GAS functions. |
Example
Given a TypeScript class:
// src/index.ts
export class GasEntrypoints {
static demoCreate() { /* ... */ }
static demoRead() { /* ... */ }
}After tsc && vite build the output bundle will contain:
function demoCreate() {}
function demoRead() {}
!function(){
// ... bundled code ...
globalThis.demoCreate = function(){ return GasEntrypoints.demoCreate(); };
globalThis.demoRead = function(){ return GasEntrypoints.demoRead(); };
}();GAS sees demoCreate and demoRead as callable top-level functions and they correctly delegate to the class static methods at runtime.
Peer Dependencies
vite>= 5.0.0
