Splitting angular2 and browserify into individual vendor and app bundles can help prevent import errors

I am currently developing an Angular2 application and I am aiming to use gulp and browserify for the build process.
By utilizing tsify, I was able to create a standalone bundle.js file, which ended up being 1.4M in size after minification.

What I desire is to have two distinct bundle files: one for vendor dependencies and another for my application.

Essentially, here is what I envision:

  <!-- Ideal index.html script tags -->
  <script src="bundle_vendors.js"></script>
  <script src="bundle_app.js"></script>

This is the approach I took via command line:

  1. I first generated the vendor bundle:

    browserify app/vendor.ts -p [tsify ] > bundle_vendors.js
    

The content of my vendor.ts file simply consists of import statements:

import 'zone.js'
import 'reflect-metadata'
import '@angular/core'
import '@angular/http'
import '@angular/router'
import '@angular/common'
import '@angular/platform-browser'
import '@angular/platform-browser-dynamic'
import 'rxjs/Rx'
  1. Next, I created the application bundle:

    browserify -x @angular/core -x @angular/common -x @angular/http -x @angular/router -x rxjs/Rx -x @angular/platform-browser -x zone.js -x reflect-metadata -x @angular/platform-browser-dynamic app/main.ts -p [tsify ] > bundle_app.js
    

Here is how my tsconfig.json looks like:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "lib": ["es6", "dom"],  
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": false
  },
  "compileOnSave": false,
  "exclude": [
  ]
}

The positive side: The second bundle specifically contains my application code and it's significantly smaller!
The downside: It doesn't seem to work as intended. When loaded in the browser, I encounter an error stating

Uncaught Error: Cannot find module '@angular/core'
.

Now onto the questions:

  • Is there something obvious that I might be overlooking?
  • How can I determine the modules available for require after loading the bundle_vendors.js? Essentially, I'm seeking a list of modules that are 'exported' for other bundles to import.

I'm a bit lost on where to begin debugging this issue.

According to my research, an alternative could be using angular-cli (which utilizes webpack), but since the project already uses gulp, I prefer to make it work with the current setup. Plus, I have invested quite some time into it now.

Any assistance would be highly appreciated!

Answer №1

There are a couple of issues at hand:

  1. In order to require modules from bundle_vendors.js, the -r option is not being utilized.
  2. tsify has a bug that interferes with Browserify's -r feature, causing a malfunction.

The second issue has been resolved and an updated version of tsify (3.0.0) is now available on NPM.

To address the first problem, modify the build command for bundle_vendors.js as follows:

browserify -r @angular/core -r @angular/common -r @angular/http -r @angular/router -r rxjs/Rx -r @angular/platform-browser -r zone.js -r reflect-metadata -r @angular/platform-browser-dynamic app/vendor.ts -p [tsify] > bundle_vendors.js

Furthermore, it may be beneficial to include the skipLibCheck compiler option in your tsconfig.json:

TypeScript 2.0 introduces a new --skipLibCheck compiler option that allows skipping type checking for declaration files (files with the extension .d.ts). When dealing with extensive declaration files, the compiler can save time by avoiding unnecessary checks on already error-free declarations, which might significantly reduce compilation times.

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

Using TemplateRef as Custom Component in Angular ng-bootstrap Modal Opening is not Supported

When attempting to use Angular ng-bootstrap Modal open with a custom component passed from a template as a TemplateRef, I encountered some limitations. Initially, my approach was to call the Modal like this: this.modalService.open(ModalWindowComponent, { ...

Encountered a style group error 'non-collision' while using Angular with HERE Maps JS API 3.1

Occasionally, I encounter an error when trying to load a HERE map with the satellite base layer: Tangram [error]: Error for style group 'non-collision' for tile 13/16/15542/12554/15 Cannot read property 'retain' of undefined: TypeE ...

Typescript defines types for parameters used in callbacks for an event bus

Encountering a TypeScript error with our custom event bus: TS2345: Argument of type 'unknown' is not assignable to parameter of type 'AccountInfo | undefined'. Type 'unknown The event bus utilizes unknown[] as an argument for ca ...

Incorporating a d3 chart into an Angular 4 project

Currently, I am in the process of developing an Angular application using TypeScript. My aim is to incorporate a force directed network graph from Mike Boston built with d3, which can be viewed here. After successfully translating most of the code to Type ...

ADAL-Node: Unable to locate tenant groups

When the authority URL is similar to (where the domain name belongs to your tenant), an error occurs: The Get Token request returned an HTTP error: 400 with the server response stating "error description AADSTS90002 Tenant 'organizations' not ...

Improving characteristics of an Observable Entity (Angular)

I have a question about Observables that I'm hoping someone can help me with. I am trying to work with an object that I want to turn into an Observable. Let's say the object is structured like this: export class Sample { public name: string; ...

Angular Material datetime picker with an option for transparent background

After upgrading my angular from version 15 to 16, I encountered a strange issue with the material date time picker. The GUI part of the picker appeared half transparent as shown in this image: https://i.sstatic.net/LC91N.png Has anyone else faced this ki ...

How can one identify the optional fields in TypeScript interfaces generated from GRPC proto-files?

Within my ts-node project, I am converting TypeScript from gRPC proto files, where certain properties are denoted as optional. However, the resulting TS interfaces end up with ALL properties being marked as optional. Additionally, an extra "_" prefixed pr ...

Kubernetes directs incoming traffic to the /api/* endpoint through nginx

In my Kubernetes cluster, I have nginx configured to route traffic to my Angular application. Everything is working as expected, but I want nginx to redirect traffic to my Express application running on port 3000 when accessing myipaddress/api/v1. Addition ...

Improving the utilization of services in Angular

I have a DatesService that handles date manipulation. Additionally, I have two services that require date manipulation - EventsService and CalendarService. The CalendarService utilizes the EventsService. My query is: what would be more efficient (in terms ...

Unable to access data from the Array by passing the index as an argument to the method

Having trouble retrieving an item from an Array using method() with an index argument that returns undefined export class DataService { public list = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, ...

Show the chosen autocomplete value on the table for visibility

Currently, I am utilizing the autocomplete and table components simultaneously on the same page/component. When a particular player is selected from the autocomplete component, I aim to showcase the details of the chosen player (such as NAME and AGE) on th ...

In Bootstrap 3, rows are aligned on the same line

My issue is that two rows are coming on the same line in Bootstrap 3. Here is the code for a chat screen with two people: Code [<div class="row single-row mt-10" style="float: left !important;> <div class="col-2" styl ...

The function `find()` will not provide any data, it will only output `undefined`

I am trying to extract the `s_id` field from this JSON data: { "StatusCode": 0, "StatusMessage": "OK", "StatusDescription": [ { "s_id": "11E8C70C8A5D78888E6EFA163EBBBC1D", "s_serial": " ...

How to link a selection choice to an option using Angular 13

Can anyone provide guidance on how to bind data with a select option in Angular? I've tried multiple approaches but haven't been successful in getting the data to display in the options. Any assistance would be greatly appreciated. Thank you in a ...

Utilize array mapping to alter the complete object

In my project using TypeScript (Angular 2), I am working on creating a "reset" method for an object array: cars [ { id: 1, color: white, brand: Ford, model: Mustang, ... }, ... ] Users have the ability to modify these objects, ...

The ngx-translate library could not be located within Ionic Framework 6

Currently, I am looking to incorporate the ngx-translate Pipe for translating my Ionic application. In my app.module.ts file: export function createTranslateLoader(http: HttpClient): TranslateHttpLoader { return new TranslateHttpLoader(http, './ass ...

Using custom properties from the Material-UI theme object with custom props in Emotion styled components: a step-by-step guide

I have implemented a custom object called fTokens into the MUI theme using Module augmentation in TypeScript This is how my theme.d.ts file is structured declare module "@mui/material/styles" { interface FPalette { ... } interface FTokens ...

Iterating over a JSON array using *ngFor

Here is the JSON structure that I have: { "first_name": "Peter", "surname": "Parker", "adresses": { "adress": [{ "info1": "intern", "info2": "bla1" }, { "info1": "extern", "info2": "bla2" }, { "info1": " ...

Dealing with Angular error interceptor and handling multiple occurrences of 401 responses

I'm currently working on implementing a refresh token feature in my Angular frontend to handle expired access tokens. The issue I'm facing is that upon reloading the page after token expiration, multiple requests are sent to the backend simultane ...