Skip to content

Commit 8629016

Browse files
antstanleydreamorosi
authored andcommitted
feat(logger): add esmodule support (#1734)
* feat(logger): add esm build output * fix(Logger): Remove barrel files update references * test(Logger): update jest/ts-jest to use ESM * chore(Logger): remove unused lodash.merge * fix(logger): reinstate lodash.merge * chore(logger): revert TS assertion * chore(logger): revert format changes * chore(logger): update postbuild to remove incremental tsbuildinfo files * fix(logger): correct reference to types output * feat(logging): add middleware export * chore(logger): replace postbuild script with echo statement * feat(logger): add typesVersions property and barrel files to /types * chore(logger): file not used, can be added back if needed * chore(logger): add space back to README * chore(logger): revert space in README
1 parent 5c8b2b1 commit 8629016

29 files changed

+165
-93
lines changed

Diff for: .gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ tmp
5050

5151
# TS build files
5252
tsconfig.tsbuildinfo
53-
.tsbuildinfo
53+
<<<<<<< HEAD
54+
.tsbuildinfo
55+
=======
56+
tsconfig.esm.tsbuildinfo
57+
>>>>>>> 0bc7960c (feat(logger): add esmodule support (#1734))

Diff for: packages/logger/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ Credits for the Powertools for AWS Lambda (TypeScript) idea go to [DAZN](https:/
118118

119119
## License
120120

121-
This library is licensed under the MIT-0 License. See the LICENSE file.
121+
This library is licensed under the MIT-0 License. See the LICENSE file.

Diff for: packages/logger/jest.config.js renamed to packages/logger/jest.config.cjs

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ module.exports = {
44
color: 'cyan',
55
},
66
runner: 'groups',
7-
preset: 'ts-jest',
7+
extensionsToTreatAsEsm: ['.ts'],
8+
moduleNameMapper: {
9+
'^(\\.{1,2}/.*)\\.js$': '$1',
10+
},
811
transform: {
9-
'^.+\\.ts?$': 'ts-jest',
12+
'^.+\\.[tj]sx?$': [
13+
'ts-jest',
14+
{
15+
useESM: true,
16+
},
17+
],
1018
},
1119
moduleFileExtensions: ['js', 'ts'],
1220
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],

Diff for: packages/logger/package.json

+40-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,55 @@
1818
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
1919
"test:e2e": "jest --group=e2e",
2020
"watch": "jest --watch --group=unit",
21-
"build": "tsc --build --force",
21+
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
22+
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
23+
"build": "npm run build:esm & npm run build:cjs",
2224
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
2325
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
2426
"prebuild": "rimraf ./lib",
27+
"postbuild": "rimraf ./lib/*.tsbuildinfo",
2528
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2629
},
2730
"lint-staged": {
2831
"*.{js,ts}": "npm run lint-fix"
2932
},
3033
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/logger#readme",
3134
"license": "MIT-0",
32-
"main": "./lib/index.js",
33-
"types": "./lib/index.d.ts",
35+
"type": "module",
36+
"exports": {
37+
".": {
38+
"require": {
39+
"types": "./lib/cjs/index.d.ts",
40+
"default": "./lib/cjs/index.js"
41+
},
42+
"import": {
43+
"types": "./lib/esm/index.d.ts",
44+
"default": "./lib/esm/index.js"
45+
}
46+
},
47+
"./middleware": {
48+
"import": "./lib/esm/middleware/middy.js",
49+
"require": "./lib/cjs/middleware/middy.js"
50+
},
51+
"./types": {
52+
"import": "./lib/esm/types/index.js",
53+
"require": "./lib/cjs/types/index.js"
54+
}
55+
},
56+
"typesVersions": {
57+
"*": {
58+
"middleware": [
59+
"lib/cjs/middleware/middy.d.ts",
60+
"lib/esm/middleware/middy.d.ts"
61+
],
62+
"types": [
63+
"lib/cjs/types/index.d.ts",
64+
"lib/esm/types/index.d.ts"
65+
]
66+
}
67+
},
68+
"types": "./lib/cjs/index.d.ts",
69+
"main": "./lib/cjs/index.js",
3470
"devDependencies": {
3571
"@aws-lambda-powertools/testing-utils": "file:../testing",
3672
"@types/lodash.merge": "^4.6.9"
@@ -66,4 +102,4 @@
66102
"serverless",
67103
"nodejs"
68104
]
69-
}
105+
}

Diff for: packages/logger/src/Logger.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@ import { Console } from 'node:console';
33
import { format } from 'node:util';
44
import type { Context, Handler } from 'aws-lambda';
55
import { Utility } from '@aws-lambda-powertools/commons';
6-
import { LogFormatterInterface, PowertoolsLogFormatter } from './formatter';
7-
import { LogItem } from './log';
6+
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
7+
import { LogFormatterInterface } from './formatter/LogFormatterInterface.js';
8+
import { LogItem } from './log/LogItem.js';
89
import merge from 'lodash.merge';
9-
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
10-
import { LogJsonIndent } from './types';
10+
import { ConfigServiceInterface } from './config/ConfigServiceInterface.js';
11+
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
12+
import { LogJsonIndent } from './types/Logger.js';
1113
import type {
12-
ClassThatLogs,
1314
Environment,
15+
LogAttributes,
16+
LogLevel,
17+
LogLevelThresholds,
18+
} from './types/Log.js';
19+
import type {
20+
ClassThatLogs,
1421
HandlerMethodDecorator,
1522
LambdaFunctionContext,
16-
LogAttributes,
1723
ConstructorOptions,
1824
LogItemExtraInput,
1925
LogItemMessage,
20-
LogLevel,
21-
LogLevelThresholds,
2226
PowertoolLogData,
2327
HandlerOptions,
24-
} from './types';
25-
28+
} from './types/Logger.js';
2629
/**
2730
* ## Intro
2831
* The Logger utility provides an opinionated logger with output structured as JSON.

Diff for: packages/logger/src/config/EnvironmentVariablesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConfigServiceInterface } from './ConfigServiceInterface';
1+
import { ConfigServiceInterface } from './ConfigServiceInterface.js';
22
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
33

44
/**

Diff for: packages/logger/src/config/index.ts

-2
This file was deleted.

Diff for: packages/logger/src/formatter/LogFormatter.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { LogFormatterInterface } from '.';
2-
import { LogAttributes, UnformattedAttributes } from '../types';
3-
import { LogItem } from '../log';
1+
import { LogFormatterInterface } from './LogFormatterInterface.js';
2+
import { LogAttributes } from '../types/Log.js';
3+
import { UnformattedAttributes } from '../types/Logger.js';
4+
import { LogItem } from '../log/LogItem.js';
45

56
/**
67
* Typeguard to monkey patch Error to add a cause property.

Diff for: packages/logger/src/formatter/LogFormatterInterface.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { LogAttributes, UnformattedAttributes } from '../types';
2-
import { LogItem } from '../log';
1+
import { LogAttributes } from '../types/Log.js';
2+
import { UnformattedAttributes } from '../types/Logger.js';
3+
import { LogItem } from '../log/LogItem.js';
34

45
/**
56
* @interface

Diff for: packages/logger/src/formatter/PowertoolsLogFormatter.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { LogFormatter } from '.';
2-
import { LogAttributes, UnformattedAttributes } from '../types';
3-
import { PowertoolsLog } from '../types/formats';
4-
import { LogItem } from '../log';
1+
import { LogFormatter } from './LogFormatter.js';
2+
import { LogAttributes } from '../types/Log.js';
3+
import { UnformattedAttributes } from '../types/Logger.js';
4+
import { PowertoolsLog } from '../types/formats/PowertoolsLog.js';
5+
import { LogItem } from '../log/LogItem.js';
56

67
/**
78
* This class is used to transform a set of log key-value pairs

Diff for: packages/logger/src/formatter/index.ts

-3
This file was deleted.

Diff for: packages/logger/src/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export * from './Logger';
2-
export * from './middleware';
3-
export * from './formatter';
1+
export { Logger } from './Logger.js';
2+
export { injectLambdaContext } from './middleware/middy.js';
3+
export { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
4+
export { LogFormatter } from './formatter/LogFormatter.js';
5+
export { LogFormatterInterface } from './formatter/LogFormatterInterface.js';

Diff for: packages/logger/src/log/LogItem.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import merge from 'lodash.merge';
2-
import { LogItemInterface } from '.';
3-
import { LogAttributes } from '../types';
2+
import { LogItemInterface } from './LogItemInterface.js';
3+
import { LogAttributes } from '../types/Log.js';
44

55
class LogItem implements LogItemInterface {
66
private attributes: LogAttributes = {};

Diff for: packages/logger/src/log/LogItemInterface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LogAttributes } from '../types';
1+
import { LogAttributes } from '../types/Log.js';
22

33
interface LogItemInterface {
44
addAttributes(attributes: LogAttributes): void;

Diff for: packages/logger/src/log/index.ts

-2
This file was deleted.

Diff for: packages/logger/src/middleware/index.ts

-1
This file was deleted.

Diff for: packages/logger/src/middleware/middy.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Logger } from '../Logger';
2-
import { HandlerOptions, LogAttributes } from '../types';
1+
import { Logger } from '../Logger.js';
2+
import { LogAttributes } from '../types/Log.js';
3+
import { HandlerOptions } from '../types/Logger.js';
34
import { LOGGER_KEY } from '@aws-lambda-powertools/commons/lib/middleware';
45
import type {
56
MiddlewareLikeObj,

Diff for: packages/logger/src/types/Logger.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
SyncHandler,
55
} from '@aws-lambda-powertools/commons';
66
import { Handler } from 'aws-lambda';
7-
import { ConfigServiceInterface } from '../config';
8-
import { LogFormatterInterface } from '../formatter';
7+
import { ConfigServiceInterface } from '../config/ConfigServiceInterface.js';
8+
import { LogFormatterInterface } from '../formatter/LogFormatterInterface.js';
99
import {
1010
Environment,
1111
LogAttributes,
1212
LogAttributesWithMessage,
1313
LogLevel,
14-
} from './Log';
14+
} from './Log.js';
1515

1616
type ClassThatLogs = {
1717
[key in Exclude<Lowercase<LogLevel>, 'silent'>]: (

Diff for: packages/logger/src/types/formats/PowertoolsLog.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LogAttributes, LogLevel } from '..';
1+
import type { LogAttributes, LogLevel } from '../Log.js';
22

33
type PowertoolsLog = LogAttributes & {
44
/**

Diff for: packages/logger/src/types/formats/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './PowertoolsLog';
1+
export type { PowertoolsLog } from './PowertoolsLog.js';

Diff for: packages/logger/src/types/index.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1-
export * from './Log';
2-
export * from './Logger';
1+
export type {
2+
LogAttributesWithMessage,
3+
LogAttributeValue,
4+
Environment,
5+
LogLevelThresholds,
6+
LogAttributes,
7+
LogLevel,
8+
} from './Log.js';
9+
10+
export type {
11+
ClassThatLogs,
12+
LogItemMessage,
13+
LogItemExtraInput,
14+
HandlerMethodDecorator,
15+
LambdaFunctionContext,
16+
UnformattedAttributes,
17+
PowertoolLogData,
18+
ConstructorOptions,
19+
HandlerOptions,
20+
} from './Logger.js';

Diff for: packages/logger/tests/tsconfig.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
2-
"extends": "../tsconfig.json",
3-
"compilerOptions": {
4-
"rootDir": "../",
5-
"noEmit": true
6-
},
7-
"include": [
8-
"../src/**/*",
9-
"./**/*",
10-
]
11-
}
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "../",
5+
"noEmit": true
6+
},
7+
"include": ["../src/**/*", "./**/*"]
8+
}

Diff for: packages/logger/tests/unit/Logger.test.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import {
88
Events as dummyEvent,
99
LambdaInterface,
1010
} from '@aws-lambda-powertools/commons';
11-
import { Logger } from '../../src';
12-
import {
13-
ConfigServiceInterface,
14-
EnvironmentVariablesService,
15-
} from '../../src/config';
16-
import { PowertoolsLogFormatter, LogFormatter } from '../../src/formatter';
11+
import { Logger } from '../../src/Logger.js';
12+
import { ConfigServiceInterface } from '../../src/config/ConfigServiceInterface.js';
13+
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
14+
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js';
15+
import { LogFormatter } from '../../src/formatter/LogFormatter.js';
16+
import { LogLevelThresholds, LogLevel } from '../../src/types/Log.js';
1717
import {
1818
ClassThatLogs,
1919
LogJsonIndent,
2020
ConstructorOptions,
21-
LogLevelThresholds,
22-
LogLevel,
23-
} from '../../src/types';
21+
} from '../../src/types/Logger.js';
2422
import type { Context } from 'aws-lambda';
2523
import { Console } from 'console';
2624

Diff for: packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @group unit/logger/all
55
*/
6-
import { EnvironmentVariablesService } from '../../../src/config';
6+
import { EnvironmentVariablesService } from '../../../src/config/EnvironmentVariablesService.js';
77

88
describe('Class: EnvironmentVariablesService', () => {
99
const ENVIRONMENT_VARIABLES = process.env;

Diff for: packages/logger/tests/unit/middleware/middy.test.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import {
88
Events as dummyEvent,
99
} from '@aws-lambda-powertools/commons';
1010
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons/lib/middleware';
11-
import {
12-
ConfigServiceInterface,
13-
EnvironmentVariablesService,
14-
} from '../../../src/config';
15-
import { injectLambdaContext } from '../../../src/middleware/middy';
16-
import { Logger } from './../../../src';
11+
import { ConfigServiceInterface } from '../../../src/config/ConfigServiceInterface.js';
12+
import { EnvironmentVariablesService } from '../../../src/config/EnvironmentVariablesService.js';
13+
import { injectLambdaContext } from '../../../src/middleware/middy.js';
14+
import { Logger } from './../../../src/Logger.js';
1715
import middy from '@middy/core';
18-
import { PowertoolsLogFormatter } from '../../../src/formatter';
19-
import { Console } from 'console';
16+
import { PowertoolsLogFormatter } from '../../../src/formatter/PowertoolsLogFormatter.js';
17+
import { Console } from 'node:console';
2018
import { Context } from 'aws-lambda';
2119

2220
const mockDate = new Date(1466424490000);

Diff for: packages/logger/tsconfig.esm.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.esm.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"outDir": "./lib/esm",
6+
"rootDir": "./src"
7+
},
8+
"include": ["./src/**/*"]
9+
}

Diff for: packages/logger/tsconfig.json

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"extends": "../../tsconfig.json",
3-
"compilerOptions": {
4-
"outDir": "./lib",
5-
"rootDir": "./src",
6-
},
7-
"include": [
8-
"./src/**/*"
9-
],
10-
}
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./lib/cjs/",
5+
"rootDir": "./src"
6+
},
7+
"include": ["./src/**/*"]
8+
}

Diff for: packages/logger/typedoc.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"extends": ["../../typedoc.base.json"],
3-
"entryPoints": [
4-
"./src/index.ts",
5-
"./src/types"
6-
],
3+
"entryPoints": ["./src/index.ts", "./src/types"],
74
"readme": "README.md"
8-
}
5+
}

0 commit comments

Comments
 (0)