Package Exports
- vite-plugin-gas
Readme
vite-plugin-gas
A Vite plugin for Google Apps Script development with TypeScript support.
Features
- ๐ Individual file compilation - Compiles each TypeScript file separately for GAS compatibility
- ๐ Module statement removal - Removes import/export statements that are not supported in GAS
- ๐ก๏ธ GAS function protection - Protects special GAS functions (onEdit, onOpen, etc.) from being minified
- โก TypeScript support - Full TypeScript support with GAS API type definitions
- ๐ฏ ES5 compatibility - Targets ES5 for maximum GAS compatibility
Installation
npm install vite-plugin-gas --save-dev
# or
pnpm add vite-plugin-gas -D
# or
yarn add vite-plugin-gas --devUsage
โจ Auto-Detection Mode (Recommended)
ๆใใทใณใใซใชไฝฟ็จๆนๆณใงใใใใฉใฐใคใณใ่ชๅ็ใซ src/ ใใฃใฌใฏใใชๅ
ใฎTSใใกใคใซใๆคๅบใใพใ๏ผ
// vite.config.ts
import { defineConfig } from 'vite'
import gas from 'vite-plugin-gas'
export default defineConfig({
plugins: [
gas({
entryDir: 'src', // TSใใกใคใซใๆค็ดขใใใใฃใฌใฏใใช
target: 'es5' // GASไบๆใฎใใES5ๆจๅฅจ
})
]
})๐ Project Structure:
src/
โโโ main.ts # โ dist/main.js
โโโ utils.ts # โ dist/utils.js
โโโ triggers.ts # โ dist/triggers.js
โโโ lib/
โโโ helper.ts # โ dist/lib_helper.js๐ง Manual Configuration
ๆๅใงใจใณใใชใผใใคใณใใๆๅฎใใใๅ ดๅ๏ผ
// vite.config.ts
export default defineConfig({
plugins: [gas()],
build: {
rollupOptions: {
input: {
main: 'src/main.ts',
utils: 'src/utils.ts'
}
}
}
})Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
target |
'es5' | 'es2015' |
'es5' |
JavaScript output target |
entryDir |
string |
'src' |
Input directory to scan for TypeScript files |
outputDir |
string |
'dist' |
Output directory |
compatCheck |
boolean |
true |
Enable GAS compatibility checks |
replaceLogger |
boolean |
false |
Replace console.log with Logger.log |
removeModuleStatements |
boolean |
true |
Remove import/export statements |
preserveGasFunctions |
boolean |
true |
Protect GAS special functions from minification |
Project Structure
my-gas-project/
โโโ src/
โ โโโ main.ts # Main functions
โ โโโ triggers.ts # GAS trigger functions
โ โโโ utils/
โ โโโ helpers.ts # Utility functions
โโโ dist/ # Build output (individual files)
โ โโโ main.js
โ โโโ triggers.js
โ โโโ helpers.js
โโโ vite.config.ts
โโโ package.jsonExample Code
Input (TypeScript)
// src/main.ts
import { logMessage } from './utils/helpers'
function main() {
logMessage('Hello, GAS!')
}
// src/triggers.ts
function onOpen() {
main()
}
function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit) {
console.log('Cell edited:', e.range.getA1Notation())
}Output (JavaScript)
// dist/main.js
function logMessage(message) {
Logger.log(message);
}
function main() {
logMessage('Hello, GAS!');
}
// dist/triggers.js
/* @preserve onOpen */ function onOpen() {
main();
}
/* @preserve onEdit */ function onEdit(e) {
Logger.log('Cell edited:', e.range.getA1Notation());
}Protected GAS Functions
The plugin automatically protects these GAS special functions from minification:
onOpen()- Triggered when a spreadsheet/document is openedonEdit(e)- Triggered when a spreadsheet is editedonSelectionChange(e)- Triggered when selection changesonFormSubmit(e)- Triggered when a form is submitteddoGet(e)- HTTP GET request handlerdoPost(e)- HTTP POST request handleronInstall(e)- Triggered when an add-on is installed
Scripts
{
"scripts": {
"build": "vite build",
"dev": "vite build --watch",
"deploy": "npm run build && clasp push"
}
}Requirements
- Node.js 18.0.0 or higher
- Vite 5.0.0 or higher
Development Requirements
This project follows strict development standards:
Code Quality Standards
- Zero Warning Policy: No TypeScript warnings or linting warnings are tolerated
- Type Safety: Strict TypeScript configuration with no
anytypes - Test Coverage: Minimum 90% test coverage required (currently achieving 94.94%)
- Error Handling: Comprehensive error handling with proper runtime type checking
File Naming Convention
- camelCase: All source files use camelCase naming convention
- Examples:
gasConfigProcessor.ts(notgas-config-processor.ts)fileDetector.ts(notfile-detector.ts)viteConfig.ts(notvite-config.ts)
Testing Standards
- All new features must include comprehensive tests
- Tests must cover both success and error cases
- Mock external dependencies appropriately
- Maintain high test coverage (90%+ required)
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.