尝试在Compoundjs中定义资产编译器并获取未定义的方法错误

时间:2013-03-30 23:01:03

标签: javascript node.js compoundjs

我正在尝试定义资产编译器according to the documentation http://compoundjs.com/docs/#asset-compiler-adding-your-own-compiler,但是,我一直收到错误:

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

我的整个environment.js文件如下:

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);
    }); 

};

我在console.log(compound.assetsCompiler);声明之前尝试了app.use(compount.assetsCompiler...,确定我得到undefined。该文档还说使用compound.assetCompiler.add(...);(单数资产),我认为这是一个拼写错误,但由于多次错误输入错误,我也尝试了它,但遇到了同样的问题。有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。或者至少是解决方法。虽然自动加载器似乎需要AssetsCompiler它不会初始化它,但解决方案是要求对象并在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);
  });
};