7
4

More than 3 years have passed since last update.

parcel v2を使ってTypeScriptで書いたAWS Lambdaをビルドする

Last updated at Posted at 2020-04-01

スクリーンショット 2020-04-01 19.42.43.png

parcel v2はまだアルファ版なので今後このやり方が変わる可能性がありますが、一応やり方を載せておきます。
また、v1でも問題なくビルドでき、プラグインも揃っているので現状ではまだv1のやり方を推奨します。
以下の記事を参考にさせていただきました。

parcelだけでAWS Lambda向けのTypeScriptをビルドする - Qiita

手順

typescriptparcelをインストール。v1ではzipプラグインが使えたが、v2では互換性がないため普通にzipします。

yarn add --dev typescript parcel@next

関数を作成。ここではindex.tsという名前にします(任意)。

export const handler = () => {
  console.log('Hello World!')
  return {}
}

parcel v1では、cliのオプションで渡したがv2ではpackage.jsonに追記します。

{
  "main": "dest/bundle.js",
  "targets": {
    "main": {
      "context": "node",
      "includeNodeModules": true
    }
  }
}

出力先が、main になります。ここは任意の名前にできますが、mainはparcelで特別扱いなので楽です。
targetsでさきほどのmainを指定します。(別の名前を定義した場合はその名前をキーにしてください)
contextnode環境を対象にし、includeNodeModulesnode_modulesのモジュールを含めてビルドします。

他のオプションや詳細については、以下の公式のドキュメントを見てください。

parcel-bundler/parcel: 📦🚀 Blazing fast, zero configuration web application bundler

package.jsonのスクリプトも更新します。

{
  "main": "dest/bundle.js",
  "targets": {
    "main": {
      "context": "node",
      "includeNodeModules": true
    }
  },
  "scripts": {
    "bundle": "parcel build index.ts",
    "zip": "zip -j ${npm_package_main}.zip ${npm_package_main}",
    "deploy:aws": "aws lambda update-function-code --function-name=my-func --zip-file fileb://./${npm_package_main}.zip --region=ap-northeast-1",
    "deploy": "yarn bundle && yarn zip && yarn deploy:aws",
  }
}

aws コマンドがインストールされていれば、yarn deployでAWS Lambdaのデプロイまでいけます。

参考

parcelだけでAWS Lambda向けのTypeScriptをビルドする - Qiita


7
4
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
7
4