Commit a5007aea by Dean Mao

merge in sourceMap support

parent 19ea3a09
...@@ -14,6 +14,9 @@ var optimist = require('optimist') ...@@ -14,6 +14,9 @@ var optimist = require('optimist')
describe: 'Include debug info in output (none|normal|map)', describe: 'Include debug info in output (none|normal|map)',
'default': 'none' 'default': 'none'
}) })
.options('source-map', {
describe: 'Emit source map'
})
.options('include-path', { .options('include-path', {
describe: 'Path to look for @import-ed files', describe: 'Path to look for @import-ed files',
'default': cwd 'default': cwd
...@@ -99,6 +102,16 @@ exports = module.exports = function(args) { ...@@ -99,6 +102,16 @@ exports = module.exports = function(args) {
options.sourceComments = options.sourceComments[0]; options.sourceComments = options.sourceComments[0];
} }
// set source map file and set sourceComments to 'map'
if (argv['source-map']) {
options.sourceComments = 'map';
if (argv['source-map'] === true) {
options.sourceMap = outFile + '.map';
} else {
options.sourceMap = path.resolve(cwd, argv['source-map']);
}
}
if (argv.w) { if (argv.w) {
var watchDir = argv.w; var watchDir = argv.w;
......
...@@ -9,7 +9,15 @@ function render(options, emitter) { ...@@ -9,7 +9,15 @@ function render(options, emitter) {
includePaths: options.includePaths, includePaths: options.includePaths,
outputStyle: options.outputStyle, outputStyle: options.outputStyle,
sourceComments: options.sourceComments, sourceComments: options.sourceComments,
success: function(css) { sourceMap: options.sourceMap,
success: function(css, sourceMap) {
var todo = 1;
var done = function() {
if (--todo <= 0) {
emitter.emit('done');
}
};
emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...')); emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...'));
...@@ -17,8 +25,19 @@ function render(options, emitter) { ...@@ -17,8 +25,19 @@ function render(options, emitter) {
if (err) { return emitter.emit('error', chalk.red('Error: ' + err)); } if (err) { return emitter.emit('error', chalk.red('Error: ' + err)); }
emitter.emit('warn', chalk.green('Wrote CSS to ' + options.outFile)); emitter.emit('warn', chalk.green('Wrote CSS to ' + options.outFile));
emitter.emit('write', err, options.outFile, css); emitter.emit('write', err, options.outFile, css);
done();
}); });
if (options.sourceMap) {
todo++;
fs.writeFile(options.sourceMap, sourceMap, function(err) {
if (err) {return emitter.emit('error', chalk.red('Error' + err)); }
emitter.emit('warn', chalk.green('Wrote Source Map to ' + options.sourceMap));
emitter.emit('write-source-map', err, options.sourceMap, sourceMap);
done();
});
}
if (options.stdout) { if (options.stdout) {
emitter.emit('log', css); emitter.emit('log', css);
} }
......
var binding; var binding;
var fs = require('fs'); var fs = require('fs');
var path = require('path');
var v8 = 'v8-' + /[0-9]+\.[0-9]+/.exec(process.versions.v8)[0];
var modPath = path.join(__dirname, 'bin', process.platform + '-' + process.arch + '-' + v8, 'binding');
try { try {
if (fs.realpathSync(__dirname + '/build')) { if (fs.realpathSync(__dirname + '/build')) {
// use the build version if it exists // use the build version if it exists
binding = require(__dirname + '/build/Release/binding'); binding = require(__dirname + '/build/Release/binding');
} }
} catch (e) { } catch (e) {
try { // default to a precompiled binary if no build exists
fs.realpathSync(modPath + '.node'); var platform_full = process.platform+'-'+process.arch;
binding = require(modPath); binding = require(__dirname + '/precompiled/'+platform_full+'/binding');
} catch (ex) { }
// No binary! if (binding === null) {
throw new Error('`'+ modPath + '.node` is missing. Try reinstalling `node-sass`?'); throw new Error('Cannot find appropriate binary library for node-sass');
}
} }
var SASS_OUTPUT_STYLE = { var SASS_OUTPUT_STYLE = {
...@@ -34,11 +29,11 @@ var SASS_SOURCE_COMMENTS = { ...@@ -34,11 +29,11 @@ var SASS_SOURCE_COMMENTS = {
}; };
var prepareOptions = function(options) { var prepareOptions = function(options) {
var paths, style; var paths, style, comments;
var options = typeof options !== 'object' ? {} : options; options = typeof options !== 'object' ? {} : options;
var paths = options.include_paths || options.includePaths || []; paths = options.include_paths || options.includePaths || [];
var style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0; style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
var comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0; comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0;
return { return {
paths: paths, paths: paths,
...@@ -49,7 +44,13 @@ var prepareOptions = function(options) { ...@@ -49,7 +44,13 @@ var prepareOptions = function(options) {
var deprecatedRender = function(css, callback, options) { var deprecatedRender = function(css, callback, options) {
options = prepareOptions(options); options = prepareOptions(options);
return binding.oldRender(css, callback, options.paths.join(':'), options.style, options.comments); var errCallback = function(err) {
callback(err);
};
var oldCallback = function(css, err) {
callback(null, css);
};
return binding.render(css, oldCallback, errCallback, options.paths.join(':'), options.style, options.comments);
}; };
var deprecatedRenderSync = function(css, options) { var deprecatedRenderSync = function(css, options) {
...@@ -68,11 +69,11 @@ exports.render = function(options) { ...@@ -68,11 +69,11 @@ exports.render = function(options) {
options.error = options.error || function(){}; options.error = options.error || function(){};
if (options.file !== undefined && options.file !== null) { if (options.file !== undefined && options.file !== null) {
return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments); return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(':'), newOptions.style, newOptions.comments, options.sourceMap);
} }
//Assume data is present if file is not. binding/libsass will tell the user otherwise! //Assume data is present if file is not. binding/libsass will tell the user otherwise!
return binding.render(options.data, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style); return binding.render(options.data, options.success, options.error, newOptions.paths.join(":"), newOptions.style);
}; };
exports.renderSync = function(options) { exports.renderSync = function(options) {
...@@ -85,11 +86,11 @@ exports.renderSync = function(options) { ...@@ -85,11 +86,11 @@ exports.renderSync = function(options) {
newOptions = prepareOptions(options); newOptions = prepareOptions(options);
if (options.file !== undefined && options.file !== null) { if (options.file !== undefined && options.file !== null) {
return binding.renderFileSync(options.file, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments); return binding.renderFileSync(options.file, newOptions.paths.join(':'), newOptions.style, newOptions.comments);
} }
//Assume data is present if file is not. binding/libsass will tell the user otherwise! //Assume data is present if file is not. binding/libsass will tell the user otherwise!
return binding.renderSync(options.data, newOptions.paths.join(path.delimiter), newOptions.style); return binding.renderSync(options.data, newOptions.paths.join(":"), newOptions.style);
}; };
exports.middleware = require('./lib/middleware'); exports.middleware = require('./lib/middleware');
...@@ -12,21 +12,7 @@ extern "C" { ...@@ -12,21 +12,7 @@ extern "C" {
void sass_free_context_wrapper(sass_context_wrapper* ctx_w) void sass_free_context_wrapper(sass_context_wrapper* ctx_w)
{ {
if (ctx_w->ctx) sass_free_context(ctx_w->ctx); if (ctx_w->ctx) sass_free_context(ctx_w->ctx);
if (ctx_w->fctx) sass_free_file_context(ctx_w->fctx);
delete ctx_w->callback;
delete ctx_w->errorCallback;
free(ctx_w);
}
sass_file_context_wrapper* sass_new_file_context_wrapper()
{
return (sass_file_context_wrapper*) calloc(1, sizeof(sass_file_context_wrapper));
}
void sass_free_file_context_wrapper(sass_file_context_wrapper* ctx_w)
{
if (ctx_w->ctx) sass_free_file_context(ctx_w->ctx);
delete ctx_w->callback; delete ctx_w->callback;
delete ctx_w->errorCallback; delete ctx_w->errorCallback;
......
...@@ -7,6 +7,7 @@ extern "C" { ...@@ -7,6 +7,7 @@ extern "C" {
struct sass_context_wrapper { struct sass_context_wrapper {
sass_context* ctx; sass_context* ctx;
sass_file_context* fctx;
uv_work_t request; uv_work_t request;
NanCallback* callback; NanCallback* callback;
NanCallback* errorCallback; NanCallback* errorCallback;
...@@ -15,16 +16,6 @@ struct sass_context_wrapper { ...@@ -15,16 +16,6 @@ struct sass_context_wrapper {
struct sass_context_wrapper* sass_new_context_wrapper(void); struct sass_context_wrapper* sass_new_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx); void sass_free_context_wrapper(struct sass_context_wrapper* ctx);
struct sass_file_context_wrapper {
sass_file_context* ctx;
uv_work_t request;
NanCallback* callback;
NanCallback* errorCallback;
};
struct sass_file_context_wrapper* sass_new_file_context_wrapper(void);
void sass_free_file_context_wrapper(struct sass_file_context_wrapper* ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
...@@ -103,4 +103,40 @@ describe('cli', function() { ...@@ -103,4 +103,40 @@ describe('cli', function() {
}); });
}); });
it('should compile with the --source-map option', function(done){
var emitter = cli([path.join(__dirname, 'sample.scss'), '--source-map']);
emitter.on('error', done);
emitter.on('write-source-map', function(err, file) {
assert.equal(file, path.join(__dirname, '../sample.css.map'));
fs.exists(file, function(exists) {
assert(exists);
});
});
emitter.on('done', function() {
fs.unlink(path.join(__dirname, '../sample.css.map'), function() {
fs.unlink(path.join(__dirname, '../sample.css'), function() {
done();
});
});
});
});
it('should compile with the --source-map option with specific filename', function(done){
var emitter = cli([path.join(__dirname, 'sample.scss'), '--source-map', path.join(__dirname, '../sample.map')]);
emitter.on('error', done);
emitter.on('write-source-map', function(err, file) {
assert.equal(file, path.join(__dirname, '../sample.map'));
fs.exists(file, function(exists) {
assert(exists);
});
});
emitter.on('done', function() {
fs.unlink(path.join(__dirname, '../sample.map'), function() {
fs.unlink(path.join(__dirname, '../sample.css'), function() {
done();
});
});
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment