JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 48
  • Score
    100M100P100Q40388F
  • License MIT

A Vite plugin for Google Apps Script development with TypeScript support

Package Exports

  • vite-plugin-gas

Readme

vite-plugin-gas

A Vite plugin for Google Apps Script development with TypeScript support.

Features

  • ๐Ÿš€ Multiple File Support - Handles multiple TypeScript files with individual compilation
  • ๐Ÿ”„ Module Statement Removal - Automatically removes import/export statements unsupported by GAS
  • ๐Ÿ›ก๏ธ GAS Function Protection - Preserves special GAS functions (onEdit, onOpen, etc.) from optimization
  • โšก TypeScript Support - Full TypeScript support with GAS API type definitions
  • ๐ŸŽฏ ES2017 Compatibility - Optimized for Google Apps Script runtime
  • ๐Ÿ“ Auto-Detection - Automatically detects TypeScript files in specified directories
  • ๐Ÿงน Smart File Filtering - Automatically filters out empty files and comment-only files
  • ๐Ÿ” console.log Transform - Optionally transforms console.log to Logger.log for GAS compatibility
  • ๐Ÿ“‹ appsscript.json Copy - Automatically copies appsscript.json to output directory for deployment

Installation

npm install vite-plugin-gas --save-dev
# or
pnpm add vite-plugin-gas -D
# or
yarn add vite-plugin-gas --dev

Quick Start

The simplest way to use the plugin. It automatically detects TypeScript files in your source directories:

// vite.config.ts
import { defineConfig } from 'vite'
import gas from 'vite-plugin-gas'

export default defineConfig({
  plugins: [
    gas({
      autoDetect: true,
      include: ['src', 'lib'],
      exclude: ['**/*.test.ts', '**/*.spec.ts'],
      outDir: 'dist',
      transformLogger: true
    })
  ]
})

๐Ÿ“ Project Structure Example

๐Ÿ“ Project Structure:
src/
โ”œโ”€โ”€ main.ts           # โ†’ dist/main.js
โ”œโ”€โ”€ utils/
โ”‚   โ”œโ”€โ”€ helper.ts     # โ†’ dist/utils/helper.js
โ”‚   โ””โ”€โ”€ common.ts     # โ†’ dist/utils/common.js
โ”œโ”€โ”€ triggers.ts       # โ†’ dist/triggers.js
โ””โ”€โ”€ models/
    โ””โ”€โ”€ user.ts       # โ†’ dist/models/user.js
lib/
    โ””โ”€โ”€ api.ts        # โ†’ dist/lib/api.js

Configuration Options

Option Type Default Description
autoDetect boolean true Enable automatic TypeScript file detection
include string[] ['src'] Directories to include when scanning for files
exclude string[] ['**/*.test.ts', '**/*.spec.ts'] File patterns to exclude
outDir string 'dist' Output directory for compiled files
transformLogger boolean true Replace console.log with Logger.log for GAS
copyAppsscriptJson boolean true Automatically copy appsscript.json to output directory

Advanced Usage

Manual Entry Configuration

If you prefer manual control over entry points:

// vite.config.ts
export default defineConfig({
  plugins: [
    gas({
      autoDetect: false  // Disable auto-detection
    })
  ],
  build: {
    rollupOptions: {
      input: {
        main: 'src/main.ts',
        triggers: 'src/triggers.ts',
        'utils/helper': 'src/utils/helper.ts'
      }
    }
  }
})

Working with Clasp

This plugin works seamlessly with clasp for GAS deployment:

// .clasp.json
{
  "scriptId": "your-script-id",
  "rootDir": "./dist"
}
# Build and deploy
npm run build
clasp push

appsscript.json Management

The plugin automatically copies your appsscript.json file to the output directory for seamless deployment:

// vite.config.ts
export default defineConfig({
  plugins: [
    gas({
      copyAppsscriptJson: true  // Default: true
    })
  ]
})

Project Structure:

project/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.ts
โ”‚   โ””โ”€โ”€ utils.ts
โ”œโ”€โ”€ appsscript.json      # Source manifest
โ””โ”€โ”€ dist/               # Build output
    โ”œโ”€โ”€ main.js
    โ”œโ”€โ”€ utils.js
    โ””โ”€โ”€ appsscript.json  # Automatically copied

Benefits:

  • Ensures manifest file is always included in deployments
  • Maintains consistency between source and build directories
  • Works seamlessly with clasp deployment workflow
  • Can be disabled if you prefer manual manifest management

Handling Empty Files

The plugin gracefully handles various file states:

  • โœ… Empty files (0 bytes)
  • โœ… Whitespace-only files
  • โœ… Comment-only files
  • โœ… Files with only type definitions โ”‚ โ””โ”€โ”€ utils/ โ”‚ โ””โ”€โ”€ helpers.ts # Utility functions

โ”œโ”€โ”€ dist/ # Build output (individual files) โ”‚ โ”œโ”€โ”€ main.js โ”‚ โ”œโ”€โ”€ triggers.js โ”‚ โ””โ”€โ”€ helpers.js โ”œโ”€โ”€ vite.config.ts โ””โ”€โ”€ package.json


### Example Code

#### Input (TypeScript)

```typescript
// src/main.ts
import { logMessage } from './utils/helpers'

function main() {
  logMessage('Hello, GAS!')
}

// src/triggers.ts
function onOpen() {
  main()
## Example

### Input (TypeScript)

```typescript
// src/main.ts
import { helper } from './utils/helper'

export function main() {
  console.log('Hello, GAS!')
  helper()
}

// src/utils/helper.ts
export function helper() {
  console.log('Helper function called')
}

// 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 helper() {
  Logger.log('Helper function called');
}

function main() {
  Logger.log('Hello, GAS!');
  helper();
}

// dist/utils/helper.js
function helper() {
  Logger.log('Helper function called');
}

// dist/triggers.js
function onOpen() {
  main();
}

function onEdit(e) {
  Logger.log('Cell edited:', e.range.getA1Notation());
}

GAS Special Functions

The plugin automatically preserves these Google Apps Script special functions:

  • onOpen() - Triggered when a spreadsheet/document is opened
  • onEdit(e) - Triggered when a spreadsheet is edited
  • onSelectionChange(e) - Triggered when selection changes
  • onFormSubmit(e) - Triggered when a form is submitted
  • doGet(e) - HTTP GET request handler
  • doPost(e) - HTTP POST request handler
  • onInstall(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

Quality Assurance

This project maintains high quality standards:

Development Standards

  • Test Coverage: >90% maintained across all modules
  • camelCase Convention: Consistent file naming throughout the project

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT ยฉ 11gather11

Changelog

See CHANGELOG.md for details about changes in each version.

  • 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 (not gas-config-processor.ts)
    • fileDetector.ts (not file-detector.ts)
    • viteConfig.ts (not vite-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.