gas-webpack-plugin
Webpack plugin for Google Apps Script
AI Summary
gas-webpack-plugin
A Webpack plugin that adapts bundled code to Google Apps Script entry-point requirements.
Target Users
Developers (intermediate+) building GAS projects with Webpack/TypeScript who need to expose functions to google.script.run without manual wrappers.
Problems Solved
In GAS, functions invoked via google.script.run must be declared at top level; modular code bundled by Webpack breaks this rule.
Tags
Main Features
Automatic top-level function generation
Detects assignments to global.* and inserts matching top-level function declarations so google.script.run can access them.
Use modular code directly in GAS
Allows CommonJS/ESM modules to be bundled by Webpack and deployed to Apps Script without writing manual wrappers.
Generate global assignments from exports.*
With autoGlobalExportsFiles option the plugin converts named exports into global assignments automatically.
Optional comment generation
The comment option lets you choose whether original JSDoc comments are copied to generated functions.
Usage Examples
Expose an echo function to GAS via Webpack bundle
// echo.js
module.exports = (message) => message; // simple echo implementation
// main.js
const echo = require('./echo');
global.echo = echo; // register the function for GAS
// webpack.config.js
const GasPlugin = require('gas-webpack-plugin');
module.exports = {
context: __dirname,
entry: './main.js',
output: {
path: __dirname,
filename: 'Code.gs',
},
plugins: [new GasPlugin()], // just add the plugin
};
Run the build:
webpack --mode production
The generated Code.gs contains a top-level function echo(){}
wrapper, so it can be invoked from client HTML via google.script.run.echo()
.