gpt4 book ai didi

javascript - 尝试在Compoundjs中定义 Assets 编译器并收到未定义的方法错误

转载 作者:行者123 更新时间:2023-12-02 18:52:00 26 4
gpt4 key购买 nike

我正在尝试定义一个 Assets 编译器 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);
});

};

我在 app.use(compount.assetsCompiler... 语句之前尝试了 console.log(compound.assetsCompiler); ,果然我得到了 未定义。文档还说要使用compound.assetCompiler.add(...);(单一 Assets ),我认为这是一个拼写错误,但因为它多次出现该拼写错误我也尝试过,但遇到了同样的问题。有人知道如何解决这个问题吗?

最佳答案

我找到了解决办法。或者至少是解决方法。虽然自动加载器似乎需要 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);
});
};

关于javascript - 尝试在Compoundjs中定义 Assets 编译器并收到未定义的方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15724457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com