【发布时间】:2023-04-03 09:34:01
【问题描述】:
我正在尝试使用 koa2 获取 node-sass-middleware。有一个模块koa-sass 可以完美运行,但它正在使用生成器。
module.exports = function (options) {
var mw = require('node-sass-middleware')(options);
return function *(next) {
yield mw.bind(mw, this.req, this.res);
yield next;
};
};
不推荐使用 koa 对生成器的支持将在 v3 中删除。有关如何转换旧中间件的示例,请参阅文档https://github.com/koajs/koa/blob/master/docs/migration.md
所以我想使用 async/await 进行转换。
这是我的代码:
module.exports = (options) => {
const sass = require('node-sass-middleware')(options);;
const middleware = async (ctx, next) => {
await sass.bind(sass, ctx.req, ctx.res);
await next();
}
return middleware;
};
它确实会返回一个错误,但它也不会编译。
app.js
app.use(sass({
src: path.join(__dirname + '/scss'),
dest: path.join(__dirname + '/public/stylesheets'),
outputStyle: 'compressed',
indentedSyntax: false
}))
app.use(require('koa-static')(__dirname, 'public'))
文件夹结构
public
|--scss
| |--style.scss
|--public
| |--stylesheets
| | |--style.css
app.js
【问题讨论】: