Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gas-webpack-plugin

fossamagna17.3kMIT2.6.0TypeScript support: included

Webpack Plugin for Google Apps Script

readme

gas-webpack-plugin NPM version Build Status Coverage percentage

Webpack plugin for Google Apps Script.

About

In Google Apps Script, an entry point called from google.script.run must be a top level function declaration on the server side. gas-webpack-plugin detects function assignment expressions to global object and generate a corresponding top level function declaration statement.

example

main.js:

var echo = require('./echo');
/**
 * Return write arguments.
 */
global.echo = echo;

echo.js:

module.exports = function(message) {
  return message;
}

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()
  ]
}

build:

$ webpack --mode production

Code.gs

/**
 * Return write arguments.
 */
function echo() {
}/******/ (function(modules) { // webpackBootstrap
/******/     // The module cache
/******/     var installedModules = {};
/******/
/******/     // The require function
/******/     function __webpack_require__(moduleId) {
/******/
/******/         // Check if module is in cache
/******/         if(installedModules[moduleId])
/******/             return installedModules[moduleId].exports;
/******/
/******/         // Create a new module (and put it into the cache)
/******/         var module = installedModules[moduleId] = {
/******/             i: moduleId,
/******/             l: false,
/******/             exports: {}
/******/         };
/******/
/******/         // Execute the module function
/******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/         // Flag the module as loaded
/******/         module.l = true;
/******/
/******/         // Return the exports of the module
/******/         return module.exports;
/******/     }
/******/
/******/
/******/     // expose the modules object (__webpack_modules__)
/******/     __webpack_require__.m = modules;
/******/
/******/     // expose the module cache
/******/     __webpack_require__.c = installedModules;
/******/
/******/     // identity function for calling harmony imports with the correct context
/******/     __webpack_require__.i = function(value) { return value; };
/******/
/******/     // define getter function for harmony exports
/******/     __webpack_require__.d = function(exports, name, getter) {
/******/         if(!__webpack_require__.o(exports, name)) {
/******/             Object.defineProperty(exports, name, {
/******/                 configurable: false,
/******/                 enumerable: true,
/******/                 get: getter
/******/             });
/******/         }
/******/     };
/******/
/******/     // getDefaultExport function for compatibility with non-harmony modules
/******/     __webpack_require__.n = function(module) {
/******/         var getter = module && module.__esModule ?
/******/             function getDefault() { return module['default']; } :
/******/             function getModuleExports() { return module; };
/******/         __webpack_require__.d(getter, 'a', getter);
/******/         return getter;
/******/     };
/******/
/******/     // Object.prototype.hasOwnProperty.call
/******/     __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/     // __webpack_public_path__
/******/     __webpack_require__.p = "";
/******/
/******/     // Load entry module and return exports
/******/     return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

var g;

// This works in non-strict mode
g = (function() {
    return this;
})();

try {
    // This works if eval is allowed (see CSP)
    g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
    // This works if the window reference is available
    if(typeof window === "object")
        g = window;
}

// g can still be undefined, but nothing to do about it...
// We return undefined, instead of nothing here, so it's
// easier to handle this case. if(!global) { ...}

module.exports = g;


/***/ }),
/* 1 */
/***/ (function(module, exports) {

module.exports = function(message) {
  return message;
};


/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {

/* WEBPACK VAR INJECTION */(function(global) {var echo = __webpack_require__(1);
global.echo = echo;

/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)))

/***/ })
/******/ ]);

Installation

$ npm install gas-webpack-plugin --save-dev

Usage

CLI

$ webpack --mode production

Options

You can pass a hash of configuration options to gas-webpack-plugin. Allowed values are as follows

Name Type Default Description
comment {Boolean} true If true then generate a top level function declaration statement with comment.
autoGlobalExportsFiles {Array<String>} [] Array of source file paths that to generate global assignments expression from exports.* statements.
include {Array<String>} [**/*] Array of path patterns to detect functions to generate top level function definitions. accept glob pattern.

Geranate global assignment expressions from exports.*

Assignments expression to global object is automatically generated from named exports (exports.*) Included in the file specified by the autoGlobalExportsFiles option.

main.ts:

import './echo';

echo.ts:

// geranate global assignment expressions from named export
export const echo = (message) => message;

webpack.config.js:

const GasPlugin = require("gas-webpack-plugin");
module.exports = {
  context: __dirname,
  entry: "./main.ts",
  module: {
    rules: [
      {
        test: /(\.ts)$/,
        loader: 'ts-loader',
      },
    ],
  },
  resolve: {
    extensions: [".ts"],
  },
  output: {
    path: __dirname ,
    filename: 'Code.js'
  },
  plugins: [
    new GasPlugin({
      autoGlobalExportsFiles: ['**/*.ts']
    })
  ]
}

Webpack version support

gas-webpack-plugin is support for Webpack@5.x

changelog

2.6.0 (2024-05-23)

Features

  • detect export function if autoGlobalExportsFiles is enabled (#909) (99925f6)

2.5.0 (2023-04-25)

2.4.0 (2023-04-25)

Features

  • support export named declaration when use autoGlobalExportsFiles (#795) (83eb97c)
  • support export named declaration when use autoGlobalExportsFiles (#801) (03fe306)

2.3.0 (2022-06-20)

2.2.2 (2022-06-15)

2.2.2-beta.0 (2022-06-15)

Bug Fixes

  • use position of original source to insert global assignments (4c76d11)

2.2.1 (2022-03-31)

Bug Fixes

  • make sure using slash as path separator of include path pattern on Windows (e959c1c)

2.2.0 (2022-03-10)

Features

  • add include option to define path patterns to detect functions (4f2c1dc)

2.1.0 (2021-07-30)

Features

2.0.2 (2021-06-07)

Bug Fixes

  • ReferenceError: "global" is not defined when use autoGlobalExportsFiles option (80553bf), closes #561

2.0.1 (2021-03-26)

Bug Fixes

  • Make it possible to use ES module in production mode (3183834), closes #516

2.0.0 (2021-02-27)

Features

1.2.2 (2020-11-29)

1.2.1 (2020-11-18)

Bug Fixes

  • Avoid to fail bundling when has module ignored by webpack (dfd9262)

1.2.0 (2020-09-11)

Features

  • Add support auto generation of global assignment expressions from exports.* (f681381)

1.1.0 (2020-09-04)

Features

1.0.5 (2020-08-24)

Bug Fixes

  • Enable to work with es3ify and source map (2f8fa79)

1.0.4 (2020-07-27)

Bug Fixes

  • Fix to prepend top-level functions when minimize too. (684c3d2)

1.0.3 (2020-06-24)

1.0.2 (2019-01-21)

Bug Fixes

  • Add test for SequenceExpression (033859b), closes #208

1.0.1 (2018-12-20)

1.0.0 (2018-12-20)

Features

0.3.0 (2018-05-13)

0.2.1 (2017-08-04)

Bug Fixes

  • package: update gas-entry-generator to version 1.0.1 (337c3a2)

0.2.0 (2017-03-25)

0.1.0 (2016-11-23)