What is the best way to organize chunk files into their own directory during the execution of ng build?

Is there a way to customize the configuration in order to automatically create separate folders within the "dist" directory for each chunk file in Angular 2?

Currently, when I use ngcli and build my project with ng build, it generates the "dist" folder, which is great.

Here is the basic structure of my project:

https://i.sstatic.net/T0WQM.png

However, I would like all *.chunk.js and *.map files to be placed in their respective folders within the dist directory. For example:

dist/modules/0.chunk.js

dist/modules/0.map

This way, the overall structure will resemble...

https://i.sstatic.net/auIDh.png

Answer №1

Although this issue may seem a bit dated, I encountered the same problem while working with Angular CLI versions 7 and 8.

As per the Angular documentation, options like '--resourceOutputPath' and '--deployUrl' were suggested. However, I found that --resourceOutputPath only worked for assets defined in stylesheets, and --deployUrl did not organize the assets into a separate folder as needed.

To address this, my current workaround involves:

  1. Setting the 'baseHref' option in the build configuration to '/static-files/'
  2. Copying the index.html file from the output folder and moving it up one directory level.

This approach results in a structure like this:

/dist
   index.html
   /static-files
      chunk1.js
      chunk2.js
      etc

While this method places all assets in a separate folder, not just JavaScript files, it serves its purpose effectively for me.

Note: If you are also utilizing Angular Routing, remember to manually set the 'APP_BASE_HREF', as the router will otherwise search for 'staric-files' in the URL (https://angular.io/api/common/APP_BASE_HREF)

Answer №2

To optimize your Angular app, you can utilize Angular CLI to create multiple NgModules and implement Lazy Loading in the Router configuration. This way, when you run ng build --prod, each module will be stored in a separate file for improved performance.

If you want to learn more about lazy loading in Angular, check out these recommended articles:

Answer №3

Consider utilizing a distinct build tool such as gup or grunt, which can organize the chunk files into a separate directory. Alternatively, you could implement a Compiler option like { outDir: 'src/chunk' } in your tsconfig.json file. Give this a try to see if it resolves the problem you are experiencing.

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

Utilizing AJAX or Angular in Django

I'm currently creating a blog platform with Django that allows users to post and comment. I've encountered an issue where adding a comment redirects the user to another page instead of staying on the same page. How can I utilize AJAX or AngularJS ...

How can I implement a master-detail view in Angular 2?

Update: I finally figured it out. After moving all the html from index.html to app.component.ts, everything started working perfectly. The method described below is the correct one. I've been facing an issue where I have a ParentComponent that retrie ...

The counterpart to node's http-proxy in the .NET framework

I am in search of a .NET solution that can serve as a proxy for API requests between a client application and a rest API. I discovered a potential solution that utilizes a node.js back end, which is the node http-proxy method demonstrated in this example. ...

Having trouble with my React component timer not functioning properly

How can I utilize the Header Component as a Clock timer for my webpage to update every second? Despite searching on Google, I couldn't find examples that match my requirements. Why is the tick() function not functioning properly even though there are ...

Utilizing Next.js and React to interact with Open AI through API requests

I'm currently experimenting with the OpenAI API to develop a chatbot using React, TypeScript, and Next.js. I am facing an issue where clicking the send button in the UI does not trigger any action. Even after adding console.log statements, nothing sho ...

Angular 2 template can randomly display elements by shuffling the object of objects

I am working with a collection of objects that have the following structure: https://i.stack.imgur.com/ej63v.png To display all images in my template, I am using Object.keys Within the component: this.objectKeys = Object.keys; In the template: <ul ...

How can time duration be accurately represented in TypeScript?

As I work on designing an interface for my personal project, I am in need of adding a field that represents the length of time taken to complete a task. What data type and format should I use in TypeScript to express this? Below is the interface I have cr ...

Loading CSS files conditionally in Angular2's index.html

Currently, my index.html page features a dark theme: <base href="/"> <html> <head> <title>XXX</title> </head> <body> <link rel="stylesheet" type="text/css" href="assets/dark_room.css"> <my-app ...

What is the best way to change a number into a byte string using TypeScript?

If I have a number represented by 3 bytes in hexadecimal form as 0x303132, how can I transform this number into a string of three characters with the same value - '012' - where each character represents the ASCII value of the corresponding byte? ...

Adding a baseURI to the image src in Angular 5 is causing issues with dynamically loading images

I am currently working on Angular 5.2.1 and I am facing an issue with loading an image from a server using its source URL. Here is the HTML code: <img #image [src]="cover" class="img-fluid" alt="NO image"> And here is the TypeScript code in image- ...

Exploring Angular 6: Unveiling the Secrets of Angular Specific Attributes

When working with a component, I have included the angular i18n attribute like so: <app-text i18n="meaning|description"> DeveloperText </app-text> I am trying to retrieve this property. I attempted using ElementRef to access nativeElement, bu ...

Detecting the language of a browser

In my Angular2 application, I am looking to identify the browser language and use that information to send a request to the backend REST API with localization settings and variable IDs that require translation. Once the request is processed, I will receive ...

Angular2: PrimeNG - Error Retrieving Data (404 Not Found)

I'm facing an issue with using Dialog from the PrimeNG Module. The error message I keep getting is: Unhandled Promise rejection: (SystemJS) Error: XHR error (404 Not Found) loading http://localhost:4200/node_modules/primeng/primeng.js I followed the ...

Tips for importing several makeStyles in tss-react

When upgrading from MUI4 to MUI5 using tss-react, we encountered a problem with multiple styles imports in some files. const { classes } = GridStyles(); const { classes } = IntakeTableStyles(); const { classes } = CommonThemeStyles(); This resulted in ...

Guide to connecting an object's attribute with an Angular2 form element using select

Currently facing an issue with angular forms. I am attempting to set up a form that retrieves data from a mongo collection and presents it using the <select> directive. Utilizing a FormBuilder to set it up as shown below: ngOnInit() { this.addFo ...

Build an Angular application without relying on the Angular CLI tool

Hello there! I was wondering if anyone could provide me with some guidance (or direct me to useful articles) on starting an angular project 4 from the ground up, without relying on angular-cli? I am just starting out and eager to learn how to develop an ...

Discover the potential of JavaScript's match object and unleash its power through

In the given data source, there is a key called 'isEdit' which has a boolean value. The column value in the data source matches the keys in the tempValues. After comparison, we check if the value of 'isEdit' from the data source is true ...

Sharing Pictures and Messages using angular, express, and multer

Struggling with posting images and text to the server while self-learning Angular. The backend works fine in Postman testing, but fails when working with Angular. Here is the code snippet I have been using: upload.component.html <form [formGroup]="upl ...

Creating and sharing a project in Typescript

My typescript project tends to have its code scattered across multiple files during development. Is there a recommended method for packaging this project into a single js and d.ts file for distribution? ...

What is the reason for the restriction on CORS with a custom scheme handler in AddCrossOriginWhitelistEntry or IsCorsEnabled?

Situation Our setup involves utilizing CefSharp (v57.0) to host an Angular2 (v4.2) application that shares components between a web application and game UI client. The goal is for the game client to send requests to the server from within the CEFSharp con ...