Question

I'm trying to define an asset compiler according to the documentationhttp://compoundjs.com/docs/#asset-compiler-adding-your-own-compiler, however, I keep getting an error:

app.use(compound.assetsCompiler.add('scss', {
                                ^
TypeError: Cannot call method 'add' of undefined

My entire environment.js file looks like:

module.exports = function (compound) {

    var express = require('express');
    var app = compound.app;

    app.configure(function (){
      app.use(compound.assetsCompiler.add('scss', {
        render: function (str, options, fn) {
          try {
            fn(null, @scss.render(str));
          } catch (err) {
            fn(err);
          }   
        },  
        scss: require('node-sass'),
        sourceExtension: 'scss',
        destExtension: 'css'
      }).init());

      app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
      app.set('jsDirectory', '/javascripts/');
      app.set('cssDirectory', '/stylesheets/');
      app.set('cssEngine', 'scss');
      app.use(express.bodyParser());
      app.use(express.cookieParser('secret'));
      app.use(express.session({secret: 'secret'}));
      app.use(express.methodOverride());
      app.use(app.router);
    }); 

};

I tried a console.log(compound.assetsCompiler); just before the app.use(compount.assetsCompiler... statement and sure enough I get undefined. The documentation also says to use compound.assetCompiler.add(...); (singular asset) which I assume is a typo but since it got that typo wrong multiple times I tried it as well but had the same problem. Does anyone have any idea how to fix this?

Was it helpful?

Solution

I figured out the solution. Or at least the workaround. Although it seems that the autoloader require the AssetsCompiler it doesn't initialize it, the solution is to require the object and manually initialize it in the environment.js

module.exports = function (compound) {
  'use strict';
  var express = require('express');
  var app = compound.app;
  // require the assetsCompiler
  var compiler = require('co-assets-compiler');

  app.configure(function () {
    // calling the init function will add the assetsCompiler object
    // to the compound object and everything else should now work
    // as expected
    compiler.init(compound);
    app.use(compound.assetsCompiler.add('scss', {
     render: function (str, options, fn) {
         @scss.render(str, ƒ (err, css) {
          fn(err,css);         
        },{                    
          includePaths: [app.root + '/app/assets/stylesheets/'] 
      });
    },
      scss: require('node-sass'),
      sourceExtension: 'scss', 
      destExtension: 'css'     
    }).init());  

    app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
    app.set('jsDirectory', '/javascripts/');
    app.set('cssDirectory', '/stylesheets/');
    app.set('cssEngine', 'scss');
    app.use(express.bodyParser());
    app.use(express.cookieParser('secret'));
    app.use(express.session({secret: 'secret'}));
    app.use(express.methodOverride());
    app.use(app.router);
  });
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top