index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var path = require("path")
  2. var fs = require("fs")
  3. var helpers = require('../helpers')
  4. var postcss = require('postcss')
  5. var autoprefixer = require('autoprefixer')
  6. var minify = require('harp-minify')
  7. /**
  8. * Build Processor list for stylesheets.
  9. *
  10. * same as doing...
  11. *
  12. * var processors = {
  13. * "less" : require("./processors/less"),
  14. * "stylus" : require("./processors/stylus")
  15. * }
  16. *
  17. */
  18. var processors = {}
  19. helpers.processors["css"].forEach(function(sourceType){
  20. processors[sourceType] = require("./processors/" + sourceType)
  21. })
  22. module.exports = function(root, filePath, callback){
  23. var srcPath = path.resolve(root, filePath)
  24. var ext = path.extname(srcPath).replace(/^\./, '')
  25. fs.readFile(srcPath, function(err, data){
  26. /**
  27. * File not Found
  28. */
  29. if(err && err.code === 'ENOENT') return callback(null, null)
  30. /**
  31. * Read File Error
  32. */
  33. if(err) return callback(err)
  34. /**
  35. * Lookup Directories
  36. */
  37. var dirs = [
  38. path.dirname(srcPath),
  39. path.dirname(path.resolve(root))
  40. ]
  41. /**
  42. * Lookup Directories
  43. */
  44. var render = processors[ext].compile(srcPath, dirs, data, function(err, css, sourcemap) {
  45. if (err) return callback(err);
  46. /**
  47. * Autoprefix, then consistently minify
  48. */
  49. postcss([autoprefixer]).process(css, {map: {
  50. inline: false,
  51. prev: sourcemap,
  52. annotation: false
  53. }
  54. }).then(function (result) {
  55. result.warnings().forEach(function (warn) {
  56. console.warn(warn.toString())
  57. })
  58. callback(null, minify.css(result.css), result.map)
  59. })
  60. })
  61. })
  62. }