UNPKG

3.44 kBMarkdownView Raw
1# Angular Compiler Webpack Plugin
2
3Webpack 5.x plugin for the Angular Ahead-of-Time compiler. The plugin also supports Angular JIT mode.
4When this plugin is used outside of the Angular CLI, the Ivy linker will also be needed to support
5the usage of Angular libraries. An example configuration of the Babel-based Ivy linker is provided
6in the linker section. For additional information regarding the linker, please see: https://v13.angular.io/guide/creating-libraries#consuming-partial-ivy-code-outside-the-angular-cli
7
8## Usage
9
10In your webpack config, add the following plugin and loader.
11
12```typescript
13import { AngularWebpackPlugin } from '@ngtools/webpack';
14
15exports = {
16 /* ... */
17 module: {
18 rules: [
19 /* ... */
20 {
21 test: /\.[jt]sx?$/,
22 loader: '@ngtools/webpack',
23 },
24 ],
25 },
26
27 plugins: [
28 new AngularWebpackPlugin({
29 tsconfig: 'path/to/tsconfig.json',
30 // ... other options as needed
31 }),
32 ],
33};
34```
35
36The loader works with webpack plugin to compile the application's TypeScript. It is important to include both, and to not include any other TypeScript loader.
37
38## Options
39
40- `tsconfig` [default: `tsconfig.json`] - The path to the application's TypeScript Configuration file. In the `tsconfig.json`, you can pass options to the Angular Compiler with `angularCompilerOptions`. Relative paths will be resolved from the Webpack compilation's context.
41- `compilerOptions` [default: none] - Overrides options in the application's TypeScript Configuration file (`tsconfig.json`).
42- `jitMode` [default: `false`] - Enables JIT compilation and do not refactor the code to bootstrap. This replaces `templateUrl: "string"` with `template: require("string")` (and similar for styles) to allow for webpack to properly link the resources.
43- `directTemplateLoading` [default: `true`] - Causes the plugin to load component templates (HTML) directly from the filesystem. This is more efficient if only using the `raw-loader` to load component templates. Do not enable this option if additional loaders are configured for component templates.
44- `fileReplacements` [default: none] - Allows replacing TypeScript files with other TypeScript files in the build. This option acts on fully resolved file paths.
45- `inlineStyleFileExtension` [default: none] - When set inline component styles will be processed by Webpack as files with the provided extension.
46
47## Ivy Linker
48
49The Ivy linker can be setup by using the Webpack `babel-loader` package.
50If not already installed, add the `babel-loader` package using your project's package manager.
51Then in your webpack config, add the `babel-loader` with the following configuration.
52If the `babel-loader` is already present in your configuration, the linker plugin can be added to
53the existing loader configuration as well.
54Enabling caching for the `babel-loader` is recommended to avoid reprocessing libraries on
55every build.
56For additional information regarding the `babel-loader` package, please see: https://github.com/babel/babel-loader/tree/main#readme
57
58```typescript
59import linkerPlugin from '@angular/compiler-cli/linker/babel';
60
61exports = {
62 /* ... */
63 module: {
64 rules: [
65 /* ... */
66 {
67 test: /\.[cm]?js$/,
68 use: {
69 loader: 'babel-loader',
70 options: {
71 cacheDirectory: true,
72 compact: false,
73 plugins: [linkerPlugin],
74 },
75 },
76 },
77 ],
78 },
79};
80```