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

What could be the reason behind my Ionic app receiving a 401 error from an API call while Postman is not encountering the

Following the instructions from a tutorial on Smart of the Home, I implemented the code below in a provider for my Ionic 2 App. return new Promise(resolve => { // Using Angular HTTP provider to make a request and process the response let headers = ...

Adjust the color of an SVG icon depending on its 'liked' status

In my React/TypeScript app, I have implemented an Upvote component that allows users to upvote a post or remove their upvote. The icon used for the upvote is sourced from the Grommet-Icons section of the react-icons package. When a user clicks on the icon ...

Is there a way to customize the language used for the months and days on the DatePicker

Is there a way to change the language of the DatePicker (months and days) in Material UI? I have attempted to implement localization through both the theme and LocalizationProvider, but neither method seems to work. Here are some resources I have looked a ...

Using the RxJS iif operator for implementing multiple condition statements

What is the optimal approach for returning Observables based on multiple conditions? This is my current code implementation: iif( () => !this.id, this.service.listStuff$(), this.service.listStuffById$(this.id) ).pipe( switchMap((list: L ...

React Native is throwing an error message saying that the Component cannot be used as a JSX component. It mentions that the Type '{}' is not assignable to type 'ReactNode'

Recently, I initiated a new project and encountered errors while working with various packages such as React Native Reanimated and React Navigation Stack. Below is my package.json configuration: { "name": "foodmatch", "version ...

Creating a JSX syntax for a simulated component and ensuring it is fully typed with TypeScript

Looking for some innovative ideas on how to approach this challenge. I have a test helper utils with added types: import { jest } from '@jest/globals' import React from 'react' // https://learn.reactnativeschool.com/courses/781007/lect ...

Cleaning up HTML strings in Angular may strip off attribute formatting

I've been experimenting and creating a function to dynamically generate form fields. Initially, the Angular sanitizer was removing <input> tags, so I discovered a way to work around this by bypassing the sanitation process for the HTML code stri ...

Retrieve the value from an HTML class using Angular

The Person Object has a field called schoolId, but the School object (not shown here) contains the schoolName. I want to display the schoolName in the table data cell instead of the schoolId from the Person Object. How can I achieve this? <tr *ngFor=& ...

Angular 2 feature that allows for a single list value to be toggled with the

Currently, my form is connected to a C# API that displays a list of entries. I am trying to implement a feature where two out of three fields can be edited for each line by clicking a button that toggles between Yes and No. When the button is clicked, it s ...

Adding a datepicker popup to an input field in angular2 with the format of YYYY-MM-DD hh:mm:ss is as simple as following

Can someone help me with adding a datepicker popup to an input field in Angular, using the format YYYY-MM-DD hh:mm:ss? I have tried the following code in my .html file but it doesn't seem to be working. <input [(ngModel)]="date2" ngui-datetime-pi ...

What is the best way to incorporate a formArray into a formGroup?

Before anything else, I want to apologize for any errors in my English. I seem to be having trouble adding an array field to a formGroup. My issue arises when attempting to use the push method to add a formArray to my rate formGroup. It appears that the ...

Is Angular equipped with named routes or states similar to UIRouter?

When working with AngularJS and ui-router, we define a state like this: .state('myState', { url: 'some-user-friendly-url' ... }) This allows us to easily specify the URL and name of the state. It simplifies navigation by letting ...

Exploring nested promises in TypeScript and Angular 2

I have a method called fallbackToLocalDBfileOrLocalStorageDB, which returns a promise and calls another method named getDBfileXHR, also returning a promise. In the code snippet provided, I am unsure whether I need to use 'resolve()' explicitly o ...

The FullCalendar does not appear as expected on Angular version 16

https://i.stack.imgur.com/DTAKr.pngI followed the steps to install FullCalendar in my Ionic 6 Angular 16 app as outlined on https://fullcalendar.io/docs/angular. However, nothing is showing up in the browser (chrome). The Inspector tool shows that the Ful ...

Leverage elements from nearby npm repository when building an Angular 2 application

After developing a generic chart component using d3 and Angular 2, I decided to share it by publishing it in a local npm repository. This way, anyone can easily incorporate the chart component into their Angular project by simply running the npm install my ...

Splitting Angular Components with Angular as-split

Having trouble adding a "title" to my angular as-split-area. The titles are appearing together rather than above the as-split-area. <div style="width: 800px; height: 400px; background: yellow;"> <as-split direction="vertical&q ...

What is the best way to execute a function while utilizing its default options?

I am working on a project that involves a drop down box and an input box. Here is the HTML code for the drop-down box: <select #select id="id" class="class" (change)="onChange($event)"> <option value="1"> 1 </option> <option v ...

Having trouble integrating NEXT AUTH with Firebase due to an error: "Cannot import statement outside

Let's take a look at our firebase configuration file: import { getFirestore } from "firebase/firestore"; export const firebaseConfig = { apiKey: process.env.FIREBASE_API_KEY, authDomain: process.env.FIREBASE_AUTH_DOMAIN, projectId: pr ...

Issue: (SystemJS) Unable to find solutions for all parameters in $WebSocket: ([object Object], [object Object], ?)

Upon running the code snippet below, an error is thrown: Error: (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [object Object], ?). app.component.ts import { Component } from '@angular/core'; import {$WebSocket} ...

What is the best way to transform this JSON data into an array of key-value pairs in JavaScript?

Dealing with nested JSON data can be challenging, especially when trying to extract key-value pairs efficiently. If anyone has suggestions on how to simplify this process and improve readability, please share your insights. The goal is to transform the ne ...