Utilize an alias to define the SCSS path in an Angular-CLI library project

I am in the process of developing a library project using angular-cli. I have been following the guidelines outlined in the angular documentation. This has resulted in the creation of two main folders: one is located at ./root/src/app, where I can showcase a test application to demonstrate my library's capabilities, and the other is at ./root/projects/library-name, containing the functionality of the library itself.

While working within the ./root/src/app folder, I am able to easily import modules, components, and classes thanks to the paths configuration set up in my tsconfig.json file by angular-cli.

{
  "compilerOptions": {
    "paths": {
      "my-library/*": [
        "dist/my-library/*"
      ]
    }
  }
}

This configuration essentially simulates the presence of our library as if it were installed as a node_module, even though the library is actually built in the ./root/dist/library-name directory.

However, an issue arises with SCSS files not adhering to the same rules as outlined in tsconfig.json. If I want to include SCSS files in my library for consumers to access, they can do so easily by importing them using @import '~library-name/scss';. Unfortunately, this doesn't work in my test application located in ./root/src/app.

To tackle this problem, how can I configure my SCSS preprocessor settings in the angular.json file to replicate what angular-cli has done with tsconfig.json but for SCSS? In essence, how can I set up a directory alias for my library's build output specifically for SCSS files?

EDIT: It's important to note that once the library is published, the SCSS files are accessible without any issues when consumed by external applications. The challenge lies in accessing the published SCSS within my ./root/src/app test application.

Answer №1

I encountered a similar issue with my project recently. I was looking to export some SCSS files alongside my library, inspired by the themes folder structure in Angular Material. After researching, I discovered that using the scss-bundle library provided a feasible solution since there were limited options available within the Angular CLI.

The process of utilizing scss-bundle is quite straightforward. You simply need to create a configuration file like this:

{
    "entry": "src/lib/themes/_core.scss",
    "dest": "dist/themes/_theme.scss"
}

Then, execute the following command whenever you build your library:

scss-bundle -c ./scss-bundle.config.json

For further information, you can access the GitHub repository for any specific requirements you may have.

UPDATE: Modifying the SASS loader configuration for easier importing of SCSS files within the application project

If you wish to import SCSS partials as you would from node_modules, adjustments need to be made to webpack and sass-loader configurations. The webpack configuration is typically concealed within the Angular CLI, and depending on your CLI version, there are two methods to modify it.

1.x In order to adjust the webpack config file, you must use ng eject. Refer to the official documentation for guidance.

6.x With the temporary disabling of ng eject in CLI version 6, there is an alternative approach outlined in this comprehensive guide here.

Subsequently, it is essential to configure sass-loader to recognize your library project as the root path. You can find detailed instructions on achieving this in the answer provided here.

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

Having trouble retrieving files from an Angular2 service

I am facing an issue in creating an Angular2 service for downloading files from the server. I have a table where each record represents a single file. When clicking on a specific record, the download method is called: download(r: FileObject) { this.re ...

What is the best Node version for me to use?

When downloading Node, you are given the option of choosing between LTS and Current versions. The LTS version is 4.6.0, while the current version is 6.7.0. I opted for the LTS version, which comes bundled with npm version 2.15.9. However, I encountered a ...

Leverage JSON files for pagination in NextJS

I am currently developing a science website where the post URLs are stored in a static JSON file. ScienceTopics.json- [ { "Subject": "Mathematics", "chapters": "mathematics", "contentList": [ ...

Guide to initiating an Angular 6 project using Angular CLI version 7.3.3

My current Angular CLI version is 7.7.3, but I need to create an Angular 6 project using it. When I use the CLI command 'ng new project_name', it only allows me to create an Angular 7 project. Is there a way to force creating an Angular 6 project ...

NPM displaying an error message

npm ERROR! Windows_NT 6.3.9600 npm ERROR! Command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\np ...

Shuffle and Place indented list

I have a bunch of ideas and a list of projects. I need to choose one idea and match it with a project. I followed this guide to implement the drag and drop feature, but encountered an issue where every project gets assigned the same idea when dragging and ...

Displaying data in a table using NgFor and allowing the user to input the number of columns

My component has the capability to accept an array input, such as [1, 2, 3, 4, 5, 6, 7], and a number of columns input. For example, if a user specifies 3 columns, I want to display the data in a table with 3 columns like this: 1 2 3 4 5 6 7 If the ...

Maintaining ongoing subscriptions using rxjs in Angular 5

As a newcomer to rxjs in Angular 5, I find it a bit challenging to articulate my question, but I am hopeful for some guidance. I frequently encounter the same setup: Multiple Components to display identical data A single Service for accessing the data ...

Issue encountered while trying to run `npm install` on an angular-cli

I recently moved my angular-cli project with node modules to a new directory. Upon running npm install, I encountered the following warnings: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2838c85978e8 ...

Removing a selected row from a data table using an HTTP request in Angular 2

I am working with a table that has data retrieved from the server. I need to delete a row by selecting the checkboxes and then clicking the delete button to remove it. Here is the code snippet: ...

Angular compilation alerted about a missing export: "ɵɵdefineInjectable was not located within '@angular/core'

I'm having an issue while trying to run my Angular application. The error message related to the "ngx-mqtt": "^6.6.0" dependency keeps popping up even though I have tried changing the versions multiple times. I am using CLI 6.2.9 and cannot seem to re ...

Exciting Angular feature: Dynamic Titles

I am working with an <i> tag <i class="actionicon icon-star" [ngClass]="{'yellow' : data.isLiked}" (click)="Like(data)" aria-hidden="true" title="Liked"></i> In my current set ...

Custom type checker that validates whether all properties of a generic object are not null or undefined

In an attempt to create a user-defined type guard function for a specific use-case, I am faced with a challenge: There are over 100 TypeScript functions, each requiring an options object. These functions utilize only certain properties from the object wh ...

Having trouble utilizing the ng-Command in Angular?

Currently, I am attempting to set up Angular in a vagrant-box environment. npm install -g @angular/cli Unfortunately, I encounter an error while trying to use the client: The program 'ng' is currently not installed. You can install it by typin ...

The `@ViewChild` reference cannot be found

My main challenge is toggling a @ViewChild element using an *ngIf, followed by invoking a native event. This snippet shows my HTML element, tagged with #audioPlayer for extracting it through @ViewChild. <audio #audioPlayer *ngIf="convers ...

Comparing two inherited classes in Typescript: A step-by-step guide

Let's say we have two classes: Animal and Dog. The Dog class is a subclass of the Animal class. I am trying to determine the types of these objects. How can I accomplish this task? class Animal {} class Dog extends Animal {} //The object can be of ...

Accepting undefined in rest parameter of typescript

I'm struggling with an exercise involving Function parameters: The maximum function below has the wrong type. To allow undefined in the rest arguments, you need to update the type of the rest parameter. Fortunately, you don't have to change the ...

Determining the quantity of variations within a union in Typescript

Is it possible to determine the number of types in a union type in Typescript, prior to runtime? Consider the following scenario: type unionOfThree = 'a' | 'b' | 'c'; const numberOfTypes = NumberOfTypes<unionOfThree>; c ...

What is the method for incorporating a reverse pipe into *ngFor that leverages an Observable?

I am developing an Ionic app that utilizes Firestore and AngularFire auto IDs for adding items to the collection. I am attempting to retrieve the Firestore collection and display it in reverse order. However, when I try to use the 'reverse' pipe, ...

"I am experiencing an issue with the PrimeNG year picker as it is unable

My goal was to set up a simple PrimeNG calendar with just a year picker. I followed the implementation instructions from the documentation: <p-calendar inputId="year" [(ngModel)]="date1" view="year" dateFormat=" ...