Skip to content

Commit

Permalink
build: bundle the exact version of sharp from the yarn.lock into the …
Browse files Browse the repository at this point in the history
…lambda-tiler bundle
  • Loading branch information
blacha committed Mar 17, 2022
1 parent 3872370 commit 95a40d6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/lambda-tiler/bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion packages/lambda-tiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
34 changes: 34 additions & 0 deletions packages/lambda-tiler/scripts/create.deployment.package.js
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 95a40d6

Please sign in to comment.