Skip to content

Commit 108d04a

Browse files
vladholubievclaude
andcommitted
feat!: migrate package to ESM modules for modern JavaScript compatibility
Converted the package from CommonJS to ESM to align with modern JavaScript standards and improve tree-shaking capabilities. Added esbuild transpilation for Lambda environments to maintain backwards compatibility. BREAKING CHANGE: Package now uses ESM modules. Consumers must use ESM imports or update their build configuration to handle ESM packages. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a60ba0d commit 108d04a

File tree

10 files changed

+68
-23
lines changed

10 files changed

+68
-23
lines changed

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ yarn.lock
2626
.idea/.gitignore
2727
!.husky/_/husky.sh
2828

29-
./test/cid
30-
./test/lib
31-
./test/node_modules
29+
test/cid
30+
test/lib
31+
test/node_modules
32+
test/dist/
33+
test/test-bundled.js
3234

3335
.DS_Store

jest.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* @type {import('jest').Config}
33
*/
44
const config = {
5+
extensionsToTreatAsEsm: ['.ts'],
56
transform: {
67
'^.+\\.(t|j)sx?$': '@swc/jest',
78
},
8-
transformIgnorePatterns: ['node_modules/(?!(is-video|@shelf/is-audio-filepath)/)'],
9+
transformIgnorePatterns: ['node_modules/(?!(is-video|@shelf/is-audio-filepath|is-image)/)'],
910
resetMocks: true,
1011
coverageThreshold: {
1112
global: {

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
"email": "vlad@shelf.io",
1616
"url": "https://shelf.io"
1717
},
18-
"main": "lib",
18+
"type": "module",
19+
"main": "lib/index.js",
1920
"types": "lib/index.d.ts",
21+
"exports": {
22+
".": {
23+
"import": "./lib/index.js",
24+
"types": "./lib/index.d.ts"
25+
}
26+
},
2027
"files": [
2128
"lib"
2229
],
@@ -40,9 +47,9 @@
4047
},
4148
"prettier": "@shelf/prettier-config",
4249
"dependencies": {
43-
"@shelf/is-audio-filepath": "2.0.0",
44-
"del": "6.0.0",
45-
"is-image": "3.1.0",
50+
"@shelf/is-audio-filepath": "3.0.1",
51+
"del": "8.0.0",
52+
"is-image": "4.0.0",
4653
"is-video": "2.0.0"
4754
},
4855
"devDependencies": {
@@ -52,10 +59,11 @@
5259
"@swc/jest": "0.2.39",
5360
"@types/jest": "30.0.0",
5461
"@types/node": "^24.2.1",
62+
"esbuild": "0.25.9",
5563
"eslint": "9.33.0",
56-
"husky": "8.0.3",
64+
"husky": "9.1.7",
5765
"jest": "30.0.5",
58-
"lint-staged": "13.3.0",
66+
"lint-staged": "16.1.5",
5967
"prettier": "3.6.2",
6068
"typescript": "5.9.2"
6169
},

src/cleanup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import fs from 'fs/promises';
2-
import del from 'del';
2+
import {deleteAsync} from 'del';
33

44
// Removes temp files generated by LibreOffice
55
export async function cleanupTempFiles(): Promise<void> {
66
const files = await fs.readdir(`/tmp`);
77
for (const file of files) {
88
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
99
try {
10-
await del([`/tmp/${file}`, `/tmp/${file}/*`], {force: true});
10+
await deleteAsync([`/tmp/${file}`, `/tmp/${file}/*`], {force: true});
1111
} catch {}
1212
}
1313
}

src/convert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import childProcess from 'child_process';
22
import util from 'util';
33
import path from 'node:path';
4-
import {cleanupTempFiles} from './cleanup';
5-
import {getConvertedFilePath} from './logs';
4+
import {cleanupTempFiles} from './cleanup.js';
5+
import {getConvertedFilePath} from './logs.js';
66

77
const exec = util.promisify(childProcess.exec);
88

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './convert';
2-
export * from './validations';
1+
export * from './convert.js';
2+
export * from './validations.js';

test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM public.ecr.aws/shelf/lambda-libreoffice-base:7.6-node18-x86_64
1+
FROM public.ecr.aws/shelf/lambda-libreoffice-base:7.6-node20-x86_64
22

3-
COPY . ${LAMBDA_TASK_ROOT}/
3+
COPY test-bundled.js ${LAMBDA_TASK_ROOT}/test.js
44
CMD [ "test.handler" ]

test/build-for-lambda.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env node
2+
import {existsSync, mkdirSync} from 'fs';
3+
import * as esbuild from 'esbuild';
4+
5+
if (!existsSync('./dist')) {
6+
mkdirSync('./dist');
7+
}
8+
9+
await esbuild.build({
10+
entryPoints: ['./test.js'],
11+
bundle: true,
12+
outfile: './dist/test.js',
13+
platform: 'node',
14+
target: 'node20',
15+
format: 'cjs',
16+
external: [
17+
// Keep native modules external
18+
'child_process',
19+
'fs',
20+
'fs/promises',
21+
'path',
22+
'util',
23+
'node:path',
24+
],
25+
logLevel: 'info',
26+
});
27+
28+
console.log('Lambda handler built successfully to ./dist/test.js');

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {writeFileSync} from 'fs';
2-
import {convertTo} from './lib';
2+
import {convertTo} from './lib/index.js';
33

44
export const handler = async () => {
55
writeFileSync('/tmp/test.txt', Buffer.from('Hello World!'));

test/test.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#!/usr/bin/env bash
22

3+
# Build the ESM code to CommonJS for Lambda compatibility
4+
echo "Building Lambda handler with esbuild..."
35
cp -r ../lib ./
4-
cp -r ../node_modules ./
5-
docker build -t lo-lambda-test .
6+
node build-for-lambda.js
7+
8+
# Copy the transpiled file to the root for the Dockerfile
9+
cp ./dist/test.js ./test-bundled.js
10+
11+
podman build --platform linux/amd64 -t lo-lambda-test .
612

713
(sleep 7; curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}') &
8-
(sleep 30; CID=$(cat ./cid) && docker stop $CID && rm ./cid) &
14+
(sleep 30; CID=$(cat ./cid) && podman stop $CID && rm ./cid) &
915

10-
docker run -p 9000:8080 --rm --cidfile ./cid lo-lambda-test
16+
podman run --platform linux/amd64 -p 9000:8080 --rm --cidfile ./cid lo-lambda-test

0 commit comments

Comments
 (0)