Extract SCSS import filepaths using NPM and Gulp

Currently, in my gulp setup, I have several CSS files being produced - a general 'core' one, and then template-specific stylesheets.

Instead of re-compiling all stylesheets with each change, the setup only compiles the necessary ones. However, this has resulted in complex "watch" paths that cover many files reflecting what's in the primary SCSS files for each template (which contain @use/@import statements).

I had an idea: rather than manually writing out all the watch filepaths, why not 'crawl' the SCSS file to automatically grab all the filepaths for any @use/@import statements recursively?

For example, let's take this "blog.scss" file:


    // Variables 
    @import 'variables';
    
    // Tools
    @import './tools';
    
    // Parts
    @import './core/base';
    @import './templates/blog';

Is there a way to extract all the filepaths for its @import statements? Like so:

'local-dev/styles/variables.scss'
'local-dev/styles/tools.scss'
'local-dev/styles/core/base.scss'
'local-dev/styles/templates/blog.scss'

I've thought about fetching the file as a string and using regex, but that wouldn't account for file extensions (.scss/.css/_partials.scss) and could lead to other issues.

Any suggestions or ideas are greatly appreciated.

Thanks in advance!

Answer №1

Incremental build systems are designed to compile only files that have been changed, without affecting anything else.

If you want to dive deeper into this concept, I highly recommend reading the article titled Incremental SASS Builds with Gulp by Sarthak Batra.


For a quick solution, follow these steps:

  1. Ensure you are using gulpJS v4 by checking your project directory with the console command: gulp -v

  2. Install the gulp-dependents plugin using the command: npm i --save-dev gulp-dependents in your project folder.

  3. Add the import statement for

    const dependents = require("gulp-dependents")
    in your gulpfile.js.

  4. Update your gulp styles function to only compile changed files.

function styles(callMeBack) {
  return gulp
    .src(paths.styles.src, { since: gulp.lastRun(styles) })
    .pipe(dependents())
    .pipe(scss())
    .pipe(gulp.dest(paths.styles.dest))
    .pipe(browserSync.stream())
    .on("end", callMeBack)
}
  • path.styles.src refers to the path of your styles folder;
  • since: gulp.lastRun(styles) checks if current files have been changed since the last run;
  • .dependents() performs a deep search to find files dependent on the current one in the pipeline;
  • The .scss() and gulp.dest functions are self-explanatory;
  • Only the styles that were edited will be compiled as a result;
  1. If you have multiple styles functions, consider creating a new one to avoid disrupting your workflow.

P.S Using since.lastRun ensures changes are detected correctly after the first run, following a process of compiling everything initially, waiting for changes, checking changes, and compiling only edited files.

P.P.S Feel free to ask any questions you may have!

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

Is there a way to use a specific keyboard input to alter the characteristics of shapes on my webpage?

Is there a way to change certain attributes of a shape onscreen when a specific key is pressed by the user? For example, can I make it so that pressing "a" changes the color of the shape? I attempted to modify a mouse rollover event to work with the desir ...

Tips for applying unique CSS classes to individual templates in Joomla

I manage a website at www.kadamjay.kg using an RT template system where each language version has its own unique template. However, when I add a class to a specific element, it only changes on one template and there are no additional templates in the pat ...

The terminal is unable to identify 'node' as a valid command, either internally or externally, in git bash. This could be due to it not being an operable

I encountered an issue where I received an error message stating 'node' is not recognized as an internal or external command, operable program or batch file when trying to run npm start from git bash CLI. $ npm start > [email protected] ...

Place an element in a higher position than a slideshow

I recently encountered an issue where I was trying to place a png image above my slideshow. Despite trying various solutions, the image always appeared behind or on top without transparency. Based on the erratic display, it seems like the problem might be ...

What is the correct method for installing webpack 4.19.1 on my system?

Currently, my system is equipped with webpack version 4.27.5, however I am in need of downgrading to an older version 4.19.1. This task needs to be performed on Ubuntu. I attempted to uninstall the current version by running the following command: npm uni ...

What is the best way to connect or link to a HTML table row <tr>

Is there a way to trigger an alert when the user double clicks on the whole row? ...

Scrollable Tailwind components

I am working on creating a page layout similar to this design: here is what I want to achieve The goal is to have a simple homepage with a navbar and split screen. The challenge is to prevent the entire page from scrolling normally and enable independent ...

The display: flex property is not being properly implemented in Tailwind CSS for the sm:flex

I have a div with the following styles <div class="hidden sm:visible sm:flex"> .... </div> The sm:visible style is applied like this @media (min-width: 640px) .sm\:visible { visibility: visible; } However, the property disp ...

Import data from a distant file into a node Buffer within the web browser

Currently, I am utilizing browserify to utilize this particular package within the browser. Specifically, I have developed a phonegap application that retrieves .fsc files from the server. The code snippet looks like this: var reader = new FileReader( ...

Is it possible to develop a Strapi plugin in real-time without the need to continuously rebuild the plugin?

Currently, I am in the process of developing a plugin for Strapi. I'm curious if there is a way to streamline the development process by having my Strapi backend automatically detect any changes made to the plugin's code without the need to run ...

What is the current importance of CSS3 vendor prefixes?

I've been pondering the importance of specifying vendor prefixes like 'webkit', 'moz', 'ms' or 'o' in modern CSS. It seems that Opera has switched to Webkit, meaning we may no longer need '-o-'. IE10 e ...

Comparing Express.js View Engine to Manual Compilation

Currently, I am utilizing Express.js along with the hbs library to incorporate Handlebars templates in my application. Lately, I've delved into establishing a build system using gulp for my app and stumbled upon packages like gulp-handlebars. My query ...

Issue with conflicting peer dependency: React Native for iOS

Struggling to build a React Native program on Mac by following the official website guidelines. Encountering errors during the process. Any advice on how to debug this? When trying "npx react-native init A2," prompted to install necessary packages but fa ...

Is it possible to direct the user to a particular link upon swiping to unlock?

Hello everyone, I could use some assistance with this code. I am looking to redirect someone to a specific page when they swipe to unlock and automatically transfer them to another page when the swipe ends. Any help is greatly appreciated. Thank you! $ ...

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help, I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjus ...

What are the steps to integrate and utilize DefinePlugin within your webpack configuration?

Hello, I'm currently trying to implement the DefinePlugin in order to update the version number and ensure that my JavaScript refreshes after a new build is released. Unfortunately, I am encountering issues with getting DefinePlugin to work properly. ...

Having trouble placing the flash element above the HTML?

Despite my efforts to use SWFObject to embed a swf file on my webpage, it seems that the flash movie is covering some of the HTML components. Strangely enough, the HTML elements are bleeding through and appearing on top of the flash content. I've tri ...

What is the best way to highlight and extract only the white-coded texts on VSCode to facilitate the process of translating webpages manually?

Currently, I'm engaged in a project where my task involves duplicating an entire website using HTTrack. However, I only need to copy the main page and web pages that are one link deep. Upon copying the website, my next challenge is to translate all of ...

The Bootstrap Jumbotron section stubbornly stays above the footer

I'm currently working on a Bootstrap 3 page and facing an issue where the yellow area does not seamlessly transition into the green footer. Instead, there is a white space between the end of the yellow area and the start of the footer. How can I resol ...