SystemJS is responsible for loading a multitude of files related to rxjs

After finding an example on the internet, I have created my own systemjs.config.js:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'js:': 'js/',
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'js:angular2/core.umd.js',
            '@angular/common': 'js:angular2/common.umd.js',
            '@angular/compiler': 'js:angular2/compiler.umd.js',
            '@angular/platform-browser': 'js:angular2/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'js:angular2/platform-browser-dynamic.umd.js',
            '@angular/http': 'js:angular2/http.umd.js',
            '@angular/router': 'js:angular2/router.umd.js',
            '@angular/forms': 'js:angular2/forms.umd.js',
            '@angular/upgrade': 'js:angular2/upgrade.umd.js',
            // other libraries
            'rxjs': 'js:rxjs',
            'angular-in-memory-web-api': 'js:in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

However, the individual rxjs files loading slowed down my Angular2 application. So, I decided to bundle RxJs in js/rxjs/bundles/Rx.js. Here's the modified systemjs.config.js:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'js:': 'js/',
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'js:angular2/core.umd.js',
            '@angular/common': 'js:angular2/common.umd.js',
            '@angular/compiler': 'js:angular2/compiler.umd.js',
            '@angular/platform-browser': 'js:angular2/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'js:angular2/platform-browser-dynamic.umd.js',
            '@angular/http': 'js:angular2/http.umd.js',
            '@angular/router': 'js:angular2/router.umd.js',
            '@angular/forms': 'js:angular2/forms.umd.js',
            '@angular/upgrade': 'js:angular2/upgrade.umd.js',
            // other libraries
            'rxjs': 'js:rxjs/bundles/Rx.js',
            'angular-in-memory-web-api': 'js:in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

This change led to an error when starting my application:

Error: (SystemJS) Syntax error
    SyntaxError: Syntax error
       at Anonymous function (eval code:2:1)
    Evaluating http://localhost:37191/js/rxjs/bundles/Rx.js/Subject
    Evaluating http://localhost:37191/app/main.js
    Error loading http://localhost:37191/app/main.js

The structure of my main.ts file:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

And this is how my app.module.ts looks:

import 'rxjs';

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { DeviceGroupsViewComponent } from './device-groups-view.component';

import { DeviceGroupService } from './devicegroup.service';
import { SourceTypeService } from './sourcetype.service';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AppRoutingModule
    ],
    declarations: [
        AppComponent,
        DeviceGroupsViewComponent
    ],
    providers: [
        DeviceGroupService,
        SourceTypeService
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

The question now is, what adjustments are needed to make my app work with the bundled Rx.js?

Answer №1

If you're using the angular2 quickstart setup, then you're on the right track.

However, it's crucial that you do not:

import {something} from 'rxjs/Rx';

This is incorrect as it will unnecessarily import the entire RxJS library (when you only need Observable).

import {Observable} from 'rxjs/Rx';

This is the correct way to do it.

import {Observable} from 'rxjs/Observable';

and will help reduce the number of imports. Make sure your app doesn't reference 'rxjs/Rx.'

PS You should avoid importing the entire RxJS library in one file because it would be too large. That's why it's recommended to only import the necessary RxJS modules.

Answer №2

A while back I created a demonstration that utilizes Angular2 and loads RxJS as a unified bundle.

Essentially, my approach was very simple:

paths: {
    'rxjs*': 'https://unpkg.com/@reactivex/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebcb6a4bd8efbe0fee0fee3acababefe0fffc">[email protected]</a>/dist/global/Rx.js'
},

The key element here is rxjs*, which corresponds to items such as rxjs/Subject or rxjs/add/operator/concat.

To view the live demo on plnkr, visit: http://plnkr.co/edit/rnO74mQNuPsHAmrIVJ8j?p=preview

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

The component does not contain the specified property

One Angular 4 component that I have is like this: export class MenuComponent { constructor(private menuService: MenuService) { } @Input(nodes):any; getMenu(path:string): void { this.menuService.getData(path).subscribe(data => { // Re ...

When combining Angular4, Protractor, Cucumber, TypeScript, and webpack-dev-server, Angular seems to have gone missing

Operating System: macOS Sierra 10.12.4 Node Version: v7.9.0 Npm Version: 5.0.3 Cucumber-js Version: 2.3.0 Protractor Version: 4.0.14 TypeScript Version: 2.2.2 Webpack-dev-server Version: 2.4.5 I encountered an issue while running end-to-end tests. When tr ...

Ways to confirm non-null values and bypass the row if it is

I have been attempting to compare 2 dates in order to appropriately display data in a table. I have tried the following approach: ${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()} However, there is an issue where CreateDate has a null value ...

Combining promises to handle the asynchronous promise received from this.storage.get() function

Struggling with managing asynchronous data retrieval from local storage in my Angular2/ionic2 app. The code snippet I'm using: request(args) { var headers = new Headers(); headers.append('Content-Type', 'application/json&a ...

Next.js (TypeScript) - Error: Property 'req' is not recognized on the type 'GetServerSideProps'

Currently, I am tackling a challenge involving the utilization of next-auth and attempting to access the session from within getServerSideProps. However, in order to achieve this, it is essential for me to provide the context request and context response ...

Exploring the Power of TailwindCss in Storybook 6 for Angular Development

I am in the process of creating a component library using Angular version 11.2.8, and I'm attempting to integrate TailwindCss and Storybook 6. Despite trying various configurations, none seem to be working correctly for me. Whenever I run Storybook, ...

Discover additional information within extensive text by leveraging Angular 4 features

Looking for a solution to truncate text and limit it to 40 words, then display the full text when clicking on a "see more" link. I experimented with ng2-truncate, read-more-directive, and ng-text-truncate-2, but they were not compatible with Angular 4. ...

Discover additional features in Angular 4 by reading further

I am struggling with incorporating a "read more" feature in my paragraph on my website. The challenge I'm facing is determining how to trigger the feature to display only when there are more than 5 lines of text. <p>Contrary to popular belief, ...

Angular-cli does not come with the Bootstrap framework pre-installed

After installing the bootstrap framework to my angular2 project through npm, I noticed that it appeared in the node-modules folder. However, upon checking the angular-cli file, I discovered that the boostrap script and css were not included. "apps": [ ...

What could be causing the Angular 5 error: "Cannot find exported module 'OpaqueToken'."?

I am currently in the process of upgrading my Angular 4 application to Angular 5. During this upgrade, I encountered the following error message: ERROR in src/app/application/services/generated/variables.ts(1,10): error TS2305: Module '"..../no ...

Filtering JSON data with Angular 4 input range feature

Hello there, I'm currently working on a search pipe in Angular 4 to filter company team sizes from JSON data using a range input between 0 and 100. However, I'm facing an issue with the range filter as I am relatively new to TypeScript and Angula ...

Uploading Images to Imgur with Angular 4

As a newcomer to TypeScript, I am faced with the challenge of uploading an image to the Imgur API using Angular. Currently, my approach involves retrieving the file from a file picker using the following code: let eventObj: MSInputMethodContext = <MSIn ...

Encountering an issue with Ionic forms: Error message NodeInjector: NOT_FOUND [ControlContainer] is displayed

I have developed an Ionic application with a form. Everything was working fine until I integrated the form group and related elements into my code. Since then, I've been encountering this error: core.js:6260 ERROR Error: Uncaught (in promise): Erro ...

What is the correct way to use Observables in Angular to send an array from a Parent component to a Child

Initially, the task was to send JSON data from the parent component to the child component. However, loading the data through an HTTP request in the ngOnInit event posed a challenge as the data wasn't being transmitted to the child component. Here is ...

Unfortunately, my capabilities do not allow me to execute the command 'ng build --configuration production

This is the issue that I am facing and need assistance: Error: src/app/app.component.html:1:1 - error NG8001: 'fuse-progress-bar' is not recognized as a valid element: If 'fuse-progress-bar' is an Angular component, please ensure that ...

Matching TypeScript search field names with column names

Seeking ways to create an API that allows admins to search for users in the database using various fields. // Define allowed search fields type SearchFieldType = 'name' | 'memberNo' | 'email' | 'companyName'; const ...

You cannot call this expression. The type 'String' does not have any call signatures. Error ts(2349)

Here is the User class I am working with: class User { private _email: string; public get email(): string { return this._email; } public set email(value: string) { this._email = value; } ...

In what way does Ionic determine the URL for input and send the password-protected email for logging in based on that URL?

I've set up a section for inserting the URL into the login form page using the following code: <ion-item> <ion-label floating>Company URL</ion-label> <ion-input type="url" value="" required></ion-input> </ion-item> ...

Encountered an issue while trying to add @angular/fire to the project - unable to resolve

Having encountered some issues with the commands I used in these versions. Can anyone provide assistance in resolving this matter? Your help is greatly appreciated. ------------------------------------------- Angular CLI: 14.0.0 Node: 16.15.1 Package ...

Can anyone provide guidance on setting up a TypeScript service worker in Vue 3 using the vite-plugin-pwa extension?

I am looking to develop a single-page application that can be accessed offline. To achieve this, I have decided to implement a PWA Service Worker in my Vue webapp using TypeScript and Workbox. I found useful examples and guidance on how to do this at . Ho ...