Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

13 changes: 13 additions & 0 deletions .changeset/modern-aspects-migrate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@toss/nestjs-aop': major
---

Ship ESM only and require Node.js 22.12.0 or later. Drop NestJS 8 and 9 support;
NestJS 10, 11 and 12 remain supported. The public decorator runtime API is unchanged.

Consumers must resolve the package through its root export; undocumented `dist/*` and `src/*` deep imports are no longer exported.
CommonJS applications can use Node's synchronous `require(esm)` support, but older
CommonJS Jest configurations need migration to Jest ESM mode. CommonJS TypeScript
consumers using NodeNext need TypeScript 5.8 or later; node16 does not support
this interop. See the [migration guide](https://github.com/toss/nestjs-aop/blob/main/docs/migrations/v3.md)
for tested Node/Jest combinations and migration instructions.
62 changes: 52 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ jobs:
- name: Lint
run: pnpm lint

- name: Typecheck source and tests
run: pnpm typecheck

- name: Test packaging assertions
run: pnpm test:packaging

- name: Build
run: |
pnpm build
Expand All @@ -29,15 +35,16 @@ jobs:
run: |
pnpm test

# Every major version listed in `peerDependencies` gets built and tested here,
# so the support range in package.json stays an actually verified claim.
# Reproducible Node/Nest pairs exercise repository tests and packed consumers.
# compat-range below separately covers Nest minimums and current major ranges.
compat:
name: NestJS v${{ matrix.nest }}
name: NestJS ${{ matrix.nest }} / Node ${{ matrix.node }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
nest: [8, 9, 10, 11, 12]
nest: [10.4.22, 11.2.6, 12.1.0]
node: [22.12.0, 24.19.0, 26.10.0]
steps:
- uses: actions/checkout@v7

Expand All @@ -50,13 +57,48 @@ jobs:
- name: Pin @nestjs/* to v${{ matrix.nest }}
run: >-
pnpm add -D
"@nestjs/common@^${{ matrix.nest }}"
"@nestjs/core@^${{ matrix.nest }}"
"@nestjs/platform-fastify@^${{ matrix.nest }}"
"@nestjs/testing@^${{ matrix.nest }}"
"@nestjs/common@${{ matrix.nest }}"
"@nestjs/core@${{ matrix.nest }}"
"@nestjs/platform-fastify@${{ matrix.nest }}"
"@nestjs/testing@${{ matrix.nest }}"

- name: Install runtime under test
run: mise install node@${{ matrix.node }}

- name: Typecheck source and tests
run: mise exec node@${{ matrix.node }} -- pnpm typecheck

- name: Test packaging assertions
run: mise exec node@${{ matrix.node }} -- pnpm test:packaging

- name: Build
run: pnpm build
run: mise exec node@${{ matrix.node }} -- pnpm build

- name: Test
run: pnpm test
run: mise exec node@${{ matrix.node }} -- pnpm test

- name: Pack and test independent consumers
run: |
mise exec node@${{ matrix.node }} -- pnpm pack --out /tmp/nestjs-aop.tgz
mise exec node@${{ matrix.node }} -- npm run test:packed -- /tmp/nestjs-aop.tgz "${{ matrix.nest }}"

compat-range:
name: NestJS range ${{ matrix.nest }} / Node 24.19.0
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
nest: ['10.0.0', '11.0.0', '12.0.1', '^10', '^11', '^12']
steps:
- uses: actions/checkout@v7

- name: Setup toolchain (node/pnpm via mise)
uses: jdx/mise-action@v4

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Pack and test minimum/current Nest consumers
run: |
pnpm pack --out /tmp/nestjs-aop.tgz
npm run test:packed -- /tmp/nestjs-aop.tgz "${{ matrix.nest }}"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ English | [한국어](https://github.com/toss/nestjs-aop/blob/main/readme_kr.md)
- 🎯 **Decorator-based AOP** — wrap any method, getter, or setter with reusable cross-cutting logic (caching, logging, retries, ...)
- 🧩 **Plays nice with the IoC container** — your aspect is a regular NestJS provider, so `@Inject` anything you need
- 🪶 **Zero runtime magic** — built on native decorators + `reflect-metadata`, no code transformation step
- ✅ **NestJS 8 → 12** — one package, no major-version treadmill
- ✅ **NestJS 10 → 12** — one package, no major-version treadmill
- 🧬 **Inheritance-aware** — decorated methods keep working when called through a subclass

<!-- INSTALLATION -->
Expand All @@ -63,10 +63,10 @@ pnpm add @toss/nestjs-aop
yarn add @toss/nestjs-aop
```

> **Using NestJS 12?** NestJS 12 ships as ESM only, while this package is published as CommonJS.
> Loading it therefore relies on Node's `require(esm)` support, so NestJS 12 users need
> **Node.js `^20.19.0 || >=22.12.0`**. On Node 20.x below 20.19.0 or Node 22.x below 22.12.0, the import fails with `ERR_REQUIRE_ESM`.
> NestJS 8 - 11 users are unaffected.
> **Requirements:** Node.js >= 22.12.0 · NestJS 10, 11 or 12 · ESM build.
> CommonJS apps can load it through `require(esm)`. CJS + TypeScript NodeNext needs TS >= 5.8; `node16` does not support this interop.
> Jest needs ESM mode + `--experimental-vm-modules`, or the tested Jest 30.4.2 CJS setup on Node >= 24.9 with the same flag.
> Upgrading from v2? See the [migration guide](https://github.com/toss/nestjs-aop/blob/main/docs/migrations/v3.md).

<!-- QUICK START -->

Expand Down
180 changes: 180 additions & 0 deletions docs/migrations/v3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# Migrating to v3

v3 ships one ESM build and changes the supported Node.js and testing toolchains.
NestJS 10, 11 and 12 remain supported with the requirements below. Existing
NestJS starter projects may need compiler and Jest configuration changes.

## Breaking changes

- Node.js 22.12.0 or later is required. CI tests Node 22.12.0, 24.19.0 and 26.10.0.
- NestJS 8 and 9 are no longer supported. Use v2 if you cannot upgrade yet.
- The package contains one ESM build, not separate ESM and CommonJS builds.
- Only the root entry is exported. Replace
undocumented `dist/*` or `src/*` imports with imports from `@toss/nestjs-aop`.
- Older CommonJS Jest setups need migration even if the application itself runs.

NestJS 10, 11 and 12 and the existing decorator runtime API remain supported.
The npm tarball includes only built `dist` files plus npm-required metadata,
README and license. Source TypeScript, tests and this migration guide stay in the
repository. JavaScript source maps embed their original source; declaration maps
are disabled because the source tree is not shipped.

Package version, runtime Node version, TypeScript module mode and Jest loader
settings are independent compatibility dimensions.

## Application imports

```ts
import { AopModule, Aspect, createDecorator } from '@toss/nestjs-aop';
import type { LazyDecorator, WrapParams } from '@toss/nestjs-aop';
```

For an ESM TypeScript application, set `"type": "module"` in its package.json and
use `"module": "NodeNext"` and `"moduleResolution": "NodeNext"`. Include `.js`
extensions in local relative imports. Preserve `experimentalDecorators` and
`emitDecoratorMetadata` for Nest's legacy decorators and dependency injection.

CommonJS applications may use `require('@toss/nestjs-aop')` on the supported Node
versions. This loads the same synchronous ESM artifact; it is not a CJS build.
Do not infer Jest compatibility from a successful native Node `require()` call.
On Node 22.12.0, `require(esm)` emits an `ExperimentalWarning`; the application
still runs. In the Node 22 line, 22.13.0 removes that default warning; the tested
Node 24/26 releases do not emit it either.

### TypeScript consumers

The compiler requirement depends on how the application emits its imports:

- **ESM application:** NodeNext with `type: module` (or `.mts`) works. Both
TypeScript 5.7.3 and 5.8.3 are covered by the packed-consumer checks.
- **CommonJS application with NodeNext:** use **TypeScript 5.8 or later**.
TypeScript 5.7 rejects the generated `require(esm)` with TS1479 even when the
installed Node version can execute it.
- **CommonJS application with `module: node16` or `node18`:** these modes model
older Node interop and reject `require(esm)`. Upgrade to NodeNext with TS >= 5.8,
or migrate the application to ESM. Updating Node alone does not fix this error.
- **Existing `module: commonjs` + `moduleResolution: node`/`node10`:** the top-level
`types` field keeps the legacy resolver working. TypeScript 5.7.3 and 5.8.3 are
checked in this mode; the emitted application still needs the supported Node
runtime. This is an existing-project compatibility path, not a recommendation
to switch a new project away from Node-aware module resolution.

These are separate from the Jest requirements below. A successful `tsc` build
and native application startup do not establish that a default CJS Jest setup
will work.

## Jest: portable ESM setup

Configure the transformer to emit ESM and launch Jest with VM modules enabled.
For a TypeScript consumer with ts-jest, the following configuration is tested
against the packed package with Jest 29.7.0 and 30.4.2 / ts-jest 29.4.12.
CI checks that this example exactly matches the configuration it runs:

```js
// jest.config.cjs (the .cjs extension also works in a type:module project)
module.exports = {
testMatch: ['**/*.test.ts'],
extensionsToTreatAsEsm: ['.ts'],
transform: {
'^.+\\.ts$': [
'ts-jest',
{
useESM: true,
tsconfig: {
module: 'ESNext',
moduleResolution: 'Bundler',
esModuleInterop: true,
experimentalDecorators: true,
emitDecoratorMetadata: true,
},
},
],
},
};
```

```sh
node --experimental-vm-modules node_modules/jest/bin/jest.js
```

When local source imports use `.js` extensions but point to TypeScript source,
configure the transform/resolver for that layout. The library's source tests use
`moduleNameMapper: { '^(\\.{1,2}/.*)\\.js$': '$1' }`. Do not remap package imports
to this repository's source in consumer tests: verify the installed package.

In ESM tests, import `jest` from `@jest/globals` when needed. ESM mock setup differs
from CJS `jest.mock`; review Jest's `jest.unstable_mockModule` guidance rather than
assuming existing module mocks are unchanged.

### Keeping CommonJS Jest tests

Upgrading to Jest 30 is not required if you use the ESM setup above. If you want
to retain CJS test output instead, the tested combination is Jest 30.4.2 with
Node >= 24.9 and `--experimental-vm-modules`. Jest 29 CJS output still fails
with that flag. On Node 22, use ESM test output. Mocking ESM dependencies still
requires ESM-aware mocks even when the test file itself is CommonJS.

## Tested compatibility

The packed candidate is installed into independent consumers with Nest
10.4.22, 11.2.6 and 12.1.0. Each is checked on Node 22.12.0, 24.19.0 and 26.10.0.

- Native ESM and CommonJS: actual Nest application bootstrap, aspect execution,
before/original/after ordering, return values, undecorated methods, lifecycle
completion and repeated calls pass.
- Jest 30.4.2 ESM with `--experimental-vm-modules`: passes on all tested nodes.
- Jest 30.4.2 CJS with that flag: passes on tested Node 24/26; fails on Node 22.12.0.
Use the ESM setup on Node 22.
- TypeScript 5.8.3 NodeNext `.mts` and `.cts`, TypeScript 5.7.3 NodeNext `.mts`,
and both versions in CommonJS/node10 mode pass with `skipLibCheck: false`,
including decorator metadata settings. CI also checks the expected TS1479
failures for TS 5.7.3 CJS/NodeNext and TS 5.8.3 CJS/node16.
- The exact ts-jest ESM configuration above bootstraps Nest, injects dependencies,
and checks aspect ordering, return values, lifecycle and repeated calls with
Jest 29.7.0 and 30.4.2.

- With Nest 11.2.6, ts-jest 29.4.12 and Node 24.19.0, Jest 29.7.0 CJS fails
even with the VM flag; switching its transformer/test mode to ESM passes.
Jest 30.4.2 CJS and ESM configurations pass with the same VM flag.

These are exact tested versions, not a claim that every earlier Jest or
TypeScript version works. The same VM flag is required for the tested newer CJS
Jest configuration. A default CJS Jest command without it is not supported here.

Alongside the reproducible matrix, CI checks baseline Nest releases
10.0.0 / 11.0.0 / 12.0.1 and latest releases matching `^10` / `^11` / `^12` on
Node 24.19.0. These cases exercise the installed package rather than claiming
that every historical patch and toolchain combination is covered. The Nest 10.0.0
minimum case uses `skipLibCheck: true` because that release has upstream TS2416
errors in its file-validator declarations; the pinned and latest Nest cases keep
`skipLibCheck: false`. The application import/compiler-boundary checks still run
for that minimum case. Nest 12.0.0 has inconsistent package peers
(`@nestjs/core@12.0.0` requires `@nestjs/common@^11`), so the baseline uses the
corrected 12.0.1 pair without bypassing peer validation.

The compatibility CI installs the tarball and independent peer dependencies,
not symlinked workspace source. Run the same check after building:

```sh
pnpm pack --out /tmp/nestjs-aop.tgz
npm run test:packed -- /tmp/nestjs-aop.tgz 12.1.0
```

The verifier uses the current Node executable. Run it through `npm run test:packed` on
Windows so it can invoke the npm CLI without executing a `.cmd` shim or shell.
Use a platform-appropriate tarball path in the commands above.

## v2.2.0 baseline

Before this migration, the published npm 2.2.0 package was installed with Nest
12.1.0 in a fresh consumer. Native CJS and ESM apps passed on Node 24.19.0.
Jest 30.4.2 CJS passed with the VM flag. Its ESM static-import consumer hit a Jest
mixed CJS/ESM loader error; sequentially awaiting imports of `@nestjs/common` and
`@nestjs/core` before dynamically importing the consumer avoided that error.
The v3 packed ESM artifact passes the static-import ESM Jest fixture directly.

## References

- [Node require(esm)](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require)
- [TypeScript NodeNext format detection](https://www.typescriptlang.org/docs/handbook/modules/reference.html#module-format-detection)
- [Jest ESM and require(esm)](https://jestjs.io/docs/30.4/ecmascript-modules)
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import tseslint from 'typescript-eslint';

export default tseslint.config(
{
ignores: ['dist/**', 'coverage/**', 'esm/**'],
ignores: ['dist/**', 'coverage/**'],
},
js.configs.recommended,
...tseslint.configs.recommended,
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
export default {
roots: ['<rootDir>'],
moduleNameMapper: { '^(\\.{1,2}/.*)\\.js$': '$1' },
testMatch: ['**/*.test.ts'],
extensionsToTreatAsEsm: ['.ts'],
transform: {
Expand Down
26 changes: 17 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "@toss/nestjs-aop",
"version": "2.2.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers 4",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --maxWorkers 4",
"build": "rm -rf dist && tsc -p tsconfig.build.json",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"changeset": "changeset",
"release": "changeset publish"
"release": "changeset publish",
"prepack": "npm run build",
"test:packed": "node scripts/verify-packed.mjs",
"typecheck": "tsc -p tsconfig.json --noEmit",
"test:packaging": "node --test scripts/verify-source-maps.test.mjs"
},
"files": [
"dist",
"src"
"dist"
],
"peerDependencies": {
"@nestjs/common": "^8 || ^9 || ^10 || ^11 || ^12",
"@nestjs/core": "^8 || ^9 || ^10 || ^11 || ^12",
"@nestjs/common": "^10 || ^11 || ^12",
"@nestjs/core": "^10 || ^11 || ^12",
"reflect-metadata": "^0.1.13 || ^0.2.0",
"rxjs": "^7.5.6"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^3.0.3",
"@eslint/js": "^9.39.5",
Expand Down Expand Up @@ -69,5 +69,13 @@
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"type": "module",
"engines": {
"node": ">=22.12.0"
},
"exports": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
Loading
Loading