Skip to content

Commit a16815e

Browse files
committed
Add support for .mjs extension
1 parent e222b78 commit a16815e

12 files changed

Lines changed: 78 additions & 34 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ jobs:
7777
key: ${{ runner.OS }}-cache-${{ hashFiles('**/package-lock.json') }}
7878
- name: Jest
7979
working-directory: .
80-
run: npm run jest
80+
run: npm run jest -- --coverage
File renamed without changes.

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"zip": true
55
},
66
"functions": {
7-
"test": "./example/test.ts"
7+
"test": "./example/example.ts"
88
}
99
}
1010
}

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
testMatch: ['**/src/**/*.test.*'],
54
};

src/bulidFunction.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import generatePackageJson from './parts/generatePackageJson';
77
import { error } from './utils/messages';
88
import zipDirectory from './parts/zipDirectory';
99

10-
const OUTPUT_FILE_NAME = 'index.js';
10+
const OUTPUT_FILE_NAME = 'index';
1111

1212
const buildFunction = async (
1313
name: string,
@@ -23,23 +23,26 @@ const buildFunction = async (
2323
const buildResult = await useEsbuild(
2424
entrypoint,
2525
functionOutputDir,
26-
OUTPUT_FILE_NAME.replace('.js', ''),
26+
OUTPUT_FILE_NAME,
2727
esbuildConfig
2828
);
2929

30+
// Get output file extension in case it was changed from the default (.js)
31+
const outputExtension = esbuildConfig.outExtension?.['.js'] || '.js';
32+
3033
// Generate package.json
3134
const metafile = buildResult.metafile;
3235
if (!metafile) {
3336
error('Missing metafile in esbuild result!');
3437
return false;
3538
}
36-
const outputFilePath = join(outputDir, name, OUTPUT_FILE_NAME);
37-
generatePackageJson(
38-
metafile,
39-
packageObject,
40-
outputFilePath,
41-
OUTPUT_FILE_NAME
39+
40+
const outputFilePath = join(
41+
outputDir,
42+
name,
43+
`${OUTPUT_FILE_NAME}${outputExtension}`
4244
);
45+
generatePackageJson(metafile, packageObject, outputFilePath);
4346

4447
// Zip functions
4548
const shouldZip = packageObject.funpack.settings.zip;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`funpack builds correctly 1`] = `
4+
Object {
5+
"dependencies": Object {
6+
"rimraf": "3.0.2",
7+
},
8+
"main": "index.js",
9+
"repository": Object {
10+
"type": "git",
11+
"url": "https://example.com/test/repo.git",
12+
},
13+
"version": "0.0.0-dev",
14+
}
15+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`funpack builds correctly 1`] = `
4+
Object {
5+
"dependencies": Object {
6+
"rimraf": "3.0.2",
7+
},
8+
"main": "index.mjs",
9+
"version": "0.0.0-dev",
10+
}
11+
`;

src/packTests/basic.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'path';
2+
import { readFileSync } from 'fs';
23

34
import funpack from '../index';
45

@@ -23,7 +24,7 @@ jest.mock('../utils/getPackageJsonObject', () =>
2324
},
2425
},
2526
functions: {
26-
testfunc: './example/test.ts',
27+
testfunc: './example/example.ts',
2728
second: './example/second.ts',
2829
},
2930
},
@@ -34,11 +35,16 @@ describe('funpack', () => {
3435
console.log = jest.fn();
3536
it('builds correctly', async () => {
3637
await funpack();
38+
const firstPackageJson = JSON.parse(
39+
readFileSync('example/dist/testfunc/package.json').toString()
40+
);
41+
expect(firstPackageJson).toMatchSnapshot();
42+
3743
expect(console.log).toHaveBeenCalledTimes(2);
3844
expect(console.log).toBeCalledWith(
3945
join('example', 'dist', 'testfunc.zip'),
4046
'-',
41-
21113,
47+
21114,
4248
'total bytes'
4349
);
4450
expect(console.log).toBeCalledWith(

src/packTests/splitting.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'path';
2+
import { readFileSync } from 'fs';
23

34
import funpack from '../index';
45

@@ -13,18 +14,21 @@ jest.mock('../utils/getPackageJsonObject', () =>
1314
funpack: {
1415
settings: {
1516
outputDir: 'example/dist-splitting',
16-
packageFieldsToCopy: ['version', 'type'],
17+
packageFieldsToCopy: ['version'],
1718
cleanupOutputDir: true,
1819
zip: true,
1920
esbuildConfigOverride: {
2021
format: 'esm',
2122
target: 'node16',
2223
splitting: true,
2324
sourcemap: true,
25+
outExtension: {
26+
'.js': '.mjs',
27+
},
2428
},
2529
},
2630
functions: {
27-
firstSplit: './example/test.ts',
31+
firstSplit: './example/example.ts',
2832
secondSplit: './example/second.ts',
2933
},
3034
},
@@ -35,6 +39,12 @@ describe('funpack', () => {
3539
console.log = jest.fn();
3640
it('builds correctly', async () => {
3741
await funpack();
42+
43+
const firstPackageJson = JSON.parse(
44+
readFileSync('example/dist-splitting/firstSplit/package.json').toString()
45+
);
46+
expect(firstPackageJson).toMatchSnapshot();
47+
3848
// TODO: Improve test case to check for files?
3949
expect(console.log).toHaveBeenCalledTimes(2);
4050
const logMock = console.log as jest.Mock;
@@ -47,6 +57,6 @@ describe('funpack', () => {
4757
expect(logMock.mock.calls[1][0]).toBe(
4858
join('example', 'dist-splitting', 'firstSplit.zip')
4959
);
50-
expect(logMock.mock.calls[1][2]).toBeCloseTo(69275, -2);
60+
expect(logMock.mock.calls[1][2]).toBeCloseTo(69327, -2);
5161
});
5262
});

src/parts/generatePackageJson.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ describe('generatePackageJson', () => {
4040
};
4141

4242
it('calls writeFileSync with correct parameters', () => {
43-
generatePackageJson(
44-
metafileMock,
45-
packageObjectMock,
46-
'test/path/index.js',
47-
'index.js'
48-
);
43+
generatePackageJson(metafileMock, packageObjectMock, 'test/path/index.js');
4944
expect(writeFileSync).toBeCalledWith(
5045
join('test', 'path', 'package.json'),
5146
JSON.stringify(
@@ -63,8 +58,7 @@ describe('generatePackageJson', () => {
6358
generatePackageJson(
6459
metafileMock,
6560
packageObjectMock,
66-
'test\\path\\index.js',
67-
'index.js'
61+
'test\\path\\index.js'
6862
);
6963
expect(writeFileSync).toBeCalledWith(
7064
join('test', 'path', 'package.json'),

0 commit comments

Comments
 (0)