Implement caching of less compiled css, including full dependency tracking
authorNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:21:40 +0000 (15:21 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:21:40 +0000 (15:21 +1100)
ndserver.js

index a6ced88..b9fea19 100755 (executable)
@@ -218,7 +218,7 @@ var app = async function(req, res, protocol) {
           ) + str
           var value = await build_cache_js.get(key)
           if (value === undefined) {
-            console.log(site, 'js compile', key)
+            console.log(site, 'compiling', key)
             value = await jstemplate(key)
             build_cache_js.set(key, value)
           }
@@ -246,35 +246,37 @@ var app = async function(req, res, protocol) {
     case 'css':
       temp = site_path + '.less'
       try {
-        data = new Buffer(
-          (
-            await less.render(
-              await readFileAsync(site_root + temp, {encoding: 'utf-8'}),
-              {
-                color: true,
-                compress: false,
-                depends: false,
-                filename: temp,
-                globalVars: null,
-                ieCompat: false,
-                insecure: false,
-                javascriptEnabled: false,
-                lint: false,
-                math: 0,
-                modifyVars: null,
-                paths: [site_root + dir_name],
-                plugins: [],
-                reUsePluginManager: true,
-                rewriteUrls: false,
-                rootpath: '',
-                strictImports: false,
-                strictUnits: false,
-                urlArgs: ''
-              }
-            )
-          ).css,
-          'utf-8'
-        )
+        var key = site_root + temp
+        var data = await build_cache_less.get(key)
+        if (data === undefined) {
+          console.log(site, 'compiling', key)
+          var result = await less.render(
+            await readFileAsync(site_root + temp, {encoding: 'utf-8'}),
+            {
+              //color: true,
+              //compress: false,
+              //depends: false,
+              filename: temp,
+              //globalVars: null,
+              //ieCompat: false,
+              //insecure: false,
+              //javascriptEnabled: false,
+              //lint: false,
+              //math: 0,
+              //modifyVars: null,
+              paths: [site_root + dir_name],
+              //plugins: [],
+              //reUsePluginManager: true,
+              //rewriteUrls: false,
+              rootpath: site_root//,
+              //strictImports: false,
+              //strictUnits: false,
+              //urlArgs: ''
+            }
+          )
+          data = new Buffer(result.css, 'utf-8')
+          build_cache_less.set(key, data, result.imports)
+        }
         console.log(
           site,
           'serving',