I am encountering an issue with the `/deep/` selector while using sass-loader in my VueJS project

I'm currently working on implementing SASS in our Vue 2.6.10 webpack application as our team is looking to transition from LESS to SCSS for our legacy application. In my package.json, I've added the following dependencies under devDependencies based on the documentation:

  • sass (v1.32.8)
  • sass-loader (v10.1.1)

Additionally, I've included node-sass (v5.0.0) in my dependencies. My current node version is v15.4.0, which aligns with the requirements stated in the documentation for node-sass v5+.

Within my Vue components, I'm attempting to utilize selectors like /deep/, ::v-deep, or >>>. In a functional sandbox playground that mirrors the versions of tools used in my project, I managed to make it work by targeting a child component's styles within a parent component using /deep/ as shown below:

<style lang="scss" scoped>
  .parent-container {
    & /deep/ .child-comp {
      background-color: tomato;
      color: white;
    }
  }
</style>

However, upon replicating the exact setup in my actual application, I encountered a compile error:

Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: expected selector.
   ╷
77 │   & /deep/ .child-test{
   │     ^
   ╵
  src/components/MenuBarComp.vue 77:5  root stylesheet

Despite trying various suggestions, including using ::v-deep and >>> selectors instead, none yielded the desired output. This led me to discover that these selectors might not be supported in SCSS according to certain sources.

After multiple attempts over several days, such as downgrading versions of node-sass, sass, and sass-loader, running npm rebuild node-sass --force, deleting the node_modules directory, clearing the npm cache, and more, the issue persists.

The existing CSS-related packages listed in my package.json are as follows:

  • css-loader (v3.1.0)
  • less (v3.0.4)
  • less-loader (v4.1.0)
  • mini-css-extract-plugin (v0.6.0)
  • optimize-css-assets-webpack-plugin (v5.0.3)
  • postcss-import (v11.0.0)
  • postcss-loader (v2.0.8)
  • postcss-url (v7.2.1)
  • vue-style-loader (v3.0.1)

Below is an overview of how my loaders are configured in webpack:

function generateLoaders(loader, loaderOptions) {
    const loaders = options.usePostCSS
      ? [cssLoader, postcssLoader]
      : [cssLoader];

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap,
        }),
      });
    }

    // Extract CSS when specified during production build
    if (options.extract) {
      return [MiniCssExtractPlugin.loader].concat(loaders);
    } else {
      return ['vue-style-loader'].concat(loaders);
    }
}

return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass'),
    scss: generateLoaders('sass', {
      additionalData: `
          @import "@/styles/_variables.scss";
        `,
    }),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus'),
};
};

If anyone has insights on resolving the issue with the /deep/ selector in this context, your assistance would be greatly appreciated!

Answer №1

The reason behind this is that the usage of /deep/ is not compatible with sass. To resolve this issue, you must explicitly configure your sass-loader to utilize node-sass in the following manner:

loader: 'sass-loader', options: { implementation: require('node-sass')

On their page, sass-loader includes a warning that states:

Be cautious when both node-sass and sass are installed! By default, sass-loader favors sass. To prevent conflicts, you can specify the implementation option.

This option allows you to choose between using sass (Dart Sass) or node-sass as a module.

Therefore, the appropriate configuration for your code within sass-loader should look like this:

scss: generateLoaders('sass', {
  implementation: require('node-sass'),
  additionalData: `
      @import "@/styles/_variables.scss";
    `,
}),

Answer №2

Many developers encounter a common issue with new angular builds caused by version mismatches in angular-build. To address this problem, we can utilize the following solution:

:host ::ng-deep as an alternative to /deep/

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

Unable to display image using HTML and CSS

I want to display two images (download and view) on top of another set of images. Here is the HTML code: <div class="row"> <div class="col-md-3 col-sm-4 col-xs-6 backgrounddownload backgroundview"> <a href="{site_url}scents/baobab/ ...

Tips for aligning Bootstrap 4 cards when the first card header breaks at a word and the second card header does not

Currently, I am working on a personal project using bootstrap 4. However, I've encountered an issue with the class "card" in my gallery section. One card header has 'white-space' set which breaks the word, while the other does not have any & ...

What is the best way to set the value of the days in a month to a div

I've been experimenting with creating a calendar using javascript, HTML, and CSS on my own. I managed to achieve some functionality like obtaining the current month and year as well as the previous month and year by clicking a button in HTML. However, ...

Tips for adding images using v-for in Vue 3

Having some trouble creating a set of images using a v-for loop, as it seems to be not recognizing the variables inside the tag. The picture is broken and the alt attribute just shows me {{ item.alt }}. Here is my HTML: <section class="our-technolo ...

Issues encountered when running a Vue.js project that already exists

I'm encountering an issue while trying to launch a pre-built UI project using vue.js. Despite having Python installed with the environment variable properly set, I keep getting an error when running the npm install command. How can this be resolved? n ...

Endless scrolling with Laravel and Vue fails to load data due to request not being sent

Welcome everyone, I am new to using vue.js. I have encountered a simple issue with infinite scroll. Even though I have configured it as shown below, when the page is reloaded, the request to fetch data from the database for page 1 is not sent. I would appr ...

Pinia shop: Fetching initial data upon store creation

Within my Vue application, I have implemented various Pinia stores. Most of these stores require initialization with data fetched from a server, which involves waiting for a server response. To achieve this, I am utilizing the Setup style stores. I aim to ...

How can I pass server-side variables to a Vue instance using webpack and vue-cli?

I am currently working on a project with vue-cli, which creates a Node project with webpack. I can successfully build all scripts and also utilize the pug language within .vue files. However, the project uses HTML-Webpack-Plugin during the build process, w ...

Conceal the Vue router on a webpage

How can I hide the vue-router from my login page? I want to remove the menu on the Login page. Is it possible and how would I achieve that? Here is the code for the Login page: Login <template> <div> <h1>Login</h1> ...

Deploying an AngularJS 1.x application bundled with Webpack to Tomcat server

I've scoured the depths of Google and combed through the official documentation for Webpack, but I’ve yet to stumble upon a tutorial, guide, or plugin that addresses this particular issue. Does anyone have any experience with this problem, or should ...

Preventing Scrollable Div Content from Displaying Behind a Fixed Div using CSS

Seeking help with a message_container division that contains a fixed scrollable message_header division and a scrollable message_body division. The issue I am facing is that the content of the message_body division is appearing behind the fixed div. How ca ...

Encountering the issue of "Module build failed: Error: Cannot find module 'ajv'" can be frustrating and can disrupt your workflow

Encountering the error message Module build failed: Error: Cannot find module 'ajv' while attempting to execute vue-cli-service serve --mode dev in my application. The package ajv is installed as a dependency of other packages, and not directly ...

Managing touch devices and animations when hovering

Is there a way to remove or prevent all hover effects on a touch device? I've tried various scripts, but none seem to work for me. How can I test it with dev tools? I attempted this script but received an error: Cannot read property 'addEventList ...

Adjusting the position of the parent div by manipulating the bottom property

Is there a way to toggle the bottom property of a div when clicking its child anchor tag? <div class="nav" :class="{ 'full-width': isFullWidth }"> <a class="toggle-button" @click="toggleNav"><i class="fa fa-angle-down">< ...

Limit image size in Outlook when using Mailchimp

I'm currently working on coding a Mailchimp template for a client and I've run into an issue with image dimensions. The problem arises when images exceed the width of the template (usually 600px), as they are displayed at their original size in ...

Using an array input in a Vue.js v-for loop

Creating a form to collect information about multiple persons can be challenging. Let's say we have 3 people to gather info on, and we need to structure the data in JSON format like this: { persons[0].surname: '', persons[0].name: &apos ...

Creating a stylish diagonal background with CSS has never been easier!

Can CSS be utilized to achieve the design effect depicted in the image below? https://i.sstatic.net/o1k7O.png I am seeking to generate <div> elements with a diagonal split background featuring a solid color on one side and white on the other. ...

Dynamically changing the borders of WebGL canvases: a guide to adding and

Here is my code snippet for creating an HTML canvas: <canvas id="glCanvas" class="canvases" width="20" height="20></canvas> To set the color, I am using the following: gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); I am w ...

Tips for refreshing the CSS cache in Symfony2 production when the content has been updated

When minifying the CSS in production (using php app/console assetic:dump), the file is recreated but the name remains the same. As a result, users' browsers continue to cache the old version. One workaround is to add a parameter like ?v=12 after the ...

Setting up bootstrap for PHP: A step-by-step guide

Struggling with integrating PHP and Bootstrap? I recently attempted to do the same, but unfortunately, my website doesn't seem to recognize Bootstrap. Instead of the sleek Bootstrap design, it's displaying a simple HTML layout. Here's a snip ...