Commit a956d188 by Adeel Mujahid

Merge pull request #589 from am11/master

Importer: General overhauling
parents f7a2ee52 84fe3fee
...@@ -5,8 +5,7 @@ var Emitter = require('events').EventEmitter, ...@@ -5,8 +5,7 @@ var Emitter = require('events').EventEmitter,
meow = require('meow'), meow = require('meow'),
replaceExt = require('replace-ext'), replaceExt = require('replace-ext'),
stdin = require('get-stdin'), stdin = require('get-stdin'),
render = require('../lib/render'), render = require('../lib/render');
fs = require('fs');
/** /**
* Initialize CLI * Initialize CLI
...@@ -109,9 +108,7 @@ function getEmitter() { ...@@ -109,9 +108,7 @@ function getEmitter() {
console.log(data); console.log(data);
}); });
emitter.on('done', function(){ emitter.on('done', process.exit);
process.exit(0);
});
return emitter; return emitter;
} }
...@@ -211,11 +208,10 @@ function run(options, emitter) { ...@@ -211,11 +208,10 @@ function run(options, emitter) {
} }
if (options.importer) { if (options.importer) {
if (fs.existsSync(options.importer)) { if ((path.resolve(options.importer) === path.normalize(options.importer).replace(/(.+)([\/|\\])$/, '$1'))) {
options.importer = require(options.importer); options.importer = require(options.importer);
} else { } else {
console.error('Could not locate importer.'); options.importer = require(path.resolve(process.cwd(), options.importer));
process.exit(1);
} }
} }
......
...@@ -144,7 +144,6 @@ function getOptions(options) { ...@@ -144,7 +144,6 @@ function getOptions(options) {
var error = options.error; var error = options.error;
var success = options.success; var success = options.success;
var importer = options.importer;
options.error = function(err, code) { options.error = function(err, code) {
try { try {
...@@ -173,23 +172,6 @@ function getOptions(options) { ...@@ -173,23 +172,6 @@ function getOptions(options) {
} }
}; };
if (importer) {
options.importer = function(file, prev, key) {
var done = function(data) {
binding.importedCallback({
index: key,
objectLiteral: data
});
};
var result = importer(file, prev, done);
if (result) {
done(result);
}
};
}
delete options.image_path; delete options.image_path;
delete options.include_paths; delete options.include_paths;
delete options.includePaths; delete options.includePaths;
...@@ -219,6 +201,25 @@ var binding = require(getBinding()); ...@@ -219,6 +201,25 @@ var binding = require(getBinding());
module.exports.render = function(options) { module.exports.render = function(options) {
options = getOptions(options); options = getOptions(options);
var importer = options.importer;
if (importer) {
options.importer = function(file, prev, key) {
function done(data) {
binding.importedCallback({
index: key,
objectLiteral: data
});
}
var result = importer(file, prev, done);
if (result) {
done(result);
}
};
}
options.data ? binding.render(options) : binding.renderFile(options); options.data ? binding.render(options) : binding.renderFile(options);
}; };
...@@ -232,6 +233,14 @@ module.exports.render = function(options) { ...@@ -232,6 +233,14 @@ module.exports.render = function(options) {
module.exports.renderSync = function(options) { module.exports.renderSync = function(options) {
options = getOptions(options); options = getOptions(options);
var importer = options.importer;
if (importer) {
options.importer = function(file, prev) {
return { objectLiteral: importer(file, prev) };
};
}
var status = options.data ? binding.renderSync(options) : binding.renderFileSync(options); var status = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
var result = options.result; var result = options.result;
......
...@@ -28,10 +28,10 @@ module.exports = function(options, emitter) { ...@@ -28,10 +28,10 @@ module.exports = function(options, emitter) {
importer: options.importer importer: options.importer
}; };
if (options.src) { if (options.data) {
renderOptions.file = options.src;
} else if (options.data) {
renderOptions.data = options.data; renderOptions.data = options.data;
} else if (options.src) {
renderOptions.file = options.src;
} }
renderOptions.success = function(result) { renderOptions.success = function(result) {
......
...@@ -124,7 +124,6 @@ function testBinary(options) { ...@@ -124,7 +124,6 @@ function testBinary(options) {
return build(options); return build(options);
} }
return; // TODO: remove it once TravisCI build pass 90% and above tests
console.log('`' + options.bin + '` exists; testing'); console.log('`' + options.bin + '` exists; testing');
var total; var total;
......
Subproject commit cf7c1d14fec91a66ab9c2a3050dcec5fab6198f5 Subproject commit 31521ef3ece636892f395a80392448ceae449b90
...@@ -4,7 +4,7 @@ extern "C" { ...@@ -4,7 +4,7 @@ extern "C" {
using namespace std; using namespace std;
void compile_it(uv_work_t* req) { void compile_it(uv_work_t* req) {
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data); sass_context_wrapper* ctx_w = (sass_context_wrapper*)req->data;
if (ctx_w->dctx) { if (ctx_w->dctx) {
compile_data(ctx_w->dctx); compile_data(ctx_w->dctx);
...@@ -23,13 +23,14 @@ extern "C" { ...@@ -23,13 +23,14 @@ extern "C" {
} }
sass_context_wrapper* sass_make_context_wrapper() { sass_context_wrapper* sass_make_context_wrapper() {
auto ctx_w = (sass_context_wrapper*)calloc(1, sizeof(sass_context_wrapper)); sass_context_wrapper* ctx_w = (sass_context_wrapper*)calloc(1, sizeof(sass_context_wrapper));
uv_mutex_init(&ctx_w->importer_mutex); uv_mutex_init(&ctx_w->importer_mutex);
uv_cond_init(&ctx_w->importer_condition_variable); uv_cond_init(&ctx_w->importer_condition_variable);
return ctx_w; return ctx_w;
} }
void sass_free_context_wrapper(sass_context_wrapper* ctx_w) { void sass_wrapper_dispose(struct sass_context_wrapper* ctx_w, char* string = 0) {
if (ctx_w->dctx) { if (ctx_w->dctx) {
sass_delete_data_context(ctx_w->dctx); sass_delete_data_context(ctx_w->dctx);
} }
...@@ -48,6 +49,14 @@ extern "C" { ...@@ -48,6 +49,14 @@ extern "C" {
NanDisposePersistent(ctx_w->result); NanDisposePersistent(ctx_w->result);
if(string) {
free(string);
}
}
void sass_free_context_wrapper(sass_context_wrapper* ctx_w) {
sass_wrapper_dispose(ctx_w);
free(ctx_w); free(ctx_w);
} }
} }
...@@ -22,6 +22,7 @@ extern "C" { ...@@ -22,6 +22,7 @@ extern "C" {
const char* file; const char* file;
const char* prev; const char* prev;
void* cookie; void* cookie;
bool is_sync;
Sass_Import** imports; Sass_Import** imports;
NanCallback* success_callback; NanCallback* success_callback;
NanCallback* error_callback; NanCallback* error_callback;
...@@ -29,7 +30,8 @@ extern "C" { ...@@ -29,7 +30,8 @@ extern "C" {
}; };
struct sass_context_wrapper* sass_make_context_wrapper(void); struct sass_context_wrapper* sass_make_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx_w); void sass_wrapper_dispose(struct sass_context_wrapper*, char*);
void sass_free_context_wrapper(struct sass_context_wrapper*);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -364,36 +364,6 @@ describe('api', function() { ...@@ -364,36 +364,6 @@ describe('api', function() {
describe('.renderSync(importer)', function() { describe('.renderSync(importer)', function() {
var src = read(fixture('include-files/index.scss'), 'utf8'); var src = read(fixture('include-files/index.scss'), 'utf8');
it('should override imports with "data" as input and fires callback with file and contents', function(done) {
var result = sass.renderSync({
data: src,
importer: function(url, prev, done) {
done({
file: '/some/other/path.scss',
contents: 'div {color: yellow;}'
});
}
});
assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
done();
});
it('should override imports with "file" as input and fires callback with file and contents', function(done) {
var result = sass.renderSync({
file: fixture('include-files/index.scss'),
importer: function(url, prev, done) {
done({
file: '/some/other/path.scss',
contents: 'div {color: yellow;}'
});
}
});
assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
done();
});
it('should override imports with "data" as input and returns file and contents', function(done) { it('should override imports with "data" as input and returns file and contents', function(done) {
var result = sass.renderSync({ var result = sass.renderSync({
data: src, data: src,
...@@ -424,34 +394,6 @@ describe('api', function() { ...@@ -424,34 +394,6 @@ describe('api', function() {
done(); done();
}); });
it('should override imports with "data" as input and fires callback with file', function(done) {
var result = sass.renderSync({
data: src,
importer: function(url, /* jshint unused:false */ prev, done) {
done({
file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
});
}
});
assert.equal(result.css.trim(), '');
done();
});
it('should override imports with "file" as input and fires callback with file', function(done) {
var result = sass.renderSync({
file: fixture('include-files/index.scss'),
importer: function(url, prev, done) {
done({
file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
});
}
});
assert.equal(result.css.trim(), '');
done();
});
it('should override imports with "data" as input and returns file', function(done) { it('should override imports with "data" as input and returns file', function(done) {
var result = sass.renderSync({ var result = sass.renderSync({
data: src, data: src,
...@@ -480,34 +422,6 @@ describe('api', function() { ...@@ -480,34 +422,6 @@ describe('api', function() {
done(); done();
}); });
it('should override imports with "data" as input and fires callback with contents', function(done) {
var result = sass.renderSync({
data: src,
importer: function(url, prev, done) {
done({
contents: 'div {color: yellow;}'
});
}
});
assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
done();
});
it('should override imports with "file" as input and fires callback with contents', function(done) {
var result = sass.renderSync({
file: fixture('include-files/index.scss'),
importer: function(url, prev, done) {
done({
contents: 'div {color: yellow;}'
});
}
});
assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
done();
});
it('should override imports with "data" as input and returns contents', function(done) { it('should override imports with "data" as input and returns contents', function(done) {
var result = sass.renderSync({ var result = sass.renderSync({
data: src, data: src,
......
var path = require('path'); var path = require('path');
module.exports = function(file) {console.log('>>>>>>>>>>');console.log(path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss')))); module.exports = function(file) {
return { return {
file: path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss'))) file: path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss')))
}; };
......
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
"index.scss" "index.scss"
], ],
"sourcesContent": [], "sourcesContent": [],
"mappings": "AAAA;EACE,AAAO;EACP,AAAQ;;AAGV,AAAQ;EACN,AAAiB;;AAGnB,AAAQ;EACN,AAAO;EAAT,AAAQ,AAAG;IAGP,AAAa", "mappings": "AAAA;EACE,AAAO;EACP,AAAQ;;AAGV,AAAQ;EACN,AAAiB;;AAGnB,AAAQ;EACN,AAAO;EAET,AAAQ,AAAG;IACP,AAAa",
"names": [] "names": []
} }
Subproject commit 00c6154dba85a38bb03b89d7279f439feb055821 Subproject commit 3dc1f5758786627f41cebd275238d9224a593e5f
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