Commit a0eb0796 by Nick Schonning

Revert "major refactor"

This reverts commit b577f579.
parent 95b7ca63
......@@ -52,7 +52,7 @@ The API for using node-sass has changed, so that now there is only one variable
[Important: currently the argument `outputStyle` has some problem which may cause the output css becomes nothing because of the libsass, so you should not use it now!]
#### sourceComments
`sourceComments` is a `String` to determine what debug information is included in the output file. Its value should be one of `'none', 'normal', 'map'`. The default is `'none'`.
`sourceComments` is a `String` to determine what debug information is included in the output file. Its value should be one of `'none', 'normal'`. The default is `'none'`.
[Important: `souceComments` is only supported when using the `file` option, and does nothing when using `data` flag.]
### Examples
......@@ -92,13 +92,18 @@ var server = connect.createServer(
, dest: __dirname + '/public'
, debug: true
, outputStyle: 'compressed'
, prefix: '/prefix'
}),
connect.static(__dirname + '/public')
connect.static('/prefix', __dirname + '/public')
);
```
Heavily inspired by <https://github.com/LearnBoost/stylus>
## DocPad Plugin
[@jking90](https://github.com/jking90) wrote a [DocPad](http://docpad.org/) plugin that compiles `.scss` files using node-sass: <https://github.com/jking90/docpad-plugin-nodesass>
## Grunt extension
[@sindresorhus](https://github.com/sindresorhus/) has created a set of grunt tasks based on node-sass: <https://github.com/sindresorhus/grunt-sass>
......@@ -115,10 +120,10 @@ Check out the project:
git clone https://github.com/andrew/node-sass.git
cd node-sass
npm install
npm install -g node-gyp
git submodule init
git submodule update
npm install
npm install -g node-gyp
node-gyp rebuild
Replace the prebuild binary with your newly generated one
......@@ -141,6 +146,14 @@ Output will be saved with the same name as input SASS file into the current work
--include-path Path to look for @import-ed files [default: cwd]
--help, -h Print usage info
## Post-install Build
Install runs a series of Mocha tests to see if your machine can use the pre-built `libsass` which will save some time during install. If any tests fail it will build from source.
If you know the pre-built version will work and do not want to wait for the tests to run you can skip the tests by setting the environment variable `SKIP_NODE_SASS_TESTS` to true.
SKIP_NODE_SASS_TESTS=true npm install
## Contributors
Special thanks to the following people for submitting patches:
......@@ -148,6 +161,7 @@ Dean Mao
Brett Wilkins
litek
gonghao
Dylan Greene
### Note on Patches/Pull Requests
......
#include <v8.h>
#include <node.h>
#include <nan.h>
#include <string>
#include <cstring>
#include <iostream>
......@@ -12,190 +11,279 @@ using namespace std;
void WorkOnContext(uv_work_t* req) {
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
if (ctx_w->ctx) {
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
sass_compile(ctx);
} else if (ctx_w->fctx) {
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->fctx);
sass_compile_file(ctx);
}
}
void extractOptions(const Arguments& args, void* cptr, sass_context_wrapper* ctx_w, bool isFile) {
char *source;
char* pathOrData;
int output_style;
int source_comments;
String::AsciiValue astr(args[0]);
void MakeOldCallback(uv_work_t* req) {
NanScope();
TryCatch try_catch;
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
if (ctx_w) {
// async (callback) style
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
if (isFile) {
ctx_w->fctx = (sass_file_context*) cptr;
ctx_w->callback = Persistent<Function>::New(callback);
ctx_w->errorCallback = Persistent<Function>::New(errorCallback);
ctx_w->request.data = ctx_w;
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 2;
Local<Value> argv[argc] = {
NanNewLocal(Null()),
NanNewLocal(String::New(ctx->output_string))
};
ctx_w->callback->Call(argc, argv);
} else {
ctx_w->ctx = (sass_context*) cptr;
ctx_w->callback = Persistent<Function>::New(callback);
ctx_w->errorCallback = Persistent<Function>::New(errorCallback);
ctx_w->request.data = ctx_w;
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
};
ctx_w->callback->Call(argc, argv);
}
output_style = args[4]->Int32Value();
source_comments = args[5]->Int32Value();
String::AsciiValue bstr(args[3]);
pathOrData = new char[strlen(*bstr)+1];
strcpy(pathOrData, *bstr);
} else {
// synchronous style
output_style = args[2]->Int32Value();
source_comments = args[3]->Int32Value();
String::AsciiValue bstr(args[1]);
pathOrData = new char[strlen(*bstr)+1];
strcpy(pathOrData, *bstr);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete ctx->source_string;
sass_free_context_wrapper(ctx_w);
}
NAN_METHOD(OldRender) {
NanScope();
sass_context* ctx = sass_new_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
char *source;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
String::AsciiValue bstr(args[2]);
if (isFile) {
sass_file_context *ctx = (sass_file_context*)cptr;
char *filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.image_path = new char[0];
ctx->options.output_style = output_style;
ctx->options.source_comments = source_comments;
ctx->options.include_paths = pathOrData;
} else {
sass_context *ctx = (sass_context*)cptr;
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.image_path = new char[0];
ctx->options.output_style = output_style;
ctx->options.source_comments = source_comments;
ctx->options.include_paths = pathOrData;
}
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.output_style = args[3]->Int32Value();
ctx->options.source_comments = args[4]->Int32Value();
ctx_w->ctx = ctx;
ctx_w->callback = new NanCallback(callback);
ctx_w->request.data = ctx_w;
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeOldCallback);
assert(status == 0);
NanReturnUndefined();
}
void MakeCallback(uv_work_t* req) {
HandleScope scope;
NanScope();
TryCatch try_catch;
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
sass_file_context* fctx = static_cast<sass_file_context*>(ctx_w->fctx);
if ((ctx && ctx->error_status == 0) || (fctx && fctx->error_status == 0)) {
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 1;
Local<Value> val;
if (ctx) {
val = Local<Value>::New(String::New(ctx->output_string));
} else {
val = Local<Value>::New(String::New(fctx->output_string));
}
Local<Value> argv[argc] = {val};
ctx_w->callback->Call(Context::GetCurrent()->Global(), argc, argv);
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->output_string))
};
ctx_w->callback->Call(argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> err;
if (ctx) {
err = Local<Value>::New(String::New(ctx->error_message));
} else {
err = Local<Value>::New(String::New(fctx->error_message));
}
Local<Value> argv[argc] = {err};
ctx_w->errorCallback->Call(Context::GetCurrent()->Global(), argc, argv);
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
};
ctx_w->errorCallback->Call(argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
if (fctx) {
delete fctx->input_path;
} else if (ctx) {
delete ctx->source_string;
}
sass_free_context_wrapper(ctx_w);
}
Handle<Value> Render(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Render) {
NanScope();
sass_context* ctx = sass_new_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
char *source;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
String::AsciiValue bstr(args[3]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.image_path = new char[0];
ctx->options.output_style = args[4]->Int32Value();
ctx->options.source_comments = args[5]->Int32Value();
ctx_w->ctx = ctx;
extractOptions(args, ctx, ctx_w, false);
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
ctx_w->request.data = ctx_w;
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
return scope.Close(Undefined());
NanReturnUndefined();
}
Handle<Value> RenderSync(const Arguments& args) {
HandleScope scope;
NAN_METHOD(RenderSync) {
NanScope();
sass_context* ctx = sass_new_context();
extractOptions(args, ctx, NULL, false);
char *source;
String::AsciiValue astr(args[0]);
String::AsciiValue bstr(args[1]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
ctx->options.output_style = args[2]->Int32Value();
ctx->options.image_path = new char[0];
ctx->options.source_comments = args[3]->Int32Value();
sass_compile(ctx);
source = NULL;
delete ctx->source_string;
ctx->source_string = NULL;
delete ctx->options.include_paths;
ctx->options.include_paths = NULL;
delete ctx->options.image_path;
ctx->options.image_path = NULL;
if (ctx->error_status == 0) {
Local<Value> output = Local<Value>::New(String::New(ctx->output_string));
Local<Value> output = NanNewLocal(String::New(ctx->output_string));
sass_free_context(ctx);
return scope.Close(output);
NanReturnValue(output);
}
Local<String> error = String::New(ctx->error_message);
sass_free_context(ctx);
ThrowException(Exception::Error(error));
return scope.Close(Undefined());
NanThrowError(error);
NanReturnUndefined();
}
Handle<Value> RenderFile(const Arguments& args) {
HandleScope scope;
sass_file_context* fctx = sass_new_file_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
ctx_w->fctx = fctx;
extractOptions(args, fctx, ctx_w, true);
/**
Rendering Files
**/
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
void WorkOnFileContext(uv_work_t* req) {
sass_file_context_wrapper* ctx_w = static_cast<sass_file_context_wrapper*>(req->data);
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->ctx);
sass_compile_file(ctx);
}
void MakeFileCallback(uv_work_t* req) {
NanScope();
TryCatch try_catch;
sass_file_context_wrapper* ctx_w = static_cast<sass_file_context_wrapper*>(req->data);
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->ctx);
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 1;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->output_string))
};
ctx_w->callback->Call(argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
};
ctx_w->errorCallback->Call(argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete ctx->input_path;
sass_free_file_context_wrapper(ctx_w);
}
NAN_METHOD(RenderFile) {
NanScope();
sass_file_context* ctx = sass_new_file_context();
sass_file_context_wrapper* ctx_w = sass_new_file_context_wrapper();
char *filename;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
String::AsciiValue bstr(args[3]);
filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.output_style = args[4]->Int32Value();
ctx->options.image_path = new char[0];
ctx->options.source_comments = args[5]->Int32Value();
ctx_w->ctx = ctx;
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
ctx_w->request.data = ctx_w;
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnFileContext, (uv_after_work_cb)MakeFileCallback);
assert(status == 0);
return scope.Close(Undefined());
NanReturnUndefined();
}
Handle<Value> RenderFileSync(const Arguments& args) {
HandleScope scope;
NAN_METHOD(RenderFileSync) {
NanScope();
sass_file_context* ctx = sass_new_file_context();
extractOptions(args, ctx, NULL, true);
char *filename;
String::AsciiValue astr(args[0]);
String::AsciiValue bstr(args[1]);
filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
ctx->options.image_path = new char[0];
ctx->options.output_style = args[2]->Int32Value();
ctx->options.source_comments = args[3]->Int32Value();
sass_compile_file(ctx);
filename = NULL;
delete ctx->input_path;
ctx->input_path = NULL;
delete ctx->options.include_paths;
ctx->options.include_paths = NULL;
delete ctx->options.image_path;
ctx->options.image_path = NULL;
if (ctx->error_status == 0) {
Local<Value> output = Local<Value>::New(String::New(ctx->output_string));
Local<Value> output = NanNewLocal(String::New(ctx->output_string));
sass_free_file_context(ctx);
return scope.Close(output);
NanReturnValue(output);
}
Local<String> error = String::New(ctx->error_message);
sass_free_file_context(ctx);
ThrowException(Exception::Error(error));
return scope.Close(Undefined());
NanThrowError(error);
NanReturnUndefined();
}
void RegisterModule(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "oldRender", OldRender);
NODE_SET_METHOD(target, "render", Render);
NODE_SET_METHOD(target, "renderSync", RenderSync);
NODE_SET_METHOD(target, "renderFile", RenderFile);
......
......@@ -6,6 +6,7 @@
'binding.cpp',
'sass_context_wrapper.cpp',
'libsass/ast.cpp',
'libsass/base64vlq.cpp',
'libsass/bind.cpp',
'libsass/constants.cpp',
'libsass/context.cpp',
......@@ -24,13 +25,17 @@
'libsass/prelexer.cpp',
'libsass/sass.cpp',
'libsass/sass_interface.cpp',
'libsass/source_map.cpp',
'libsass/to_c.cpp',
'libsass/to_string.cpp',
'libsass/source_map.cpp',
'libsass/units.cpp'
],
'include_dirs': [
'<!(node -e "require(\'nan\')")'
],
'cflags!' : [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'cflags_cc' : [ '-fexceptions', '-frtti' ],
'conditions': [
['OS=="mac"', {
'xcode_settings': {
......@@ -38,12 +43,6 @@
'GCC_ENABLE_CPP_RTTI': 'YES',
'MACOSX_DEPLOYMENT_TARGET': '10.7'
}
}],
['OS=="linux"', {
'cflags_cc': [
'-fexceptions',
'-frtti'
]
}]
]
}
......
......@@ -2,7 +2,7 @@
"author": "Andrew Nesbitt <andrewnez@gmail.com> (http://andrew.github.com)",
"name": "node-sass",
"description": "wrapper around libsass",
"version": "0.5.4",
"version": "0.7.0",
"homepage": "https://github.com/andrew/node-sass",
"keywords": [
"sass",
......@@ -24,8 +24,9 @@
"url": "git://github.com/andrew/node-sass.git"
},
"scripts": {
"install": "node rebuild.js",
"test": "mocha test"
"install": "node build.js",
"test": "mocha test",
"prepublish": "bash scripts/prepublish.sh"
},
"bin": {
"node-sass": "bin/node-sass"
......@@ -36,10 +37,10 @@
},
"dependencies": {
"mkdirp": "0.3.x",
"colors": "0.6.0-1",
"optimist": "0.4.x"
},
"devDependencies": {
"mocha": "1.7.x"
"optimist": "0.6.x",
"node-watch": "0.3.x",
"mocha": "1.13.x",
"chalk": "~0.3.0",
"nan": "~0.6.0"
}
}
var binding;
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 {
if (fs.realpathSync(__dirname + '/build')) {
// use the build version if it exists
binding = require(__dirname + '/build/Release/binding');
}
} catch (e) {
// default to a precompiled binary if no build exists
var platform_full = process.platform+'-'+process.arch;
binding = require(__dirname + '/precompiled/'+platform_full+'/binding');
}
if (binding === null) {
throw new Error('Cannot find appropriate binary library for node-sass');
try {
fs.realpathSync(modPath + '.node');
binding = require(modPath);
} catch (ex) {
// No binary!
throw new Error('`'+ modPath + '.node` is missing. Try reinstalling `node-sass`?');
}
}
var SASS_OUTPUT_STYLE = {
......@@ -29,11 +34,11 @@ var SASS_SOURCE_COMMENTS = {
};
var prepareOptions = function(options) {
var paths, style, comments;
options = typeof options !== 'object' ? {} : options;
paths = options.include_paths || options.includePaths || [];
style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0;
var paths, style;
var options = typeof options !== 'object' ? {} : options;
var paths = options.include_paths || options.includePaths || [];
var style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
var comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0;
return {
paths: paths,
......@@ -44,13 +49,7 @@ var prepareOptions = function(options) {
var deprecatedRender = function(css, callback, options) {
options = prepareOptions(options);
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);
return binding.oldRender(css, callback, options.paths.join(':'), options.style, options.comments);
};
var deprecatedRenderSync = function(css, options) {
......@@ -69,11 +68,11 @@ exports.render = function(options) {
options.error = options.error || function(){};
if (options.file !== undefined && options.file !== null) {
return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(':'), newOptions.style, newOptions.comments);
return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments);
}
//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(":"), newOptions.style);
return binding.render(options.data, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style);
};
exports.renderSync = function(options) {
......@@ -86,11 +85,11 @@ exports.renderSync = function(options) {
newOptions = prepareOptions(options);
if (options.file !== undefined && options.file !== null) {
return binding.renderFileSync(options.file, newOptions.paths.join(':'), newOptions.style, newOptions.comments);
return binding.renderFileSync(options.file, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments);
}
//Assume data is present if file is not. binding/libsass will tell the user otherwise!
return binding.renderSync(options.data, newOptions.paths.join(":"), newOptions.style);
return binding.renderSync(options.data, newOptions.paths.join(path.delimiter), newOptions.style);
};
exports.middleware = require('./lib/middleware');
......@@ -12,7 +12,24 @@ extern "C" {
void sass_free_context_wrapper(sass_context_wrapper* ctx_w)
{
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->errorCallback;
free(ctx_w);
}
......
#include "libsass/sass_interface.h"
#include <node.h>
#include <nan.h>
#ifdef __cplusplus
extern "C" {
......@@ -7,15 +7,24 @@ extern "C" {
struct sass_context_wrapper {
sass_context* ctx;
sass_file_context* fctx;
uv_work_t request;
v8::Persistent<v8::Function> callback;
v8::Persistent<v8::Function> errorCallback;
NanCallback* callback;
NanCallback* errorCallback;
};
struct sass_context_wrapper* sass_new_context_wrapper(void);
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
}
#endif
......@@ -10,20 +10,7 @@ var scssStr = '#navbar {\
a {\
font-weight: bold; }}';
//sass.render(scssStr, function(err, css){
//console.log(css)
//})
sass.render(scssStr, function(err, css){
console.log(css)
})
//console.log(sass.renderSync(scssStr));
console.log(sass.renderSync(scssStr));
//console.log(sass.renderSync({
//file: './test/sample.scss'
//}));
sass.render({
file: './test/sample.scss'
}, function(output) {
console.log(output);
});
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