137 Commits

Author SHA1 Message Date
Charles Lyding
8f47f1e965 feat(@angular-devkit/build-angular): provide default and abbreviated build target support for dev-server and extract-i18n
The `buildTarget` options for both the `dev-server` and `extract-i8n` builders
now have default values that reflect the recommended and new project generated
values. The defaults are as follows where `<current-project>` is the name of the project
where the `dev-server` or `extract-i18n` builder target is located:
* `dev-server` --> `<current-project>:build:development`
* `extract-i18n` --> `<current-project>:build`

Additionally, abbreviated target specifiers are now supported for these
options. This allows target specifiers such as `::production` which would expand
to `<current-project>:build:production` for either builder.
Abbreviated target specifiers are only supported for the `buildTarget` option in
the `dev-server` and `extract-i18n` builders.
2024-01-12 10:58:29 -08:00
Charles Lyding
18a11f503c refactor(@angular-devkit/build-angular): add experimental builder selector extension for dev-server
When using the experimental programmatic API for the development server, the choice of builder used
to execute the actual underlying build can now be chosen via a selector function extension option.
The returned string value must be one of the first-party esbuild-based builders for the Vite-based
development server to be used. All other returned values will cause the Webpack-based development
server to be used. The Vite-based development server currently requires one of those builders to
be used (either `@angular-devkit/build-angular:application` or `@angular-devkit/build-angular:browser-esbuild`).
2024-01-09 09:41:40 -08:00
Angular Robot
a6f3987341 build: update all non-major dependencies 2024-01-03 16:15:34 +01:00
Charles Lyding
c257e6e60e refactor(@angular-devkit/build-angular): support cache option with JavaScript transformer
The previously unused `reuseResults` option for the JavaScript transformer used by the
`application` builder has been removed and replaced with an optional cache option. This
option is currently unused by will allow the caching of JavaScript transformations including
the Angular linker.
2023-12-29 13:04:56 -05:00
Charles Lyding
9d7d136b32 refactor(@angular-devkit/build-angular): pass full extensions to application builder for dev server
Experimental programmatically added build extensions are now passed to the internal application builder
when used with the development server. Previously, the plugins were not passed in a manner that would
allow them to be used if the `application` builder was selected as the builder for dev server usage.
2023-12-19 10:50:07 -05:00
Alan Agius
a5d4735b80 fix(@angular-devkit/build-angular): construct SSR request URL using server resolvedUrls
With vite `header.host` is undefined when SSL is enabled. This resulted in an invalid URL to be constructed.

Closes #26652
2023-12-13 20:14:37 +01:00
Marco Steinhoff
e9a51a37f6 refactor(@angular-devkit/build-angular): unify proxy config loading in dev-server and ssr-dev-server
This improvement harmonizes the proxy configuration loading mechanisms between the dev-server and ssr-dev-server. Previously, these servers used different methods for loading the proxy configuration, leading to inconsistencies. Notably, the ssr-dev-server was limited to loading configurations only in JSON format. This change enables the ssr-dev-server to support various configuration formats, aligning it with the dev-server's capabilities and improving overall developer experience.
2023-12-13 17:43:53 +01:00
Alan Agius
72bd0ab9d0 fix(@angular-devkit/build-angular): allow vite to serve JavaScript and TypeScript assets
This commit fixes an issue which caused vite to transform JavaScript and TypeScript assets.

Closes #26641
2023-12-12 17:43:29 +01:00
Alan Agius
4e1f0e44dc fix(@angular-devkit/build-angular): the request url "..." is outside of Vite serving allow list for all assets
Vite's fs.allow was being populated incorrectly.

Closes #26624
2023-12-11 15:46:07 +01:00
Alan Agius
4b3a965429 fix(@angular-devkit/build-angular): baseHref with trailing slash causes server not to be accessible without trailing slash
This commit fixes an issue were when using a `baseHref` with trailing slash, vite dev-server would have been only accessible via a URL with a trailing slash. As vite would redirect to an error page similar to the below;

```
The server is configured with a public base URL of /myapp/ - did you mean to visit [/myapp/](http://localhost:4200/myapp/) instead?
```

Closes: #26618
2023-12-08 10:25:23 -05:00
Charles Lyding
d07ef2f0b9 fix(@angular-devkit/build-angular): ensure browser-esbuild is used in dev server with browser builder and forceEsbuild
To ensure that the `forceEsbuild` development server option chooses the correct underlying build implementation when
the project contains the `browser` builder within the build target, an explicit check and conversion of the builder
name is now performed during the initialization phase of the development server builder.
2023-12-08 09:57:38 -05:00
Charles Lyding
5b8e2d5e57 fix(@angular-devkit/build-angular): ensure port 0 uses random port with Vite development server
Vite appears to consider a port value of `0` as a falsy value and use the default Vite port of
`5173` when zero is used as a value for the development server port option. To workaround this
issue, the port checker code now explicitly handles the zero value case and determines a random
port as would be done automatically by the Webpack-based development server.
2023-12-08 09:57:22 -05:00
Alan Agius
aca0be06e8 test(@angular-devkit/build-angular): enable vite service worker unit tests
Enable service worker tests for vite.
2023-12-04 09:12:34 +01:00
Charles Lyding
9906ab7b47 fix(@angular-devkit/build-angular): normalize asset source locations in Vite-based development server
The Vite-based development server uses an allow list to permit access to configured assets. This list
is checked internally to Vite by using its normalized path form. To ensure that assets provided by the
build are checked correctly on all platforms, the asset list is now normalized with Vite's path
normalization prior to being used.
2023-12-04 09:12:06 +01:00
Alan Agius
a9fa561027 test(@angular-devkit/build-angular): enable dev-server watch tests for vite
These tests can now be enabled.
2023-12-01 19:39:27 +01:00
Charles Lyding
ccba849e48 feat(@angular-devkit/build-angular): support keyboard command shortcuts in application dev server
When using the `application` or `browser-esbuild` builders with the `dev-server`, interactive key
shortcuts are now available to use while the server is active. Once the build has successfully completed
and the development server has initialized, typing a single letter key followed by enter will execute
the associated action. These actions include forcing a browser full reload, clearing the console, or
quitting. More actions may be added in the future.

The following actions are currently available:
```
  press r + enter to force reload browser
  press u + enter to show server url
  press o + enter to open in browser
  press c + enter to clear console
  press q + enter to quit
```

The `h` action is also available which will display the above list at runtime.
2023-11-29 13:55:33 -05:00
Alan Agius
150a2e7754 fix(@angular-devkit/build-angular): serve assets from the provided serve-path
When the `serve-path` option is used assets are now correctly servered from this location.

Closes #26509
2023-11-28 16:56:05 +01:00
Charles Lyding
154c7ce8f0 refactor(@angular-devkit/build-angular): adjust wording of dev server client status messages
To minimize ambiguity, the update related log messages for the development server now consistently
indicate that the page reload, HMR update, or otherwise has actually been sent to any connected clients.
2023-11-23 14:24:08 +01:00
Charles Lyding
6473b01603 fix(@angular-devkit/build-angular): ensure all configured assets can be served by dev server
The Vite-based development server restricts filesystem access by default. However, assets
configured by the build option `assets` are intended to be accessible by the application at
runtime. To ensure that these files are accessible, the allow list will now explicitly include
all configured assets found during the build.
2023-11-22 10:31:58 -05:00
Charles Lyding
a21046f606 refactor(@angular-devkit/build-angular): fully disable Vite internal file watching
With the Vite-based development server, the build pipeline itself watches for files
and rebuilds the relevant parts of the application as needed. The Vite file watching
is unneeded and was previously setup to ignore all files. This worked but was not
ideal as chokidar was still initialized inside Vite. However, Vite now supports fully
disabling the file watching by passing `null` for the Vite watch option value.
2023-11-20 09:29:38 -05:00
Alan Agius
944e964c59 fix(@angular-devkit/build-angular): only include vendor sourcemaps when using the dev-server when the option is enabled
Prior to this change the vendor sourcemaps option was not being considered when in the vite JavaScript transformation pipeline.
2023-11-20 14:25:10 +01:00
Charles Lyding
43b09ce55a refactor(@angular-devkit/build-angular): use application builder for unknown builders in Vite dev server
The application builder logic will now be used when the Vite-based development server is used with a
custom builder. This aligns with the programmatic export of application builder. This allows consistent
behavior for custom builders that use both the application builder and dev server builder APIs.
2023-11-20 08:04:22 -05:00
Charles Lyding
3b93df42da feat(@angular-devkit/build-angular): allow configuring loaders for custom file extensions in application builder
When using the `application` builder, a new `loader` option is now available for use.
The option allows a project to define the type of loader to use with a specified file extension.
A file with the defined extension can then used within the application code via an import statement
or dynamic import expression, for instance.
The available loaders that can be used are:
* `text` - inlines the content as a string
* `binary` - inlines the content as a Uint8Array
* `file` - emits the file and provides the runtime location of the file
* `empty` - considers the content to be empty and will not include it in bundles

The loader option is an object-based option with the keys used to define the file extension and the values used
to define the loader type.

An example to inline the content of SVG files into the bundled application would be as follows:
```
loader: {
    ".svg": "text"
}
```
An SVG file can then be imported:
```
import contents from './some-file.svg';
```
Additionally, TypeScript needs to be aware of the module type for the import to prevent type-checking
errors during the build. This can be accomplished with an additional type definition file within the
application source code (`src/types.d.ts`, for example) with the following or similar content:
```
declare module "*.svg" {
  const content: string;
  export default content;
}
```
The default project configuration is already setup to use any type definition files present in the project
source directories. If the TypeScript configuration for the project has been altered, the tsconfig may
need to be adjusted to reference this newly added type definition file.
2023-11-16 12:11:33 -05:00
Alan Agius
40c16760c4 fix(@angular-devkit/build-angular): normalize paths in ssr sourcemaps to posix when using vite
Path normalization that fixes a tests on windows
2023-11-16 17:08:51 +01:00
Alan Agius
117628f796 build: update dependency vite to v5
(cherry picked from commit 5b93b9d1dddd34b2b2d7b6ec9e84be8dfc0e52c6)
2023-11-16 15:27:41 +01:00
Alan Agius
44f2c4ebb4 fix(@angular-devkit/build-angular): do not process ssr entry-point when running ng serve
The ssr entry-point is not used by vite.
2023-11-15 18:57:28 +01:00
Alan Agius
9926123912 fix(@angular-devkit/build-angular): add support for vendor sourcemaps when using the dev-server
Prior to this change the vendor sourcemaps were never generated when using vite prebundling.
2023-11-15 18:11:33 +01:00
Alan Agius
286e5d32f2 fix(@angular-devkit/build-angular): rewire sourcemap back to original source root
Prior to this change, when a error occurs when using the SSR dev-server the stacktraces pointed back to the virtual root path.
2023-11-13 14:59:50 -05:00
Alan Agius
6d89c18807 test(@angular-devkit/build-angular): expand unit tests dev-server suite to include vite
This commit updates the dev-server unit test suit to run tests using vite
2023-11-08 16:46:14 +01:00
Alan Agius
c0921d61d4 refactor(@angular-devkit/build-angular): move teardown logic registration before
In some cases, this was regestered too late which caused the server never to terminate.
2023-11-08 16:46:14 +01:00
Alan Agius
07a8ffd5af fix(@angular-devkit/build-angular): keep dependencies pre-bundling validate between builds
Prior to this commit, the pre-bundling of dependencies was being invalidated for each build. This is because vite uses parts of the config among other things to generate a hash. In our case the config was not always consistent for the same project between different builds.

The main 2 problems areas were:
- Random `root` path
- `include` and `exclude` arrays were not always in the same order.
2023-11-06 16:35:53 +01:00
Alan Agius
7cb5d35a26 fix(@angular-devkit/build-angular): disable dependency optimization for SSR
It appears that Vite currently, has a number of limitation/bugs when using `optimizeDeps` for SSR bundles.

Currently this causes a number of issues:
  - Deps are re-optimized everytime the server is started.
  - Added deps after a rebuild are not optimized.
  - Breaks RxJs (Unless it is added as external). See: #26235

We should follow up with Vite and try to get this solved as this would be a nice feature to use.

Closes #26235 and #26234
2023-11-06 15:20:13 +01:00
Alan Agius
ef2437fe95 refactor(@angular-devkit/build-angular): clean externalMetadata arrays on every rebuild
Currently, externalMetadata arrays get appended the same items over and over again on every rebuild. This commit cleans the array before it appending the new values.
2023-11-06 14:07:56 +01:00
Charles Lyding
988dce6eff refactor(@angular-devkit/build-angular): allow JS transformer result reuse for application dev-server builder
The `JavaScriptTransformer` class that is responsible for Angular linking and several build optimization
transformations can now be configured to track and reuse previous and pending transformation requests.
This allows for cases where multiple consumers of the class will not cause repeat transformation actions.
Pending results will be stored if the constructor option `reuseResults` is enabled. If two transformation
requests are issued for the same file, the same underlying result will be provided to each. This behavior
currently only applies to file transformation requests.
2023-11-06 11:53:39 +01:00
Alan Agius
17399244b3 perf(@angular-devkit/build-angular): start SSR dependencies optimization before the first request
Vite will only start dependency optimization of SSR modules when the first request comes in.

In some cases, this causes a long waiting time. To mitigate this, we call `ssrLoadModule` to initiate this process before the first request.
2023-11-05 18:38:03 +01:00
Alan Agius
64fdb31c78 fix(@angular-devkit/build-angular): exclude node.js built-ins from vite dependency optimization
This fixes the following warnings
```
Cannot optimize dependency: url, present in 'ssr.optimizeDeps.include'
Cannot optimize dependency: path, present in 'ssr.optimizeDeps.include'
```
2023-11-05 13:31:05 +01:00
Alan Agius
8c76cb2830 fix(@angular-devkit/build-angular): media files download files in vite
Unlike assets, output file were not prefixed with a `/`. This change alings these and now files are always prefixed with a slash.

Closes #26215
2023-11-03 16:47:01 +01:00
Alan Agius
59908753b6 perf(@angular-devkit/build-angular): optimize server or browser only dependencies once
This commit splits the retrieval of external dependencies into two. Server imports and browser imports. This is so that we avoid vite from optimizing server or browser only dependencies twice.

This also fixes an issue were in some cases Vite would issue a warning like `Cannot optimize dependency: path, present in 'optimizeDeps.include'`. This was caused because of server only dependencies ended up trying to be optimized for a browser build.
2023-11-03 16:06:32 +01:00
Alan Agius
5547095693 refactor: import path methods individually
Minor style change
2023-11-03 15:47:12 +01:00
Alan Agius
98a61ff932 fix(@angular-devkit/build-angular): in vite skip SSR middleware for path with extensions
Do not try to SSR a request that has extensions.

Closes #26199
2023-11-03 15:47:12 +01:00
Charles Lyding
bcd3a86db8 refactor(@angular-devkit/build-angular): add experimental extension points to Vite-based dev server
When using the experimental programmatic API for the development server with an esbuild-based
builder (`application`/`browser-esbuild`), express compatible middleware can now be added.
Also, the index HTML transformer that previously only worked with the Webpack-based development
server is also now enabled.
However, usage of these options may result in unexpected application output and/or build failures.
They are also not officially supported and SemVer guarantees are not present.
Stable and supported methods for build process extension are being evaluated for a future release.
2023-11-03 13:51:09 +01:00
Alan Agius
4c1265a9bf fix(@angular-devkit/build-angular): prebundle dependencies for SSR when using Vite
The commit introduces dependencies prebundling and optimisation for SSR dependencies. This is primarily needed for Angular linking and async/await downlevelling. To enable this, we need to use the undocumented `optimizeDeps` setting under the `ssr` option. This is because, the top level `optimizeDeps` vite config option  only controls browser dependencies.

For the above mentioned option to take effect and transform node packages, we also need to use `noExternal` and use a catch all `RegExp`. Note: setting this option to `true` has a different effect from a catch all `RegExp`, as the former will cause the `external` option to be ignored.

Additionally together with `externalMetadata.explicit` we add Node.js build-ins as `external`.

Closes: #26192
2023-11-02 16:03:34 +01:00
Charles Lyding
5b4f50c7ca fix(@angular-devkit/build-angular): provide server baseUrl result property in Vite-based dev server
The protractor E2E builder relies on a development server `baseUrl` result property to determine the address
to connect when testing the application. The Vite-based development server now also provides this
property. While the protractor builder is considered deprecated, it is still used in CLI E2E tests.
2023-11-01 14:48:47 +01:00
Charles Lyding
e588e84160 test: remove node-fetch package usage
Native fetch support is now available in Node.js and the `node-fetch` package can now be removed.
2023-10-31 08:57:42 +01:00
Charles Lyding
2f1b650554 fix(@angular-devkit/build-angular): remove unactionable error overlay suggestion from Vite-based dev server
The Vite-based development server that is used with the esbuild-based builders (`application`/`browser-esbuild`)
will show an error overlay when the application build encounters an error. This overlay previously provided a
suggestion to edit the `vite.config.js` configuration file to disable the error overlay. Since Vite usage is
encapsulated within the Angular CLI, this suggestion is unactionable and may lead to user confusion due to
no Vite configuration file being present within the project nor would creating one have an effect on the build
process.
2023-10-30 11:27:45 +01:00
Charles Lyding
09682e5f2c fix(@angular-devkit/build-angular): ensure correct web worker URL resolution in vite dev server
When using the application builder with the development server, Web Worker URLs previously may
have been incorrectly resolved. This caused Vite to consider the Web Worker URLs as outside
the project root and generate a special file system URL. While this worked on Mac/Linux, it
would fail on Windows. Since Vite does not appear to support resolve plugins for Web Workers,
the virtual project root for the in-memory build has now been adjusted to allow the referencing
file to have a path that resolves the Web Worker URL to a project relative location.
2023-10-30 09:01:55 +01:00
Alan Agius
8178c23c88 fix(@angular-devkit/build-angular): handle HTTP requests to assets during SSG in dev-server
This commit fixes an issue were during SSG/SSR in the dev-server http requests to assets causes the page rendering to fail.
2023-10-26 16:01:39 +02:00
cexbrayat
62a7019a47 fix(@angular-devkit/build-angular): ensure that externalMetadata is defined
`ng serve` might fail with: `Cannot read properties of undefined (reading 'explicit')` because `result.externalMetadata` is optional.
This commit guards the access and should fix the issue related to the fix 9768c184e0
2023-10-26 14:59:04 +02:00
Charles Lyding
142a36a99f refactor(@angular-devkit/build-angular): prevent vite from resolving explicit external dependencies
When using the Vite-based development server combined with the `externalDependencies` option, Vite
will no longer attempt to resolve the explicitly marked externals.
This is still not ideal since it vite will still transform the import specifier to `/@id/${source}`
but is currently closer to a raw external than a resolved file path. Further investigation with
a possible feature request for Vite may be needed to achieve the desired outcome of an unresolved
and untransformed external import specifier.
2023-10-26 14:58:53 +02:00
Charles Lyding
9768c184e0 fix(@angular-devkit/build-angular): automatically include known packages in vite prebundling
When using the Vite-based development server, the application build step already contains the
list of known packages that would need to be prebundled. This information can be passed to Vite
directly to avoid Vite needing to perform discovery on every output file that will be requested.
This also avoids the Vite server behavior where Vite forces a reload of the page when it discovers
a new dependency. This behavior can result in lost state during lazy loading of a route.
2023-10-25 12:52:46 +02:00