diff --git a/packages/lambda-tiler/bundle.sh b/packages/lambda-tiler/bundle.sh index c5ec1b22a4..85a7bd9362 100755 --- a/packages/lambda-tiler/bundle.sh +++ b/packages/lambda-tiler/bundle.sh @@ -4,8 +4,8 @@ # ../../scripts/bundle.js package.json cd dist +../scripts/create.deployment.package.js # Make the new package a commonjs module -cat ../package.json | grep -v '"type": "module"' > package.json cp -r ../static . # @see https://sharp.pixelplumbing.com/en/stable/install/#aws-lambda -npm install --arch=x64 --platform=linux sharp +npm install --arch=x64 --platform=linux --production diff --git a/packages/lambda-tiler/package.json b/packages/lambda-tiler/package.json index 672ae936d6..69635e23ba 100644 --- a/packages/lambda-tiler/package.json +++ b/packages/lambda-tiler/package.json @@ -11,7 +11,7 @@ "url": "https://linz.govt.nz", "organization": true }, - "type": "module", + "type": "commonjs", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, diff --git a/packages/lambda-tiler/scripts/create.deployment.package.js b/packages/lambda-tiler/scripts/create.deployment.package.js new file mode 100755 index 0000000000..8f2f98328e --- /dev/null +++ b/packages/lambda-tiler/scripts/create.deployment.package.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node +/** + * Create a pacakge.json for the `dist` bundle based off the parent package.json + * + * This is needed as libsharp has to be "installed" into the dist node_modules so that lambda has access to sharp + */ +const fs = require('fs'); + +const parentPackage = JSON.parse(fs.readFileSync('../package.json').toString()); + +// Find the exact version of a package in the yarn lock +function getPackageVersion(packageName) { + const parentLock = fs.readFileSync('../../../yarn.lock').toString().split('\n'); + + for (let i = 0; i < parentLock.length; i++) { + if (parentLock[i].startsWith(packageName + '@')) { + const versionList = parentLock[i + 1].trim(); + if (!versionList.startsWith('version ')) throw new Error('Failed to find sharp version'); + return JSON.parse(versionList.slice('version '.length)); + } + } +} + +// the bundle is a commonjs module +parentPackage.type = 'commonjs'; +parentPackage.main = 'index.js'; +parentPackage.dependencies = { sharp: getPackageVersion('sharp') }; + +// Clean up +delete parentPackage.types; +delete parentPackage.devDependencies; + +console.log('Installing dependencies', parentPackage.dependencies); +fs.writeFileSync('./package.json', JSON.stringify(parentPackage, null, 2));