parcel v2
はまだアルファ版なので今後このやり方が変わる可能性がありますが、一応やり方を載せておきます。
また、v1でも問題なくビルドでき、プラグインも揃っているので現状ではまだv1のやり方を推奨します。
以下の記事を参考にさせていただきました。
parcelだけでAWS Lambda向けのTypeScriptをビルドする - Qiita
手順
typescript
とparcel
をインストール。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
を指定します。(別の名前を定義した場合はその名前をキーにしてください)
context
でnode
環境を対象にし、includeNodeModules
でnode_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
parcel v2、プラグインまわりが一新されて既存のプラグインすべて動作しないの笑ってしまうな
— あかめ@無職.js (@akameco) April 1, 2020