Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = tools.makeStringTransform('sassify', {
includeExtensions: ['.css', '.sass', '.scss'],
evaluateArguments: true
}, function(content, opts, done) {
var self = this;
var inject = (typeof opts.config !== 'undefined' && typeof opts.config['auto-inject'] !== 'undefined') ? opts.config['auto-inject'] : false;
var file = opts.file;

Expand All @@ -24,6 +25,17 @@ module.exports = tools.makeStringTransform('sassify', {
options.outFile = opts.file;
options.sourceMapEmbed = true;
options.sourceMapContents = true;
options.importer = function(url, prev) {
var importAbsolutePath = path.resolve(path.dirname(prev), url);
var underscoredImportAbsolutePath = path.join(path.dirname(importAbsolutePath), '_' + path.basename(importAbsolutePath));
self.emit('file', importAbsolutePath);
self.emit('file', importAbsolutePath + '.scss');
self.emit('file', underscoredImportAbsolutePath);
self.emit('file', underscoredImportAbsolutePath + '.scss');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit aggressive to do every possible file, but the alternative is fs.exists. Not sure which is more/less intrusive?

return {
file: url
};
};
options.success = function (css) {
var exp;
if (inject) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Browserify middleware for adding required styles to the page.",
"main": "index.js",
"scripts": {
"test": "browserify -t . -r ./index.js:sassify test/*.js -o test_pack.js && cat test_pack.js | tape-run -b firefox | tap-spec && cat test_pack.js | tape-run -b phantom | tap-spec"
"test": "browserify -t . -r ./index.js:sassify test/*.js -o test_pack.js && cat test_pack.js | tape-run -b firefox | tap-spec && cat test_pack.js | tape-run -b phantom | tap-spec",
"test-importer": "node_modules/.bin/mocha test/importer.js"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to make too many assumptions here. Is mocha cool? would you prefer this command be added to the test script?

},
"repository": {
"type": "git",
Expand Down Expand Up @@ -57,9 +58,11 @@
},
"devDependencies": {
"browserify": "^9.0.3",
"mocha": "^2.2.1",
"tap-spec": "^2.2.2",
"tape": "^3.5.0",
"tape-run": "^0.3.0"
"tape-run": "^0.3.0",
"watchify": "^2.6.0"
},
"bugs": {
"url": "https://github.com/davidguttman/sassify/issues"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/_settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$bg: red;
1 change: 1 addition & 0 deletions test/fixtures/_settings.scss.new
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$bg: blue;
1 change: 1 addition & 0 deletions test/fixtures/main.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './something';
5 changes: 5 additions & 0 deletions test/fixtures/something.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import './settings';

body {
background-color: $bg;
}
50 changes: 50 additions & 0 deletions test/importer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var assert = require("assert");
var browserify = require("browserify");
var watchify = require("watchify");
var fs = require("fs");
var path = require("path");
var sassify = require("..");

function fixturePath(filename) {
return path.join(__dirname, 'fixtures', filename);
}

function copySync(src, dest) {
fs.writeFileSync(dest, fs.readFileSync(src));
}

var settingsContent = fs.readFileSync(fixturePath('_settings.scss'));

describe("the sass importer", function() {
before(function() {
this.b = browserify();
this.b.transform(sassify.configure({
'auto-inject': false
}))
.add(fixturePath('main.scss'));
this.w = watchify(this.b);
});

after(function() {
fs.writeFileSync(fixturePath('_settings.scss'), settingsContent);
if (this.w) {
this.w.close();
}
});

it("should update when an imported file changes", function(done) {
this.w.bundle(function(err, bundle) {
assert(bundle.toString().match(/background-color:\s*red/));

this.w.once('update', function() {
this.w.bundle(function(err, bundle) {
assert(bundle.toString().match(/background-color:\s*blue/));
done();
}.bind(this));
}.bind(this));

// This will trigger the watcher to update.
copySync(fixturePath('_settings.scss.new'), fixturePath('_settings.scss'));
}.bind(this));
});
});