From cce7d977e7917c6c0b66dd8ee9f459bf577d26e5 Mon Sep 17 00:00:00 2001
From: nod_ <nod_@598310.no-reply.drupal.org>
Date: Tue, 11 Feb 2025 23:48:40 +0900
Subject: [PATCH] Issue #3504265 by finnsky, ksenzee, smustgrave: Yarn watch
 task broken

---
 core/scripts/css/postcss-watch.js | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/core/scripts/css/postcss-watch.js b/core/scripts/css/postcss-watch.js
index 472c4a944b44..43e2138ce434 100644
--- a/core/scripts/css/postcss-watch.js
+++ b/core/scripts/css/postcss-watch.js
@@ -9,19 +9,18 @@
 
 'use strict';
 
-const fs = require('node:fs');
-const path = require('node:path');
-const chokidar = require('chokidar');
+const { watch } = require('chokidar');
+const { stat, unlink } = require('node:fs');
 
 const changeOrAdded = require('./changeOrAdded');
 const log = require('./log');
 
-// Match only on .pcss.css files.
-const fileMatch = './**/*.pcss.css';
-// Ignore everything in node_modules
-const watcher = chokidar.watch(fileMatch, {
+// Initialize watcher.
+const watcher = watch(['./themes', './modules', './profiles'], {
   ignoreInitial: true,
-  ignored: './node_modules/**'
+  ignored: (filePath, stats) =>
+    stats?.isFile() && !filePath.endsWith('.pcss.css') || filePath.includes('node_modules'),
+  usePolling: true,
 });
 
 const unlinkHandler = (err) => {
@@ -36,8 +35,10 @@ watcher
   .on('change', changeOrAdded)
   .on('unlink', (filePath) => {
     const fileName = filePath.slice(0, -9);
-    fs.stat(`${fileName}.css`, () => {
-      fs.unlink(`${fileName}.css`, unlinkHandler);
+    stat(`${fileName}.css`, (err) => {
+      if (!err) {
+        unlink(`${fileName}.css`, unlinkHandler);
+      }
     });
   })
-  .on('ready', () => log(`Watching '${fileMatch}' for changes.`));
+  .on('ready', () => log(`Watching '**/*.pcss.css' for changes.`));
-- 
GitLab