nx/packages/angular/docs/dev-server-examples.md
Leosvel Pérez Espinosa 0e56533d01
docs(angular): call out @nx/angular:dev-server is required when using @nx/angular build executors (#22883)
- Calls out the `@nx/angular:dev-server` executor as required when using
`@nx/angular` build executors
- Improves & aligns executor descriptions and examples

Main docs updated with the callouts:


https://nx-dev-git-fork-leosvelperez-docs-angular-executors-nrwl.vercel.app/nx-api/angular/executors/application

https://nx-dev-git-fork-leosvelperez-docs-angular-executors-nrwl.vercel.app/nx-api/angular/executors/browser-esbuild

https://nx-dev-git-fork-leosvelperez-docs-angular-executors-nrwl.vercel.app/nx-api/angular/executors/webpack-browser

https://nx-dev-git-fork-leosvelperez-docs-angular-executors-nrwl.vercel.app/nx-api/angular/executors/dev-server

The rest of the Angular executors' descriptions were also updated.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-05-07 11:34:22 +02:00

2.7 KiB

This executor is a drop-in replacement for the @angular-devkit/build-angular:dev-server builder provided by the Angular CLI. In addition to the features provided by the Angular CLI builder, the @nx/angular:dev-server executor also supports the following:

  • Serving applications with Vite when using the @nx/angular:application or @nx/angular:browser-esbuild executors to build them
  • Serving applications with webpack when using the @nx/angular:webpack-browser executor
  • Providing HTTP request middleware functions when the build target is using an esbuild-based executor
  • Incremental builds

Examples

{% tabs %} {% tab label="Using a custom webpack configuration" %}

This executor should be used along with @nx/angular:webpack-browser to serve an application using a custom webpack configuration.

Add the serve target using the @nx/angular:dev-server executor, set the build target executor as @nx/angular:webpack-browser and set the customWebpackConfig option as shown below:

"build": {
  "executor": "@nx/angular:webpack-browser",
  "options": {
    ...
    "customWebpackConfig": {
      "path": "apps/my-app/webpack.config.js"
    }
  }
},
"serve": {
  "executor": "@nx/angular:dev-server",
  "configurations": {
    "production": {
      "buildTarget": "my-app:build:production"
    },
    "development": {
      "buildTarget": "my-app:build:development"
    }
  },
  "defaultConfiguration": "development",
}
module.exports = (config) => {
  // update the config with your custom configuration

  return config;
};

{% /tab %}

{% tab label="Providing HTTP request middleware function" %}

{% callout type="warning" title="Overrides" }

Available for workspaces using Angular version 17.0.0 or greater and with build targets using an esbuild-based executor.

{% /callout %}

The executor accepts an esbuildMidleware option that allows you to provide HTTP require middleware functions that will be used by the Vite development server.

{
  ...
  "targets": {
    "serve": {
      "executor": "@nx/angular:dev-server",
      "options": {
        ...
        "esbuildMidleware": ["apps/my-app/hello-world.middleware.ts"]
      }
    }
    ...
  }
}
import type { IncomingMessage, ServerResponse } from 'node:http';

const helloWorldMiddleware = (
  req: IncomingMessage,
  res: ServerResponse,
  next: (err?: unknown) => void
) => {
  if (req.url === '/hello-world') {
    res.end('<h1>Hello World!</h1>');
  } else {
    next();
  }
};

export default helloWorldMiddleware;

{% /tab %}