Giter Club home page Giter Club logo

clasp-technique's Introduction

clasp technique

Motivation

Google Apps Script では全てのソースコードが一つのファイルにまとまったように、いわば cat *.js | node - のように動作する。一方、clasp を使ってローカルで開発する場合、そのままでは各ファイルは別ファイルの内容を認識できないので、コーディング時にエラーが発生する。

例えば、以下は Google Apps Script では整合だがローカルでは IDE がエラーを発する。このエラーを解消し、参照を利用できる環境構築方法を模索した。

project-01/greet.js

const greet = "Hello world!";

project-01/say.js

function say() {
  return greet; // Cannot find name 'greet'.
}

Solution

ソースディレクトリに以下のファイルを配置すると TS server が当該ディレクトリ、および配下の全てのファイルの内容を解析する。このため上述のファイル構成はエラーではなくなり、参照も機能する(project-01/say.js を開いて ⌘ を押しながら greet をクリックすると、定義が記述されたファイル project-01/greet.js が開かれる)。

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true, // JSファイルの検証を有効化する
    "checkJs": true, // 型エラーを警告する
    "noEmit": true // ファイル出力を抑制することでエラー "Cannot write file '*.js' because it would overwrite input file." を防ぐ
    // "lib": ["ES2016"], // エラーが出たら設定する
    // "target": "ES5" // エラーが出たら設定する
  },
  "include": ["./"] // 無くても良いが念のため明示する
}

Google Apps Script の V8 エンジンがどのバージョンの ECMAScript に準拠しているかは公表されていないため基本的に compilerOptions.libcompilerOptions.target は設定せず、エラーが出たときに設定する。

Workaround

@types/google-apps-script で定義されている型はサーバー側が解釈する型と異なる。例えばサーバー側では UrlFetchApp のレスポンスの型は UrlFetchApp.HTTPResponse であるが、ローカルでは GoogleAppsScript.URL_Fetch.HTTPResponse になる。この不一致により、ローカルに合わせるとサーバー側で補完が効かなくなり、サーバー側に合わせるとこれ加えて IDE がエラーを発してしまう。現時点でこの問題の解決策は無いようなので、ローカルでエラーが出ないようコーディングしてサーバー側で補完が効かないことを許容するほかなさそうである。

https://stackoverflow.com/questions/73503410/incompatible-type-notation-in-jsdoc-for-google-apps-script-online-vs-locally

clasp-technique's People

Contributors

takuyahara avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.