Skip to content

Commit

Permalink
adding css file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Oct 31, 2013
1 parent 326a9cf commit 16c1809
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
47 changes: 44 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
'use strict';

var resolveSassPaths = require('./lib/resolve-sass-paths');
var resolveSassPaths = require('./lib/resolve-sass-paths')
, scss = require('./lib/scss')
, os = require('os')
, path = require('path')
, fs = require('fs')
;

exports = module.exports = function (root, cb) {
var tmpdir = os.tmpDir();
var genImportsPath = path.join(tmpdir, 'sass-resolve-generated-imports.scss');

/**
* Resolves paths to all .scss files from the current package and its dependencies.
* The location of these sass files is indicated in the "sass" field inside packags.json.
* It then generates the css file including all the rules found in the resolved .scss files.
* Additionally it generates a .css.map file to enable sass source maps.
*
* NOTE: at this point sass 3.3 is still not officially released, but required to get sourcemaps.
*
* @name sassResolve
* @function
* @param root {String} path to the current package
* @param cssFile {String} path at which the resulting css file should be saved, the .css.map file is saved right next to it
* @param cb {Function} called back with an error or null when the css file was successfully generated.
*/
exports = module.exports = function (root, cssFile, cb) {
imports(root, function (err, src) {
if (err) return cb(err);
// the imports contain absolute paths, so it doesn't matter where the import file ends up
fs.writeFile(genImportsPath, src, 'utf8', function (err) {
if (err) return cb(err);
scss(genImportsPath, cssFile, cb);
});
});
}

/**
Expand All @@ -26,7 +55,7 @@ exports.paths = resolveSassPaths;
* @param root {String} full path to the project whose sass files to resolve
* @param cb {Function} called back with imports for the .scss files or an error if one occurred
*/
exports.imports = function (root, cb) {
var imports = exports.imports = function (root, cb) {
resolveSassPaths(root, function (err, scssFiles) {
if (err) return cb(err);
var imports = scssFiles.map(function (f) {
Expand All @@ -37,3 +66,15 @@ exports.imports = function (root, cb) {
});
}

// Test
if (!module.parent) {
var cssFile = path.join(__dirname, 'test', 'fixtures', 'imports.css');
exports(
path.join(__dirname, 'test', 'fixtures')
, cssFile
, function (err) {
if (err) return console.error(err);
var css = fs.readFileSync(cssFile, 'utf8');
console.log(css);
});
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16c1809

Please sign in to comment.