OUCC

BLOG

記事一覧 タグ一覧

ESModuleをGASで実行できるようにする

投稿日:

exportした関数のエントリーポイントを自動で生成するesbuildプラグイン esbuild-plugin-gas-generator を作成しました。

GitHub - miyaji255/esbuild-plugin-gas-generator: This plugin allows you to execute functions exported within Google Apps Script (GAS) environment.
This plugin allows you to execute functions exported within Google Apps Script (GAS) environment. - miyaji255/esbuild-plugin-gas-generator
GitHub - miyaji255/esbuild-plugin-gas-generator: This plugin allows you to execute functions exported within Google Apps Script (GAS) environment. favicon https://github.com/miyaji255/esbuild-plugin-gas-generator
GitHub - miyaji255/esbuild-plugin-gas-generator: This plugin allows you to execute functions exported within Google Apps Script (GAS) environment.
esbuild-plugin-gas-generator
This plugin allows you to execute functions exported within Google Apps Script (GAS) environment.. Latest version: 1.0.0, last published: 11 days ago. Start using esbuild-plugin-gas-generator in your project by running `npm i esbuild-plugin-gas-generator`. There are no other projects in the npm registry using esbuild-plugin-gas-generator.
esbuild-plugin-gas-generator favicon https://www.npmjs.com/package/esbuild-plugin-gas-generator
esbuild-plugin-gas-generator

使い方

build.jsに以下のように記述することで使用できます。プラグインの都合上bundleとformatはtrue'esm'であることが必須です。

出力ファイルはoutfileかオプションのtargetsで指定する。

const esbuild = require('esbuild');
const GasPlugin = require('esbuild-plugin-gas-generator');

esbuild.build({
    entryPoints: ['src/index.ts'],
    bundle: true,
    format: 'esm',    
    outfile: 'dist/bundle.js',
    plugins: [GasPlugin()],

    // targetsで指定する場合
    plugins: [
        GasPlugin({
            targets: ['dist/bundle.js']
        })
    ]
}).catch((e) => {
    console.error(e);
    process.exit(1);
});

エントリーポイントでexportすると自動でGAS用のエントリーポイントを生成します。

export function hello() {
    console.log("hello")
}

実装

meriyahでパースしてexportした変数をすべて以下のように変換します

function hello() {}
(()=>{
function hello() {
    console.log("hello")
}

globalThis.hello=hello;
})()