Skip to content

Commit

Permalink
fix(nest): update project config to enable artifacts to be built as dev
Browse files Browse the repository at this point in the history
closed #26761
  • Loading branch information
ndcunningham committed Nov 28, 2024
1 parent dc67660 commit ae75272
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/nest/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,67 @@ 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();
Expand All @@ -45,6 +95,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`);
Expand All @@ -61,6 +112,7 @@ describe('application generator', () => {
await applicationGenerator(tree, {
directory: appDirectory,
strict: true,
addPlugin: true,
});
const tsConfig = devkit.readJson(tree, `${appDirectory}/tsconfig.app.json`);

Expand All @@ -79,6 +131,7 @@ describe('application generator', () => {

await applicationGenerator(tree, {
directory: appDirectory,
addPlugin: true,
});

expect(devkit.formatFiles).toHaveBeenCalled();
Expand All @@ -90,6 +143,7 @@ describe('application generator', () => {
await applicationGenerator(tree, {
directory: appDirectory,
skipFormat: true,
addPlugin: true,
});

expect(devkit.formatFiles).not.toHaveBeenCalled();
Expand All @@ -101,6 +155,7 @@ describe('application generator', () => {
await applicationGenerator(tree, {
directory: appDirectory,
e2eTestRunner: 'none',
addPlugin: true,
});

const projectConfigurations = devkit.getProjects(tree);
Expand Down
19 changes: 19 additions & 0 deletions packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit ae75272

Please sign in to comment.