@gas-plugin/unplugin
v0.1.2
Published
Universal bundler plugin for Google Apps Script projects (Vite, Rollup, Rolldown, webpack, esbuild, Bun)
Maintainers
Readme
@gas-plugin/unplugin
A universal bundler plugin for Google Apps Script (GAS) projects. Works with Vite, Rollup, Rolldown, webpack, esbuild, and Bun.
What It Does
Google Apps Script requires plain top-level function declarations — no export keywords, no ES module syntax. This plugin bridges the gap between modern bundler output and what GAS expects:
- Strips
exportkeywords from function/class/variable declarations - Removes
export { ... }blocks generated by bundlers - Prevents tree-shaking of GAS entry points (e.g.,
onOpen,doGet) - Copies
appsscript.jsonmanifest to the output directory - Copies additional files (HTML, JSON, etc.) via glob patterns
Install
# npm
npm install -D @gas-plugin/unplugin
# yarn
yarn add -D @gas-plugin/unplugin
# pnpm
pnpm add -D @gas-plugin/unpluginUsage
Vite
// vite.config.ts
import gasPlugin from "@gas-plugin/unplugin/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [gasPlugin()],
build: {
lib: {
entry: "src/main.ts",
formats: ["es"],
fileName: () => "Code.js",
},
},
});Rollup
// rollup.config.js
import gasPlugin from "@gas-plugin/unplugin/rollup";
export default {
input: "src/main.ts",
output: { dir: "dist", format: "es" },
plugins: [gasPlugin()],
};webpack
// webpack.config.js
const gasPlugin = require("@gas-plugin/unplugin/webpack").default;
module.exports = {
entry: "./src/main.ts",
output: { path: __dirname + "/dist" },
plugins: [gasPlugin()],
};esbuild
import gasPlugin from "@gas-plugin/unplugin/esbuild";
import { build } from "esbuild";
await build({
entryPoints: ["src/main.ts"],
outdir: "dist",
bundle: true,
format: "esm",
plugins: [gasPlugin()],
});Rolldown
// rolldown.config.mjs
import gasPlugin from "@gas-plugin/unplugin/rolldown";
export default {
input: "src/main.ts",
output: { dir: "dist", format: "es" },
plugins: [gasPlugin()],
};Bun
import gasPlugin from "@gas-plugin/unplugin/bun";
await Bun.build({
entrypoints: ["src/main.ts"],
outdir: "dist",
plugins: [gasPlugin()],
});Options
interface GasPluginOptions {
/**
* Path to appsscript.json manifest file.
* Resolved relative to the project root.
* @default "appsscript.json"
*/
manifest?: string;
/**
* Glob patterns for additional files to copy flat to the output directory.
* Files are copied without subdirectory structure (basename only).
* Duplicate basenames trigger a warning; the first match wins.
* @default []
*/
include?: string[];
/**
* Function names to explicitly protect from tree-shaking.
* These functions will be kept as top-level declarations in the output,
* even if they are not exported from the entry point.
* @default []
*/
globals?: string[];
/**
* When true (default), exported functions are automatically added to the
* tree-shake protection list. When false, only functions explicitly listed
* in `globals` are protected.
* @default true
*/
autoGlobals?: boolean;
}Examples
Basic GAS Project
// vite.config.ts
import gasPlugin from "@gas-plugin/unplugin/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [gasPlugin()],
build: {
lib: {
entry: "src/main.ts",
formats: ["es"],
fileName: () => "Code.js",
},
},
});GAS Web App with HTML
// vite.config.ts
import gasPlugin from "@gas-plugin/unplugin/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
gasPlugin({
include: ["src/**/*.html"],
globals: ["getData", "saveData"],
}),
],
build: {
lib: {
entry: "src/main.ts",
formats: ["es"],
fileName: () => "Code.js",
},
},
});How It Works
Transform phase: Detects exported function declarations and injects tree-shake protection markers (
globalThis.__gas_keep__) to prevent bundlers from removing GAS entry points.Post-process phase: After bundling, strips all
exportkeywords andexport { ... }blocks, removes the tree-shake protection markers, and normalizes trailing newlines.File copy phase: Copies
appsscript.jsonand any files matchingincludepatterns to the output directory.
Compatibility
| Bundler | Version | | -------- | -------------- | | Vite | >= 5.0.0 | | Rollup | >= 4.0.0 | | Rolldown | >= 1.0.0-rc.1 | | webpack | >= 5.0.0 | | esbuild | >= 0.17.0 | | Bun | latest |
Node.js: >= 20
License
MIT
