nodejs-gas-bundler
v1.0.0
Published
Node.js bundler for Google Apps Script
Readme
Node.js Bundler for Google Apps Script
A powerful tool that bundles Node.js modules (including NPM packages) into a single JavaScript file compatible with Google Apps Script (GAS). This enables you to use thousands of NPM libraries directly in your Google Apps Script projects.
The tool can be used in command-line, or programatically as a library with Rollup.
📦 Installation
Global Installation (Recommended)
npm install -g nodejs-gas-bundlerLocal Installation
npm install nodejs-gas-bundler --save-devUsing npx (No Installation Required)
npx nodejs-gas-bundler --help🎯 Quick Start
1. Create Your Entry Point
Create an index.js file that exports what you want to use in Google Apps Script:
// Export your own functions
export function add(x, y) {
return x + y;
}
export function subtract(x, y) {
return x - y;
}
// Export from NPM packages
export { marked } from 'marked';2. Install Dependencies
npm init -y
npm install marked3. Bundle for Google Apps Script
To generate the Google Apps Script bundle into bundle.js, run:
bundle-for-gas -i index.js -o bundle.js -n MyLibWith the -n MyLib option, all the definitions will be added to a top-level object MyLib. If you prefer
the definitions to be added to the global object, you can use the -g option instead:
bundle-for-gas -i index.js -o bundle.js -gThis is especially useful if you intend to use the generated bundled as a library within Google Apps Script projects.
4. Import and use in Google Apps Script
Copy the generated bundle.js content into your Google Apps Script project and use it:
// If you used -n MyLib
const result = MyLib.add(5, 3);
const html = MyLib.marked('# Hello World');
// If you used -g flag
const result = add(5, 3);
const html = marked('# Hello World');You can also upload your bundled code to Google Apps Script using the clasp command-line tool. For improved organization and easy reuse, consider publishing your bundle as a Google Apps Script library. This approach makes it simple to share your code and use it across multiple Apps Script projects.
📖 Usage
Command-line interface
Basic Syntax
bundle-for-gas -i <input> -o <output> [options]Options
| Option | Alias | Description | Default |
|--------|-------|-------------|---------|
| --input | -i | Path to source JavaScript file | Required |
| --output | -o | Path to output bundle file | Required |
| --global-name | -n | Name of the global variable | '' |
| --merge-into-global | -g | Merge exports into global object | false |
| --help | -h | Show help information | - |
Examples
Basic Bundle with Namespace
bundle-for-gas -i src/index.js -o dist/bundle.js -n MyLibraryGlobal Variable Exposure
bundle-for-gas -i src/index.js -o dist/bundle.js -gCombined Options
bundle-for-gas -i src/index.js -o dist/bundle.js -n MyLib -gLibrary
You can also use this package as a library in your build scripts:
import { createRollupConfig } from 'nodejs-gas-bundler';
import { rollup } from 'rollup';
async function myBundle() {
const config = createRollupConfig({
inputFile: 'src/index.js',
outputFile: 'dist/bundle.js',
globalName: 'MyLibrary',
mergeIntoGlobal: false
});
const bundle = await rollup(config);
await bundle.write(config.output);
}
myBundle().catch(console.error);The above process is encapsulated in the bundle function:
import { bundle } from 'nodejs-gas-bundler';
bundle({
inputFile: 'src/index.js',
outputFile: 'dist/bundle.js',
globalName: 'MyLibrary',
mergeIntoGlobal: false
}).catch(console.error);Full example
A full example is available in the examples directory. This example bundles two popular NPM libraries:
csv-parse(a CSV parser),marked(a Markdown parser)
and uploads them in an existing Google Apps Script using clasp. You can see the resulting project here.
⚠️ Limitations & Considerations
Google Apps Script Environment
- No Node.js APIs: Node.js-specific modules won't work (fs, path, etc.)
- Memory Limits: Large bundles may hit GAS memory limits
- Execution Time: Complex operations may timeout
Polyfills
The bundler includes basic polyfills for common Node.js globals, but some packages may require additional configuration.
🙏 Credits
This project uses the following open source tools:
📝 TODOs
- Polyfill for fetch using UrlFetchApp
