0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Firebase Cloud Functionsで関数を分割させてくれや〜ってなったのでメモ

Last updated at Posted at 2020-12-31

前提

  • node: v10.23.0
  • yarn: 1.22.10

やりたいこと

  • index.tsにまとまっていて、分割したい
src配下
.
└── index.ts
  • こんな感じで関数ごとに分けたい
src配下
.
├── hoge2.ts
└── index.ts

やったこと

  • index.ts からhogeをimportしてexportする
index.ts
import * as hoge from './hoge';

export {
  hoge,
};

hoge.ts
import * as functions from 'firebase-functions';

export const aaa = functions.region('asia-northeast1')
.https.onRequest(async (req: any, res: any) => {
  console.log('hoge');
  res.status(200).send('ok');
})

問題

  • hogeが読めないと怒られる
    • Unable to resolve path to module './hoge'

対応

cd function
yarn add eslint-import-resolver-typescript
  • eslintrc.js に以下を追加
  settings: {
    'import/resolver': {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    }
  }

おわりに

上記で関数ごとに分割できました〜。
ファイル分けられると見通しよくていいね!!
関数化している人もいるので、そのうち試してみたいです。
https://uyamazak.hatenablog.com/entry/2018/10/22/113000

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?