gas-webpack-plugin

raw JSON →
2.6.0 verified Sat Apr 25 auth: no javascript

Webpack plugin for Google Apps Script that transforms module exports into top-level function declarations compatible with google.script.run. Current version 2.6.0, maintained regularly. It bridges the gap between webpack's module system and GAS's requirement that callable functions be globally declared. Supports both CommonJS and ES module syntax, works with webpack 4 and 5. Key differentiator: automates generation of GAS-compatible wrapper code from standard webpack bundles, avoiding manual function declarations.

error Global function not defined in Google Apps Script
cause Top-level function not generated because assignment was not detected.
fix
Confirm gas-webpack-plugin is correctly installed and webpack config includes the plugin. Use global.xxx = xxx assignment at top-level of entry module.
error Uncaught ReferenceError: ... is not defined when calling from client side
cause The function is not exposed globally due to improper assignment.
fix
Ensure the function is assigned to global object in the entry point: global.myFunction = myFunction;
gotcha Functions assigned to global must be top-level; nested assignments may not be detected.
fix Ensure assignments like global.myFunc = myFunc are at the top level of entry file, not inside conditionals or callbacks.
gotcha Plugin outputs Code.gs; ensure no file naming conflicts if multiple outputs.
fix Use output.filename option to customize; or update to >=2.5.0 for better behavior.
npm install gas-webpack-plugin
yarn add gas-webpack-plugin
pnpm add gas-webpack-plugin

Sets up webpack with gas-webpack-plugin to bundle code for Google Apps Script, converting global assignments to top-level function declarations.

// webpack.config.js
const GasPlugin = require('gas-webpack-plugin');
const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'Code.gs'
  },
  plugins: [
    new GasPlugin()
  ]
};

// src/index.js
const myFunc = require('./myFunc');
global.myFunc = myFunc;

// src/myFunc.js
module.exports = function(message) {
  return message;
};