Encountered an issue with Angular while trying to import scss variables: Module parse failed due to an unexpected token at

Our project previously utilized a palette for importing styles, which functioned correctly in Angular 13. However, upon upgrading to Angular 14, the palette no longer works as expected. Below are the specific details of the issue:

Error: Module parse failed: Unexpected token (1:0)
File was processed with these loaders:
 * ./node_modules/resolve-url-loader/index.js
 * ./node_modules/@angular-devkit/build-angular/node_modules/sass-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
> :export {
|   -mainBlue-: "#4285EB";
|   -delete-: "#ff3f3f";

The error within palette.scss is related to the following code snippet:

:export {
  -mainBlue-: "#{$main-blue}";
  -delete-: "#{$delete}";
  -orange-: "#{$orange}";
  -white-: "#{$white}";
  -error-: "#{$error}";
}

We have a typescript file called palette.scss.ts that exports the following interface:

export interface Palette {
  mainBlue: string,
  delete: string,
  orange: string,
  white: string,
  error: string,
}

let palette;
export default palette;

This configuration is also included in our angular.json architect section like this:

"styles": [
  "src/styles/styles.scss",
  "src/styles/palette.scss",
]

When importing the palette in a component, we apply the following logic:

let stringifiedPalette: string = palette
  .replace(/(:export|\n|\s)/g,'')
  .replace(/;/g,',')
  .replace(/-/g,'"');
const lastCommaIndex = stringifiedPalette.lastIndexOf(',');
stringifiedPalette = stringifiedPalette.slice(0, lastCommaIndex) + stringifiedPalette.slice(lastCommaIndex + 1);
this.colorPalette = JSON.parse(stringifiedPalette);

If you have any suggestions on how to address this issue or if you would like more information on the breaking change, please refer to the following link: https://github.com/angular/angular-cli/issues/23273

I suspect it might be a configuration problem, but I have yet to find a solution that resolves it.

Answer №1

Just discovered that the previous method is no longer available as they have removed the feature:

The process of importing a .scss file in TypeScript is considered non-standard and heavily relies on the underlying build system, specifically Webpack in this case.

Although Angular CLI does incorporate Webpack, it is important to note that it is an implementation detail not officially supported by the Angular tooling team. Therefore, using Webpack specific features may result in unexpected issues.

It is advised not to depend on Webpack specific features due to the lack of support from the Angular tooling team.

https://github.com/angular/angular-cli/issues/19622

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

Executing the Ionic code within the Xcode Swift environment

I have developed an Ionic application that I successfully compiled in Xcode for iOS. Additionally, I have integrated a widget into the application. My goal is to set it up so that when the application is opened using the regular app icon, it displays the m ...

Creating a dynamic path to an imported file in React: A step-by-step guide

Struggling with a dilemma regarding dynamically generated paths for importing files in React. I have utilized the map() function to generate a dynamic part of the code, consisting of a repetitive sequence of div elements, each housing an audio element. The ...

Transforming an object into an interface in TypeScript

Recently, I have started learning Typescript and am currently working on a project where I am building a REST API. In this project, I have defined a specific model for my request payload. However, even after typecasting, the type of 'resObj' rem ...

Leverage the power of Firebase Firestore by seamlessly integrating full-text search capabilities with external services while also

While I understand that using external services like Algolia and Elasticsearch is necessary for full-text queries in Firestore, my struggle lies in integrating these tools with Firestore's existing querying options such as "where," "limit," and "start ...

Excluding a common attribute from a combined type of objects can lead to issues when accessing non-common attributes (TypeScript)

In the process of developing a wrapper function, I am passing a refs property into a send function. The Event type used to construct my state machine is defined as an intersection between a base interface { refs: NodeRefs } and a union of possible event ob ...

Simply output the integer value

Recently, I've been working on a function that I'm trying to condense into a one-liner code for a challenge on Codewars. You can find the problem here. Here's the code snippet that I currently have: export class G964 { public static dig ...

What is the best way to click on a particular button without activating every button on the page?

Struggling to create buttons labeled Add and Remove, as all the other buttons get triggered when I click on one. Here's the code snippet in question: function MyFruits() { const fruitsArray = [ 'banana', 'banana', & ...

React Typescript: Unable to set component as element

Currently, I am working on mapping my JSX component (Functional Component) inside an object for dynamic rendering. Here's what I have devised up to this point: Interface for Object interface Mappings { EC2: { component: React.FC<{}>; ...

Repeatedly invoke http.post function and finally return the observable

In order to attach the list of documents, each document can contain a maximum of 20 MB data and the user has the ability to attach an unlimited number of files. Consequently, I am initiating one file upload per REST call. Upon successfully saving all the ...

Issues with TypeScript "Compile on save" functionality in Visual Studio 2015

The feature of "Compile on save" is not functioning properly for me since I upgraded to Visual Studio 2015. Even though the status bar at the bottom of the IDE shows Output(s) generated successfully after I make changes to a .ts file and save it, the resul ...

Attempting to incorporate an npm package (specifically Howler) into an Angular 2 application

I'm facing an issue with importing Howler into my Angular 2 app as it doesn't have a typings file. Despite my efforts in searching for a solution, I haven't been able to find anything helpful. Can someone guide me on how to import "howler" i ...

Limit the height of a collapsed accordion section in Bootstrap 5 using Vue3

I'm currently in the process of building a web app using Vue3 and Bootstrap 5. However, I seem to be facing an issue that is specific to Bootstrap. My goal is to limit the height of the collapsed accordion segment so that it doesn't push the othe ...

An error occurred with code 1 when running the command "npm run build -- --prod"

I am currently working on a project in Visual Studio 2017 using Asp.Net Core 2 and Angular 5. While attempting to publish my project, I encountered the error message 'The command "npm run build -- --prod" exited with code 1' in the error list wi ...

Unable to use console log in shorthand arrow function while working with Typescript

When debugging an arrow function in JavaScript, you can write it like this: const sum = (a, b) => console.log(a, b) || a + b; This code will first log a and b to the console and then return the actual result of the function. However, when using TypeSc ...

How come code suggestion works flawlessly on Stackblitz, yet fails to function in Intellij?

Check out this GitHub repository for a simple way to reproduce the issue: https://github.com/tmtron/mathjs-typescript-types Successful Code Completion in Stackblitz Everything works perfectly when you open the project in Stackblitz: https://i.stack.imgur ...

I am curious if there is a method to obtain the component's logic or the value of an element's style within animation metadata in Angular

Is there a way to determine the height of a dynamically created component from a recursive tree without using the tree inside metadata declarations? One possibility is calculating the height inside the *ngStyle directive as shown below: <my-directory [ ...

Port 4200 is currently being utilized. Please utilize the '--port' option to designate an alternative port

Hi there, I am currently running an Angular 2 app with Express as the backend and encountering the following issue: [3] Port 4200 is already in use. Use '--port' to specify a different port. [3] [nodemon] app crashed - waiting for file changes b ...

Testing the Child Component's EventEmitter Functionality

In my child component, there is an event emitter that sends a boolean value when the style changes in the parent component: export class ExampleComponent implements OnInit, OnChanges { @Output() statusEvent = new EventEmitter<boolean>(); getS ...

How can I convert a MongoDB document into a DTO in NestJS?

I'm currently working with a data layer that interacts with a MongoDB database. My goal is to only handle MongoDB documents in this layer without exposing the implementation details to my services. My current approach involves the following code: // ...

Trick for using :checked in CSS

Is it possible to change the color of a deep linking div using an input checkbox hack in the code below? <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /&g ...