Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nest): update project config to enable artifacts to be built as dev #29110

Merged
merged 1 commit into from
Nov 28, 2024
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
56 changes: 55 additions & 1 deletion packages/nest/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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`);
Expand All @@ -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`);

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

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

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

expect(devkit.formatFiles).not.toHaveBeenCalled();
Expand All @@ -101,6 +154,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