MDL-69117 theme_boost: themedesigner source maps
authorBas Brands <bas@moodle.com>
Tue, 23 Jun 2020 09:30:16 +0000 (11:30 +0200)
committerBas Brands <bas@moodle.com>
Wed, 2 Sep 2020 10:14:13 +0000 (12:14 +0200)
lib/outputlib.php

index f324c4b..a1d9abd 100644 (file)
@@ -1440,17 +1440,33 @@ class theme_config {
 
         // TODO: MDL-62757 When changing anything in this method please do not forget to check
         // if the validate() method in class admin_setting_configthemepreset needs updating too.
-        $cacheoptions = '';
+
+        $cachedir = make_localcache_directory('scsscache-' . $this->name, false);
+        $cacheoptions = [];
         if ($themedesigner) {
-            $scsscachedir = $CFG->localcachedir . '/scsscache/';
             $cacheoptions = array(
-                  'cacheDir' => $scsscachedir,
+                  'cacheDir' => $cachedir,
                   'prefix' => 'scssphp_',
                   'forceRefresh' => false,
             );
+        } else {
+            if (file_exists($cachedir)) {
+                remove_directory($cachedir);
+            }
         }
+
         // Set-up the compiler.
         $compiler = new core_scss($cacheoptions);
+
+        if ($this->supports_source_maps($themedesigner)) {
+            // Enable source maps.
+            $compiler->setSourceMapOptions([
+                'sourceMapBasepath' => str_replace('\\', '/', $CFG->dirroot),
+                'sourceMapRootpath' => $CFG->wwwroot . '/'
+            ]);
+            $compiler->setSourceMap($compiler::SOURCE_MAP_INLINE);
+        }
+
         $compiler->prepend_raw_scss($this->get_pre_scss_code());
         if (is_string($scss)) {
             $compiler->set_file($scss);
@@ -2192,6 +2208,19 @@ class theme_config {
         $this->rtlmode = $inrtl;
     }
 
+    /**
+     * Checks if source maps are supported
+     *
+     * @param bool $themedesigner True if theme designer is enabled.
+     * @return boolean True if source maps are supported.
+     */
+    public function supports_source_maps($themedesigner): bool {
+        if (empty($this->rtlmode) && $themedesigner) {
+            return true;
+        }
+        return false;
+    }
+
     /**
      * Whether the theme is being served in RTL mode.
      *