Having difficulties generating ngc and tsc AOT ES5 compatible code

I've explored various options before seeking help here. I have an angular2 library that has been AOT compiled using ngc. Currently, I am not using webpack and solely relying on plain npm scripts. Below is the tsconfig file being utilized:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "lib": ["dom","es2015"],
    "outDir": "tsc-out"
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules/*",
    "aot/",
    "app/boot-aot.ts"
  ],
  "angularCompilerOptions": {
    "genDir": "aot/",
    "strictMetadataEmit" : true
  }
}

When running ngc, it generates a folder named "aot" which includes a "node_modules" subfolder as illustrated below.

Contents within the aot folder:

app
node_modules
 @angular
 @ngbootstrap 
 rxjs

As of now, all factories appear to be in order but they are saved as ts files. My plan is to run tsc on this output so I can utilize these factories in a native ES5 environment. Therefore, I attempted to execute tsc on this directory utilizing a different tsconfig file.

However, this method doesn't reproduce the node_modules folder. Consequently, some of the generated factory files contain errors as they still reference the absent node_modules folder. For instance, the app.module.ngfactory.js (located inside app folder) contains references like:

var import59 = require("../node_modules/@ng-bootstrap/ng-
bootstrap/tooltip/tooltip.ngfactory");
var import60 = require("../node_modules/@ng-bootstrap/ng-
bootstrap/typeahead/typeahead-window.ngfactory");

Am I overlooking something crucial here? Despite trying out numerous variations, it seems I'm lacking certain details. Is there a way to expose these aot factories to ES5 given the current behavior of ngc and tsc?

Answer №1

A strategy to expose AOT factories to ES5 is achievable with the current behavior of ngc.

There is no need to execute tsc after ngc because ngc handles it for you. It is advisable to inspect the final outcomes in your outDir (out-tsc) rather than genDir (aot). In cases where Angular 5 is utilized, the aot directory may not even be visible.

It is also suggested to configure the "module" parameter as "es2015" instead of "commonjs" and then utilize rollup for tree-shaking and the eventual es5 bundling process.

Exclusions like node_modules/aot are not necessary as they should be compiled along with the final bundle.

To view a comprehensive example using ngc and rollup, visit:

https://github.com/fintechneo/angular2-templates

The repository has been upgraded to Angular 5, but examining earlier versions can guide you back to the configuration setup for Angular 2.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular iframe is not displaying content

My component includes an iframe as shown below: <iframe [innerHTML]="html"></iframe> When I use: <div [innerHTML]="html"></div> it works perfectly. However, it must be contained within an iframe to apply custom styles and script ...

Angular - Strategies for Handling Observables within a Component

I am new to Angular and recently started learning how to manage state centrally using ngRx. However, I am facing a challenge as I have never used an Observable before. In my code, I have a cart that holds an array of objects. My goal is to iterate over the ...

What is the process for importing types from the `material-ui` library?

I am currently developing a react application using material-ui with typescript. I'm on the lookout for all the type definitions for the material component. Despite attempting to install @types/material-ui, I haven't had much success. Take a look ...

Replace the ngOnDestroy method

Is there a way to complete an observable when the ngOnDestroy is triggered? I'd prefer not to create new child components when dealing with just one component instance. I attempted to override ngOnDestroy by modifying the function in the component&apo ...

Utilizing Angular 2 for Displaying SVG Information

I am facing an issue with my angular application where I need to display product information in a SVG image obtained from a service in JSON format. Despite trying different methods, I have been unsuccessful in rendering the image on the HTML template. Att ...

How to Determine the Requirement Status of Input Variables in an Angular 2 Directive?

Is it possible to specify an input variable in a directive as required or optionally as non-required? In the example below, we have set a default value of false. However, if I fail to declare it in the parent component template, ng2 AoT throws an error: ...

Difficulty reloading after updating input using AngularJS ngTable

I'm encountering an issue where I need to utilize ngTable with TableParams to load and modify a table. For instance, when the page initializes, entering a number in the textbox for the first time displays the appropriate number of rows in the table. ...

The observer error silently assumes an undefined type

Currently, I am attempting to implement the guidance provided in this Stack Overflow post on performing a File Upload using AngularJS 2 and ASP.net MVC Web API. The issue arises from the upload.service.ts file where an error is identified next to the prob ...

Tips on sending component values to Host Listener in Custom Directives using Angular 2

I am looking to transmit model values from my HTML template to a custom directive: @Directive({ selector: '[eventlistener]' }) export class EventListener { @Input() value:string = 'Not Defined'; @HostListener('click& ...

Troubleshooting issue: Unable to Subscribe to Subject in Angular 5+ with RxJS

My service has an observable that is subscribed to, and the payload is passed to a subject in the service. However, when I subscribe to the subject in my component, nothing happens. I expect to see the output from console.log in the .subscribe method withi ...

Ensuring Map Safety in Typescript

Imagine having a Map structure like the one found in file CategoryMap.ts export default new Map<number, SubCategory[]>([ [11, [100, 101]], [12, [102, 103]], ... ]) Is there a way to create a type guard for this Map? import categoryMap fro ...

Mismatched non-intersecting categories with TypeScript

I have an object that I need to conditionally render some JSX based on certain properties. I want to restrict access to specific parts of the object until certain conditions are met. Here is my scenario: const { alpha, bravo } = myObject; if (alpha.loadin ...

Contrast between employing typeof for a type parameter in a generic function and not using it

Can you explain the difference between using InstanceType<typeof UserManager> and InstanceType<UserManager> I'm trying to understand TypeScript better. I noticed in TS' typeof documentation that SomeGeneric<typeof UserManager> ...

Angular 2 Quickstart encountered a 404 error when trying to retrieve the /app/main.js

I'm attempting to follow the Angular 2 quickstart guide, but I'm having trouble getting it to work. I've searched for similar questions but haven't found a solution yet. Can anyone assist me with this? Here is my code: app.component.t ...

Issue in kendo-angular-upload: Failure to trigger error event following a 500 Server Error Code

When developing the Front End, I utilize Angular 7 to attempt uploading an image using Kendo: <kendo-upload [saveUrl]="uploadSaveUrl" [removeUrl]="uploadRemoveUrl" [restrictions]="uploadRestrictions" [mul ...

The Material UI Grid is not compatible with the Chrome DevTools Device Toolbar and may not function properly

Using MUI v5 with React and Typescript has been an interesting experience for me. When I utilize the Grid system, I set options like xs sm md lg to define item width. Interestingly, setting just xs or sm works fine, but when I include md, other options su ...

Build a new datatable within an Angular component

Currently, I am working on an Angular 2 application where I have a module named Home with a component of the same name. HomeModule.ts: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ...

Execute the form validation and submission simultaneously

I am facing an issue with my form validation and submission process. When I click on the creer button, the form validation works fine but the submit function does not work. I don't see any errors in my console. I have tried using the onsubmit() method ...

What is the reason for encountering a TypeScript error when using a union type?

interface Bird { age:string, eat:()=>any } interface Fish { age:string, swim:()=>any } type Pet = Fish | Bird; everything looks good so far, defining a Pet type const pet:Pet={ age:"sd", eat:()=>{} } when trying to return ...

Trouble communicating between Angular HttpClient POST request and Spring Controller

I'm encountering difficulties while trying to perform a POST request from my Angular frontend service class to the backend Spring controller. Despite my efforts, I am unable to see the log.info message from the controller unless I manually trigger th ...