前提
- 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'
対応
-
以下を参考にした
-
function配下に、path解決のライブラリをinstall
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