From 5b4b4cc5fb25136b9b535922b48988c7fedca971 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Thu, 28 Nov 2024 09:06:17 -0700 Subject: [PATCH] fix(nest): update project config to enable artifacts to be built as dev closed #26761 --- .../application/application.spec.ts | 56 ++++++++++++++++++- .../src/generators/application/application.ts | 19 +++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/packages/nest/src/generators/application/application.spec.ts b/packages/nest/src/generators/application/application.spec.ts index 9bb132cc26458..3be9fd03f1e18 100644 --- a/packages/nest/src/generators/application/application.spec.ts +++ b/packages/nest/src/generators/application/application.spec.ts @@ -15,17 +15,66 @@ describe('application generator', () => { it('should generate project configurations', async () => { await applicationGenerator(tree, { directory: appDirectory, + addPlugin: true, }); const projectConfigurations = devkit.getProjects(tree); + const project = projectConfigurations.get(appDirectory); - expect(projectConfigurations.get(appDirectory)).toBeTruthy(); expect(projectConfigurations.get(`${appDirectory}-e2e`)).toBeTruthy(); + expect(project).toMatchInlineSnapshot(` + { + "$schema": "../node_modules/nx/schemas/project-schema.json", + "name": "my-node-app", + "projectType": "application", + "root": "my-node-app", + "sourceRoot": "my-node-app/src", + "tags": [], + "targets": { + "build": { + "configurations": { + "development": { + "args": [ + "node-env=development", + ], + }, + }, + "executor": "nx:run-commands", + "options": { + "args": [ + "node-env=production", + ], + "command": "webpack-cli build", + }, + }, + "serve": { + "configurations": { + "development": { + "buildTarget": "my-node-app:build:development", + }, + "production": { + "buildTarget": "my-node-app:build:production", + }, + }, + "defaultConfiguration": "development", + "dependsOn": [ + "build", + ], + "executor": "@nx/js:node", + "options": { + "buildTarget": "my-node-app:build", + "runBuildTargetDependencies": false, + }, + }, + }, + } + `); }); it('should generate files', async () => { await applicationGenerator(tree, { directory: appDirectory, + addPlugin: true, }); expect(tree.exists(`${appDirectory}/src/main.ts`)).toBeTruthy(); @@ -45,6 +94,7 @@ describe('application generator', () => { it('should configure tsconfig correctly', async () => { await applicationGenerator(tree, { directory: appDirectory, + addPlugin: true, }); const tsConfig = devkit.readJson(tree, `${appDirectory}/tsconfig.app.json`); @@ -61,6 +111,7 @@ describe('application generator', () => { await applicationGenerator(tree, { directory: appDirectory, strict: true, + addPlugin: true, }); const tsConfig = devkit.readJson(tree, `${appDirectory}/tsconfig.app.json`); @@ -79,6 +130,7 @@ describe('application generator', () => { await applicationGenerator(tree, { directory: appDirectory, + addPlugin: true, }); expect(devkit.formatFiles).toHaveBeenCalled(); @@ -90,6 +142,7 @@ describe('application generator', () => { await applicationGenerator(tree, { directory: appDirectory, skipFormat: true, + addPlugin: true, }); expect(devkit.formatFiles).not.toHaveBeenCalled(); @@ -101,6 +154,7 @@ describe('application generator', () => { await applicationGenerator(tree, { directory: appDirectory, e2eTestRunner: 'none', + addPlugin: true, }); const projectConfigurations = devkit.getProjects(tree); diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index b02534a0e4ca2..58b808302c2e4 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -159,6 +159,21 @@ function getServeConfig(options: NormalizedSchema): TargetConfiguration { }; } +function getNestWebpackBuildConfig(): TargetConfiguration { + return { + executor: 'nx:run-commands', + options: { + command: 'webpack-cli build', + args: ['node-env=production'], + }, + configurations: { + development: { + args: ['node-env=development'], + }, + }, + }; +} + function addProject(tree: Tree, options: NormalizedSchema) { const project: ProjectConfiguration = { root: options.appProjectRoot, @@ -175,6 +190,10 @@ function addProject(tree: Tree, options: NormalizedSchema) { if (!hasWebpackPlugin(tree)) { addBuildTargetDefaults(tree, `@nx/webpack:webpack`); project.targets.build = getWebpackBuildConfig(project, options); + } else if (options.isNest) { + // If we are using Nest that has the webpack plugin we need to override the + // build target so that node-env can be set to production or development so the serve target can be run in development mode + project.targets.build = getNestWebpackBuildConfig(); } } project.targets.serve = getServeConfig(options);