Skip to content

Commit f4d9512

Browse files
Merge pull request #203 from aligent/feat/MI-251-nx-cdk-improvements
MI-251: Extract vite-plugin-handler and improve nx-cdk preset
2 parents 1470593 + c3d4813 commit f4d9512

26 files changed

Lines changed: 2205 additions & 1350 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
vite-plugin-handler: minor
3+
nx-cdk: minor
4+
---
5+
6+
Extract Lambda handler bundling into a standalone Vite plugin (`@aligent/vite-plugin-handler`) and update the nx-cdk preset to use it instead of inline vite config.

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Aligent's TypeScript monorepo for microservice development utilities.
1515
| `packages/nx-appbuilder` | Nx plugin with generators for Adobe App Builder apps |
1616
| `packages/nx-cdk` | Nx plugin with generators for AWS CDK projects |
1717
| `packages/nx-openapi` | Nx plugin with generators for OpenAPI client generation |
18+
| `packages/vite-plugin-handler` | Vite plugin for bundling Lambda handlers as ESM |
1819

1920
## Commands
2021

@@ -65,6 +66,7 @@ npx nx g @tools/generators:package
6566
```
6667

6768
Common mappings: `Core.Logger``@adobe/aio-lib-core-logging`, `Core.Config``@adobe/aio-lib-core-config`, `Events``@adobe/aio-lib-events`, `State``@adobe/aio-lib-state`, `Files``@adobe/aio-lib-files`. The generated app's `eslint.config.mjs` ships a `no-restricted-imports` rule that enforces this.
69+
6870
- **Don't read `process.env` inside action handlers.** App Builder routes runtime configuration through OpenWhisk `params` (declared as `inputs:` in `app.config.yaml`); `process.env` is not reliably propagated between activations. The generated app's eslint config flags `process.env.*` reads under `src/**/actions/**`.
6971

7072
## Workflow

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Aligent's monorepo for Microservice Development Utilities. For more details abou
1111
- [Nx App Builder](/packages/nx-appbuilder/README.md)
1212
- [Nx CDK](/packages/nx-cdk/README.md)
1313
- [Nx Openapi](/packages/nx-openapi/README.md)
14+
- [Vite Plugin Handler](/packages/vite-plugin-handler/README.md)
1415

1516
# Development
1617

@@ -48,6 +49,7 @@ microservice-development-utilities/
4849
│ ├── nx-appbuilder/ # Nx plugin with generators for Adobe App Builder apps
4950
│ ├── nx-cdk/ # Nx plugin for CDK project generation
5051
│ ├── nx-openapi/ # Nx plugin for OpenAPI code generation
52+
│ ├── vite-plugin-handler/ # Vite plugin for bundling Lambda handlers
5153
└── package.json # Root package configuration
5254
```
5355

package-lock.json

Lines changed: 1484 additions & 1198 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
},
2828
"devDependencies": {
2929
"@aligent/ts-code-standards": "^4.2.0",
30-
"@nx/devkit": "22.7.5",
31-
"@nx/eslint": "22.7.5",
32-
"@nx/eslint-plugin": "22.7.5",
33-
"@nx/js": "22.7.5",
34-
"@nx/plugin": "22.7.5",
35-
"@nx/rollup": "22.7.5",
36-
"@nx/vitest": "22.7.5",
37-
"@nx/web": "22.7.5",
30+
"@nx/devkit": "22.7.6",
31+
"@nx/eslint": "22.7.6",
32+
"@nx/eslint-plugin": "22.7.6",
33+
"@nx/js": "22.7.6",
34+
"@nx/plugin": "22.7.6",
35+
"@nx/rollup": "22.7.6",
36+
"@nx/vitest": "22.7.6",
37+
"@nx/web": "22.7.6",
3838
"@smithy/util-stream": "^3.2.1",
3939
"@swc-node/register": "1.11.1",
4040
"@swc/core": "1.15.41",
@@ -44,26 +44,26 @@
4444
"@vitest/coverage-v8": "4.1.8",
4545
"@vitest/ui": "4.1.8",
4646
"aws-sdk-client-mock": "^4.1.0",
47-
"create-nx-workspace": "22.7.5",
47+
"create-nx-workspace": "22.7.6",
4848
"eslint": "^9.39.0",
4949
"eslint-config-prettier": "10.1.8",
5050
"eslint-plugin-import": "^2.32.0",
5151
"jiti": "2.7.0",
5252
"jsonc-eslint-parser": "^2.4.1",
53-
"nx": "22.7.5",
53+
"nx": "22.7.6",
5454
"oauth-sign": "^0.9.0",
5555
"openapi-fetch": "0.17.0",
5656
"openapi-typescript-helpers": "0.1.0",
5757
"prettier": "^3.8.4",
5858
"prompts": "^2.4.2",
5959
"rollup": "^4.62.0",
60-
"tslib": "^2.3.0",
61-
"typedoc": "^0.28.14",
60+
"tslib": "^2.8.1",
61+
"typedoc": "^0.28.19",
6262
"typedoc-plugin-markdown": "^4.12.0",
6363
"typescript": "^5.9.3",
6464
"verdaccio": "6.7.2",
65-
"vite": "7.3.1",
66-
"vitest": "4.1.8",
65+
"vite": "^8.0.14",
66+
"vitest": "^4.1.7",
6767
"yargs": "^18.0.0"
6868
},
6969
"overrides": {

packages/nx-cdk/src/generators/helpers/configs/base-package/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
"typecheck": "nx affected -t typecheck",
1212
"typecheck:all": "nx run-many -t typecheck",
1313
"audit": "nx run-many -t lint typecheck test --configuration coverage --skip-nx-cache",
14-
"pg:synth": "nx run application:pg:synth --mode=development --profile playground",
15-
"pg:deploy": "nx run application:pg:deploy --mode=development --method=direct --require-approval never --profile playground",
16-
"pg:diff": "nx run application:pg:diff --mode=development --profile playground",
17-
"pg:destroy": "nx run application:pg:destroy --mode=development --profile playground",
14+
"pg:synth": "nx run application:cdk -- synth '**' --mode=development --profile playground",
15+
"pg:deploy": "nx run application:cdk -- deploy '**' --mode=development --method=direct --require-approval never --profile playground",
16+
"pg:diff": "nx run application:cdk -- diff '**' --mode=development --profile playground",
17+
"pg:destroy": "nx run application:cdk -- destroy '**' --mode=development --profile playground",
1818
"postinstall": "[ -d .git ] && git config core.hooksPath '.git-hooks' && chmod +x .git-hooks/* || true"
1919
},
2020
"dependencies": {
21-
"@aligent/microservice-util-lib": "^1.2.0"
21+
"@aligent/microservice-util-lib": "^1.6.0"
2222
},
2323
"devDependencies": {
2424
"@aligent/cdk-aspects": "^0.6.3",
2525
"@aligent/cdk-nodejs-function-from-entry": "^0.2.3",
2626
"@aligent/cdk-step-function-from-file": "^0.5.3",
2727
"@aligent/nx-openapi": "^2.1.2",
2828
"@aligent/ts-code-standards": "^5.0.0",
29+
"@aligent/vite-plugin-handler": "^0.1.0",
2930
"@nx/eslint": "22.7.3",
3031
"@nx/eslint-plugin": "22.7.3",
3132
"@nx/js": "22.7.3",
@@ -65,5 +66,5 @@
6566
"libs/*",
6667
"services/*"
6768
],
68-
"packageManager": "yarn@4.16.0+sha512.5374c94eb4ef6aa8188fb112f20c1aa6569f248d676c5e576e1fd2a1a4d8d87a96df65d9dfe1c2a0252cbe38bda46cf18d955005b81b43cc7607a5c9d56fd2b6"
69+
"packageManager": "yarn@4.17.0+sha512.c2957de2f9025ab14d63b24d0d8be1f1655810e22c341042c27f7ecd017b180ec12db73d69ac366d71b304ef9f069349ce462de96f04f8f1da317f4f762c95ae"
6970
}

packages/nx-cdk/src/generators/helpers/configs/nxJson.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { NxJsonConfiguration } from '@nx/devkit';
22
import { SERVICES_SCOPE } from '../../constants';
33

4-
export const NX_JSON: NxJsonConfiguration & { $schema: string } = {
4+
export const NX_JSON: NxJsonConfiguration = {
55
$schema: './node_modules/nx/schemas/nx-schema.json',
6+
neverConnectToCloud: true,
7+
analytics: false,
68
defaultBase: 'origin/staging',
79
sync: { applyChanges: true },
810
plugins: [
@@ -40,8 +42,5 @@ export const NX_JSON: NxJsonConfiguration & { $schema: string } = {
4042
cdk: {
4143
dependsOn: [{ target: 'build', params: 'forward', projects: `${SERVICES_SCOPE}/*` }],
4244
},
43-
pg: {
44-
dependsOn: [{ target: 'build', params: 'forward', projects: `${SERVICES_SCOPE}/*` }],
45-
},
4645
},
4746
} as const;

packages/nx-cdk/src/generators/preset/files/application/README.md.template

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,44 @@ This application manages the deployment of microservices and their shared infras
2020

2121
```bash
2222
# Synthesize for default environment/stage (dev)
23-
yarn pg:synth --args="dev/**"
23+
yarn pg:synth
2424

2525
# Synthesize for a custom environment/stage
26-
yarn pg:synth --args="plg/** -c environment=plg"
26+
yarn pg:synth -c environment=plg
2727
```
2828

2929
##### Deploy
3030

3131
```bash
3232
# Deploy for default environment/stage (dev)
33-
yarn pg:deploy --args="dev/**"
33+
yarn pg:deploy
3434

3535
# Deploy to a custom environment/stage
36-
yarn pg:deploy --args="plg/** -c environment=plg"
36+
yarn pg:deploy -c environment=plg
3737
```
3838

3939
##### Diff
4040

4141
```bash
4242
# Diff checking for default environment/stage (dev)
43-
yarn pg:diff --args="dev/**"
43+
yarn pg:diff
4444

4545
# Diff checking for a custom environment/stage
46-
yarn pg:diff --args="plg/** -c environment=plg"
46+
yarn pg:diff -c environment=plg
4747
```
4848

4949
##### Destroy
5050

5151
```bash
5252
# Destroy default environment/stage (dev)
53-
yarn pg:destroy --args="dev/**"
53+
yarn pg:destroy
5454

5555
# Destroy a custom environment/stage
56-
yarn pg:destroy --args="plg/** -c environment=plg"
56+
yarn pg:destroy -c environment=plg
5757
```
5858

5959
### Direct Nx Commands
6060

6161
```bash
62-
yarn nx run application:cdk <command> <args>
63-
```
64-
65-
## Testing
66-
67-
### Mock Services
68-
69-
The application may include mock services for testing integrations without external dependencies.
70-
71-
<!-- list mock services here -->
72-
73-
Change the value of the `/application/dev/url` SSM Parameter to switch between real and mock endpoints
74-
75-
### Local Development
76-
77-
```bash
78-
# Type checking
79-
yarn typecheck
80-
81-
# Testing
82-
yarn test
83-
84-
# Linting
85-
yarn lint
62+
yarn nx run application:cdk -- <command> <args>
8663
```

packages/nx-cdk/src/generators/preset/files/application/package.json.template

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,6 @@
1616
"command": "cdk",
1717
"cwd": "{projectRoot}"
1818
}
19-
},
20-
"pg": {
21-
"executor": "nx:run-commands",
22-
"configurations": {
23-
"synth": {
24-
"command": "cdk synth"
25-
},
26-
"deploy": {
27-
"command": "cdk deploy"
28-
},
29-
"diff": {
30-
"command": "cdk diff"
31-
},
32-
"destroy": {
33-
"command": "cdk destroy"
34-
}
35-
},
36-
"options": {
37-
"color": true,
38-
"cwd": "{projectRoot}"
39-
}
4019
}
4120
}
4221
}
Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { globSync } from 'node:fs';
2-
import { builtinModules } from 'node:module';
3-
import { extname, resolve } from 'node:path';
1+
import { resolve } from 'node:path';
42
import { defineConfig } from 'vite';
53

64
export const baseConfig = defineConfig(() => {
@@ -38,69 +36,3 @@ export const baseConfig = defineConfig(() => {
3836
},
3937
};
4038
});
41-
42-
// ESM doesn't define __dirname/__filename. This shim ensures bundled CJS dependencies
43-
// that reference them won't throw a ReferenceError at runtime.
44-
// TODO: [MI-251] Support shims only when needed instead consider looking at tsup, tsdown and rollup shims plugins
45-
const shimBanner = `import { fileURLToPath as __shim_fileURLToPath } from 'node:url';
46-
import { dirname as __shim_dirname } from 'node:path';
47-
const __filename = __shim_fileURLToPath(import.meta.url);
48-
const __dirname = __shim_dirname(__filename);`;
49-
50-
/**
51-
* Creates Vite environments for bundling Lambda handlers using the Vite Environment API.
52-
* Each handler file gets its own environment that outputs to dist/<entryName>/index.mjs.
53-
*
54-
* This uses Vite's built-in `mode` to control build behaviour.
55-
* See https://vite.dev/guide/env-and-mode
56-
*/
57-
export function defineLambdaEnvironments(subPath, options = {}, envPrefix = 'lambda') {
58-
// When running vitest, skip the Lambda environments entirely
59-
if (process.env.VITEST === 'true') return {};
60-
61-
if (subPath.includes('..')) throw new Error('Invalid path provided');
62-
63-
const handlersPath = resolve(process.cwd(), subPath);
64-
const handlers = globSync(`${handlersPath}/**/*.ts`);
65-
66-
const environments = {};
67-
for (const handler of handlers) {
68-
const bundledPath = handler.replace(`${handlersPath}/`, '');
69-
const entryName = bundledPath.replace(extname(bundledPath), '');
70-
const envName = `${envPrefix}_${entryName.replace(/[\\/]/g, '_')}`;
71-
72-
environments[envName] = {
73-
resolve: { noExternal: true },
74-
build: {
75-
outDir: `dist/${entryName}`,
76-
minify: options.mode === 'development' ? 'oxc' : false,
77-
sourcemap: options.mode !== 'production',
78-
platform: 'node',
79-
rollupOptions: {
80-
input: { index: handler },
81-
moduleTypes: { ...options.moduleTypes },
82-
external: [...builtinModules, ...builtinModules.map(m => `node:${m}`)],
83-
output: {
84-
entryFileNames: 'index.mjs',
85-
format: 'esm',
86-
comments: { legal: false },
87-
codeSplitting: false,
88-
banner: shimBanner,
89-
},
90-
},
91-
},
92-
};
93-
}
94-
95-
return {
96-
environments,
97-
builder: {
98-
buildApp: async builder => {
99-
const lambdaEnvs = Object.entries(builder.environments)
100-
.filter(([name]) => name.startsWith(`${envPrefix}_`))
101-
.map(([, env]) => env);
102-
await Promise.all(lambdaEnvs.map(env => builder.build(env)));
103-
},
104-
},
105-
};
106-
}

0 commit comments

Comments
 (0)