Koa系列CSS Preprocessor:koa-sass
2019-03-11
#koa
字数统计:5.2k 字
阅读时长 ≈ 5 分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| module.exports = ({ mountAt, src, dest, importPaths = [] }) => { mountAt = mountAt.substr(-1) === '/' ? mountAt.slice(0, -1) : mountAt; if (fs.existsSync(dest) === false) { fs.mkdirsSync(dest); }
return (ctx, next) => { const subPath = path.parse(ctx.path.slice(mountAt.length)); const srcFile = path.join(src, subPath.dir, subPath.name + '.scss'); if (fs.existsSync(srcFile) === false || subPath.ext !== '.css') { return next(); } const destFile = path.join(dest, subPath.dir, subPath.base); const result = sass.renderSync({ file: srcFile, importer: nodeSassGlobImporter(), includePaths: importPaths }); fs.writeFileSync(destFile, result.css); return mount(mountAt, serveStatic(dest))(ctx, next); }; }
|
首选是判断需要解析的文件,接着解析路径,然后得到源 .scss
文件路径,再接着重新得到目标文件路径,然后通过 node-sass
提供的renderSync执行 .scss
结果。然后写入,最后通过 koa-mount
和 koa-static
解析静态资源。