gas-webpack-plugin
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');
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
function echo() {
} (function(modules) {
var installedModules = {};
function __webpack_require__(moduleId) {
if(installedModules[moduleId])
return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
i: moduleId,
l: false,
exports: {}
};
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
module.l = true;
return module.exports;
}
__webpack_require__.m = modules;
__webpack_require__.c = installedModules;
__webpack_require__.i = function(value) { return value; };
__webpack_require__.d = function(exports, name, getter) {
if(!__webpack_require__.o(exports, name)) {
Object.defineProperty(exports, name, {
configurable: false,
enumerable: true,
get: getter
});
}
};
__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;
};
__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
__webpack_require__.p = "";
return __webpack_require__(__webpack_require__.s = 2);
})
([
(function(module, exports) {
var g;
g = (function() {
return this;
})();
try {
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
if(typeof window === "object")
g = window;
}
module.exports = g;
}),
(function(module, exports) {
module.exports = function(message) {
return message;
};
}),
(function(module, exports, __webpack_require__) {
(function(global) {var echo = __webpack_require__(1);
global.echo = echo;
}.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:
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